我想为不同的输入变量Q2和Q3 求解一个函数并将其存储。现在我的问题是如何定义Q2和Q3并返回每个步骤的值,以便以后可以绘制它们。
Q2 = np.array([20, 25, 30, 25, 20, 15, 10, 15, 20, 25, 30, 25, 20])
Q3 = np.array([40, 45, 50, 45, 40, 35, 30, 35, 40, 45, 50, 45, 40])
for i in range(13):
def function(xyz):
x, y, z = xyz
return [x -y -Q2[i], y + z -Q3[i], K[0]*x**2 + K[1]*y**2 - K[2]*z**2]
def jacobian(xyz):
x, y, z = xyz
return [[1, -1, 0],
[0, 1, 1],
[K[0]*x*2, K[1]*y*2, -K[2]*z*2]]
def iterative_newton(fun, x_init, jacobian):
max_iter = 300
epsilon = 1e-8
x_last = x_init
for k in range(max_iter):
# Solve J(xn)*( xn+1 - xn ) = -F(xn):
J = np.array(jacobian(x_last))
F = np.array(fun(x_last))
diff = np.linalg.solve( J, -F )
x_last = x_last + diff
# Stop condition:
if np.linalg.norm(diff) < epsilon:
print('converged with req. iterations:', k)
break
else: # only if the for loop end 'naturally'
print('not converged')
return x_last
# Answer to iteration:
x_sol = iterative_newton(function, [10, 10, 10], jacobian)
print(x_sol)
我想要的输出为:
[[x[0] y[0] z[0]], [x[1] y[1] z[1]], ... , [x[n] y[n] z[n]]
以便我可以在图中绘制它们。