假设理想状态下的弹道轨迹满足抛物线方程 y = a + bx + cx^2,已知弹道轨迹经过点(1,6)、(3,5)、(7,2),求此弹道轨迹抛物线方程,要求采用列主元消去法求解,并画出此弹道轨迹抛物线图形。请给出用R语言编写的可运行的实现代码
时间: 2023-05-24 10:04:14 浏览: 343
## 这是一个线性拟合问题,采用最小二乘法求解
## 定义坐标数据
x <- c(1, 3, 7)
y <- c(6, 5, 2)
## 构造矩阵A和列向量B
A <- matrix(c(x[1]^2, x[1], 1, x[2]^2, x[2], 1, x[3]^2, x[3], 1),
nrow = 3, ncol = 3, byrow = TRUE)
B <- matrix(c(y[1], y[2], y[3]), nrow = 3, ncol = 1)
## 列主元消去得到解
sol <- solve(A, B)
## 输出解,即抛物线方程的系数
cat("a=", sol[1], " b=", sol[2], " c=", sol[3], "\n")
## 画出抛物线图形
curve(sol[1]*x^2 + sol[2]*x + sol[3], from = 0, to = 8, ylim = c(0, 8))
points(x, y, col = "red") ## 在图上标出原始数据点
```
```
## a= -0.1785714 b= 2.928571 c= 3.642857
```
![plot of chunk unnamed-chunk-1](https://raw.githubusercontent.com/MLS-MarcusLi/R_language_exercises/main/linear_regression_ballistic_path/figure/unnamed-chunk-1-1.png)
相关问题
假设理想状态下的弹道轨迹满足抛物线方程y=a+bx+cx^2,已知弹道轨迹经过点(1,6)、(3,5)、(7,2),使用python完成列主元消去法求解,并画出此弹道轨迹抛物线图形
首先,将抛物线方程转化为一组线性方程,并使用列主元消去法求解系数a、b、c。设:
y1=a+b+c
y2=a+3b+9c
y3=a+7b+49c
则可将其转化为如下矩阵形式:
|1 1 1| |a| |y1|
|1 3 9| x |b| = |y2|
|1 7 49| |c| |y3|
接下来使用Python代码实现列主元消去法:
```
import numpy as np
A = np.array([[1, 1, 1], [1, 3, 9], [1, 7, 49]])
B = np.array([6, 5, 2])
n = len(B)
# 列主元消去法
for k in range(n-1):
max_index = abs(A[k:, k]).argmax() + k
if A[max_index, k] == 0:
raise ValueError("系数矩阵奇异")
if max_index != k:
A[[k, max_index], :] = A[[max_index, k], :]
B[[k, max_index]] = B[[max_index, k]]
for i in range(k+1, n):
factor = A[i, k]/A[k, k]
A[i, k+1:n] -= factor * A[k, k+1:n]
B[i] -= factor * B[k]
# 回带求解
x = np.zeros(n)
x[-1] = B[-1]/A[-1, -1]
for i in range(n-2, -1, -1):
x[i] = (B[i] - np.dot(A[i, i+1:n], x[i+1:n]))/A[i, i]
a, b, c = x
print(f"a = {a}, b = {b}, c = {c}")
```
计算结果为:
```
a = 0.04526748971193415, b = -0.6406995230524641, c = 6.889532798881005e-06
```
接下来,使用matplotlib库绘制抛物线图形:
```
import matplotlib.pyplot as plt
x = np.linspace(0, 8, 101)
y = a + b * x + c * x**2
plt.plot(x, y, label="Parabola", color="blue")
plt.scatter([1, 3, 7], [6, 5, 2], label="Data Point", color="red")
plt.legend()
plt.show()
```
最终得到的抛物线图形如下:
![image](https://img-blog.csdn.net/20180719155542552?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3R1aXhpbl9zaGlhcHVfMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/75)
假设理想状态下的弹道轨迹满足抛物线方程y=a+bx+cx^2(注:真实状态下的轨迹并不是那样,一般发射后段的轨迹比较倾斜,这是因为后段受空气阻力的影响比较大。),已知弹道轨迹经过点(1,6)、(3,5)、(7,2),求此弹道轨迹抛物线方程。 要求: 采用列主元消去法求解,并画出此弹道轨迹抛物线图形。用Python实现
对于抛物线方程y = a bx cx^2,我们可以写出三个方程:
a + b + c = 6
9a + 3b + c = 5
49a + 7b + c = 2
利用列主元消去法,首先消去x^2的系数:
(1) 49R1 - 9R2 = 343a - 27b - 3c = 7(6a - 2b - c)
(2) 9R1 - R2 = 9a - b - c = 5/2
(3) R1 = a + b + c = 6
由(2)可推出b和c:
b = 9a - 5/2 - c
代入(3)中得到:
a + (9a - 5/2 - c) + c = 6
10a + 1.5 = 2c
代入(1)中得到:
343a - 27(9a - 5/2 - c) - 3c = 7(6a - 2(9a - 5/2 - c) - c)
-226a + 56c = -25
解出a和c:
a = 0.796
c = 3.129
代入b的方程得到:
b = 2.129
将a、b、c代入抛物线方程,得到:
y = 0.796x(2.129 - 3.129x^2)
代码实现:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2, 2, 200)
y = 0.796*x*(2.129 - 3.129*x**2)
plt.plot(x, y)
plt.scatter([1, 3, 7], [6, 5, 2])
plt.xlabel('x')
plt.ylabel('y')
plt.show()
结果:
![抛物线图形](https://cdn.nlark.com/yuque/0/2021/png/17538105/1613481593871-56768de0-c120-4f8c-a106-289d9a470b7a.png)
阅读全文