Newtons Method but error message "Singular Matrix"
이전 댓글 표시
When I tried to solve theta and V with Newton for systems I got this error message
- Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.765187e-24.
Previously I had no problems just solving theta with Newtons for systems, but adding V got me an ill conditioned matrix. We tried different initial values to see if they were triggering, but all of them lead to the same error message. We would be very grateful for any help we could get!
function [theta,V]= newtonfsys1(theta1,V1)
t=1; it=0; maxit=100;theta0=0;V0=200/3.6;
while norm(t)>1e-9 & it<maxit
%Creates a list of x and y values
[v1,v2,v3,v4,v5]=varden(theta0,V0);
[x0,y0]=rungekutta(v1,v2,v3,v4,v5);
[v1,v2,v3,v4,v5]=varden(theta1,V1);
[x1,y1]=rungekutta(v1,v2,v3,v4,v5);
%checking if condition is being met
for i=1:length(x1)
if (abs(11.8872-x1(i))<0.01) && (abs(18.288-x1(end))<0.01)
break
end
end
for p=1:length(x0)
if (abs(11.8872-x0(p))<0.01) && (abs(18.288-x0(end))<0.01)
break
end
end
% takes the "i" & "p" from the condition and picks a good(hopefully constant for x and y
y1=y1(i)-1.001; y0=y0(p)-1.001; x1=x1(i)-11.8872; x0=x0(p)-11.8872;
%approximate derivative
dxdtheta= (x1-x0)/(theta1-theta0);
dydtheta = (y1-y0)/(theta1-theta0);
dxdV = (x1-x0)/(V1-V0);
dydV = (y1-y0)/(V1-V0);
f=[x1;y1];
J = [dxdtheta,dxdV;dydtheta,dydV];
t=J\f;
disp(' theta V f t')
disp([theta1 norm(V1) norm(f) norm(t)])
theta0=theta1; V0=V1; theta1=theta1-t; V1=V1-t(2); it=it+1;
end
if it<maxit
theta=theta1; V=V1;
else
disp('Ingen konvergens!')
theta=[];
end
댓글 수: 9
Torsten
2023년 12월 7일
Shouldn't this be
theta1=theta1-t(1);
instead of
theta1=theta1-t;
?
Helen Hailegesesse
2023년 12월 7일
You are aware that you overwrite x0,x1,y0 and y1 as arrays by this line
% takes the "i" & "p" from the condition and picks a good(hopefully constant for x and y
y1=y1(i)-1.001; y0=y0(p)-1.001; x1=x1(i)-11.8872; x0=x0(p)-11.8872;
so that in the next iteration these loops
%checking if condition is being met
for i=1:length(x1)
if (abs(11.8872-x1(i))<0.01) && (abs(18.288-x1(end))<0.01)
break
end
end
for p=1:length(x0)
if (abs(11.8872-x0(p))<0.01) && (abs(18.288-x0(end))<0.01)
break
end
end
are senseless because x0 and x1 have become scalars ?
Or what does rungekutta return for x0,y0,x1 and y1 ?
So you see: we are not certain how your code works. We need an executable version that reproduces the error. Otherwise it seems almost impossible to help.
Helen Hailegesesse
2023년 12월 7일
편집: Helen Hailegesesse
2023년 12월 7일
Sorry, but I'm completely lost in reconstructing the problem you are trying to solve with your code.
Could you try to explain ? Is it a boundary-value problem you are trying to solve with Newton's method ?
To solve the system you included, you have to rewrite it in a system of first-order differential equations:
Let
z(1) = x, z(2) = xdot, z(3) = y, z(4) = ydot.
Then your system reads
dz(1)/dt = z(2)
dz(2)/dt = -K_x*(sqrt(z(2)^2+z(4)^2))^1.5 / m
dz(3)/dt = z(4)
dz(4)/dt = (-m*g - K_y*(sqrt(z(2)^2+z(4)^2))^1.5) / m
So there should be 4 Runge-Kutta updates, not only 2 as in your code.
Helen Hailegesesse
2023년 12월 7일
Helen Hailegesesse
2023년 12월 7일
Helen Hailegesesse
2023년 12월 7일
채택된 답변
추가 답변 (1개)
Landing on a point with ill-conditioned J is a hazard of plain-vanilla Newton's method. That's why people usually don't use it. They use fsolve instead.
댓글 수: 2
Helen Hailegesesse
2023년 12월 7일
Well, there are several things you can try,
- Use actual derivatives instead of finite differences
- Reduce the finite difference stepsize
- Choose a different initial point. Since it's only a 2 variable problem,you should be able to plot the objective function to see approximately where the solution lies.You could also plot cond(J) as a surface to see where the singularities are, and avoid them.
카테고리
도움말 센터 및 File Exchange에서 Linear Least Squares에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


