I am attempting to write a code for Newton's Method for nonlinear systems in Python. My g function is a 5x1 matrix and the jacobian (derivative matrix) of this is a 5x5 matrix. The vector for the initial y values (y0) is also a 5x1. i keep on getting the error
ValueError: solve: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (m,m),(m,n)->(m,n) (size 1 is different from 5)
I have tried solving my problem manually and I get my answer however when I run my code. I suspect that the error is something silly that I am simply overlooking. But I just can't for the life of me figure out what the issue is. Below is my code:
def newton_prob(y0, g, jac, tol):
max_iteration = 100
tol = 1e-6
y_value = y0
for k in range(max_iteration):
J = np.array(jac(y_value))
G = np.array(g(y_value))
diff = np.linalg.solve(J, -G)
y_value = y_value + diff
stopcrit = np.linalg.norm(y_value - y0, 2) / np.linalg.norm(y0, 2)
if stopcrit < tol:
print('Convergence, nre iter:' , k)
break
else:
return y_value
#Test
y0 = np.array([[17],
[17],
[17],
[17],
[17]])
g = lambda y: np.array([[-9*y[1] + 18*y[0] - 9*(17) - (3/16)*y[0]*y[1] + (3/16)*y[0]*(17) + (124/27)],
[-9*y[2] + 18*y[1] -9*y[0] -(3/16)*y[1]*y[2] + (3/16)*y[0]*y[1] +(557/108)],
[-9*y[3] + 18*y[2] -9*y[1] + (3/16)*y[1]*y[2] - (3/16)*y[2]*y[3] + 6],
[-9*y[4] + 9*y[3] -9*y[2] - (3/16)*y[3]*y[4] + (3/16)*y[2]*y[3] + (775/108)],
[-9*(43/3) +18*y[4] -9*y[3] + (3/16)*y[3]*y[4] - (3/16)*y[4]*(43/3) + (236/27)]])
jac = lambda y: np.array([[18 -(3/16)*y[1] + (3/16)*(17), -9 -(3/16)*y[0], 0, 0, 0],
[-9 + (3/16)*y[1], 18 - (3/16)*y[2] + (3/16)*y[0], -9 - (3/16)*y[1], 0, 0],
[0, -9 + (3/16)*y[2], 18 + (3/16)*y[1] - (3/16)*y[3], -9 - (3/16)*y[2], 0],
[0, 0, -9 + (3/16)*y[3], 9 - (3/16)*y[3] + (3/16)*y[2], -9 - (3/16)*y[3]],
[0, 0, 0, -9 + (3/16)*y[4], 18 + (3/16)*y[3] - (3/16)*(43/3)]])
tol = 1e-6
print(newton_prob(y0, g, jac, tol))
Please help if possible
The dimensions of y0
and g
seems to be wrong. Reduce them by one dimension:
y0 = np.array([17,
17,
17,
17,
17])
g = lambda y: np.array([-9*y[1] + 18*y[0] - 9*(17) - (3/16)*y[0]*y[1] + (3/16)*y[0]*(17) + (124/27),
-9*y[2] + 18*y[1] -9*y[0] -(3/16)*y[1]*y[2] + (3/16)*y[0]*y[1] +(557/108),
-9*y[3] + 18*y[2] -9*y[1] + (3/16)*y[1]*y[2] - (3/16)*y[2]*y[3] + 6,
-9*y[4] + 9*y[3] -9*y[2] - (3/16)*y[3]*y[4] + (3/16)*y[2]*y[3] + (775/108),
-9*(43/3) +18*y[4] -9*y[3] + (3/16)*y[3]*y[4] - (3/16)*y[4]*(43/3) + (236/27)])
Output:
[ 1.90727371e-01 -1.59772226e+01 -4.74196657e+01 -5.16165838e+03 4.86453399e+01]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.