假设理想状态下的弹道轨迹满足抛物线方程y=a+bx+cx^2,已知弹道轨迹经过点(1,6)、(3,5)、(7,2),使用python完成列主元消去法求解,并画出此弹道轨迹抛物线图形
时间: 2023-05-23 17:06:04 浏览: 341
弹道轨迹画图
首先,将抛物线方程转化为一组线性方程,并使用列主元消去法求解系数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)
阅读全文