Newton Raphson gives answers divided by 0

5 次查看(过去 30 天)
I'm trying to estimate 4 angles and 2 distances using the newton raphson method with phi4 and theta as the input. I have 6 functions that equal to 0 and when i try substituting to calculate dx (the array of all my values) i get a division by 0. I transposed some arrays to columns, but id be surprised if thats the issue. The functions themselves check out and im mostly using matlab functions to make this work so I don't really know what could be wrong
Angle=3;
phi4=pi/180*Angle;
theta=linspace(0,2*pi, 72);
%First guesses
phi1=ones(1, 72);
phi2=ones(1, 72);
phi3=ones(1, 72);
phi5=ones(1, 72);
d=ones(1,72);
s=ones(1, 72);
syms phi1V phi2V phi3V phi5V dV sV;
xV=[sV, dV, phi1V, phi2V, phi3V, phi5V];
ds=0;
dd=0;
dphi1=0;
dphi2=0;
dphi3=0;
dphi4=0;
dx=[ds, dd, dphi1, dphi2, dphi3, phi5]';
lim=0.01;
i=1;
while(i<73)
%redefine functions for changes in theta
f1=25-sV+dV*cos(phi2V)+3.1*cos(phi3V);
f2=-5+dV*sin(phi2V)+3.1*sin(phi3V);
f3=-25+6.3*cos(theta(i))+27*cos(phi1V)-19.2*cos(phi2V);
f4=11+6.3*sin(theta(i))+27*sin(phi1V)-19.2*sin(phi2V);
f5=-40+6.3*cos(theta(i))+(dV-19.2)*cos(phi2V)+29.1*cos(phi5V)+16*cos(phi4);
f6=-24+6.3*sin(theta(i))+(dV-19.2)*sin(phi2V)+29.1*sin(phi5V)+16*sin(phi4);
f=[f1, f2, f3, f4, f5, f6]';
J=jacobian(f, xV);
M=-inv(J)*f;
x=[s(i), d(i), phi1(i), phi2(i), phi3(i), phi5(i)];
firstloop=1;
while(ds > lim | dd > lim | dphi1 > lim | dphi2 > lim | dphi3 > lim | dphi4 > lim | firstloop==1)
dx=subs(M, xV, x);
x=x+dx;
firstloop=0;
end
i=i+1;
end
Error using sym/subs
Division by zero.

采纳的回答

Sam Chak
Sam Chak 2024-3-10
During the computation of M, certain elements involve divisions by . In the line 'dx = subs(M, xV, x)', an attempt is made to substitute the variables in 'xV = [sV, dV, phi1V, phi2V, phi3V, phi5V]' with 'x = [1, 1, 1, 1, 1, 1]', which leads to . Consequently, it triggers the 'Division-by-zero' error message.

更多回答(2 个)

Torsten
Torsten 2024-3-10
编辑:Torsten 2024-3-10
Your linear systems in the course of the Newton iterations cannot be solved since the Jacobian is rank-deficient (it has only rank 5 instead of rank 6 as needed). This usually indicates that the equations you try to solve are not independent or even contradictory.
Angle=3;
phi4=pi/180*Angle;
theta=linspace(0,2*pi, 72);
%First guesses
phi1=ones(1, 72);
phi2=ones(1, 72);
phi3=ones(1, 72);
phi5=ones(1, 72);
d=ones(1,72);
s=ones(1, 72);
syms phi1V phi2V phi3V phi5V dV sV;
xV=[sV, dV, phi1V, phi2V, phi3V, phi5V];
ds=0;
dd=0;
dphi1=0;
dphi2=0;
dphi3=0;
dphi4=0;
%dx=[ds, dd, dphi1, dphi2, dphi3, phi5]';
lim=0.01;
i=1;
while(i<73)
%redefine functions for changes in theta
f1=25-sV+dV*cos(phi2V)+3.1*cos(phi3V);
f2=-5+dV*sin(phi2V)+3.1*sin(phi3V);
f3=-25+6.3*cos(theta(i))+27*cos(phi1V)-19.2*cos(phi2V);
f4=11+6.3*sin(theta(i))+27*sin(phi1V)-19.2*sin(phi2V);
f5=-40+6.3*cos(theta(i))+(dV-19.2)*cos(phi2V)+29.1*cos(phi5V)+16*cos(phi4);
f6=-24+6.3*sin(theta(i))+(dV-19.2)*sin(phi2V)+29.1*sin(phi5V)+16*sin(phi4);
f=[f1, f2, f3, f4, f5, f6]';
J=jacobian(f, xV);
%M=-inv(J)*f
x=[s(i), d(i), phi1(i), phi2(i), phi3(i), phi5(i)]
firstloop=1;
while(ds > lim | dd > lim | dphi1 > lim | dphi2 > lim | dphi3 > lim | dphi4 > lim | firstloop==1)
dx=-subs(J,xV,x)\subs(f,xV,x);%subs(M, xV, x)
rank(subs(J,xV,x))
rank([subs(J,xV,x),subs(f,xV,x)])
x=x+dx;
firstloop=0;
end
i=i+1;
end
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6
x = 1×6
1 1 1 1 1 1
Warning: Solution does not exist because the system is inconsistent.
ans = 5
ans = 6

Matt J
Matt J 2024-3-11
编辑:Matt J 2024-3-11
Raw Newton-Raphson is a pretty terrible method, but I assume you'll tell us you have no choice...
[f,M]=preanalysis(); %Symbolic pre-analysis
%%%%Numeric stuff -- Newton-Raphson
Angle=3;
phi4=pi/180*Angle;
theta=linspace(0,2*pi, 72);
lim=1e-6;
i=1;
clear solutions
while(i<73)
F=@(x) f(x, theta(i),phi4);
Update=@(x) M(x, theta(i),phi4);
x=F(zeros(6,1))/1000; %Initial guess - needs some experimentation
dx=inf;
firstloop=1;
while( all(abs(dx)>lim) | firstloop==1)
dx=Update(x);
if any(~isfinite(dx)),
warning 'Bad update. Aborting...'
break;
end
x=x+dx;
x(1:4)=atan2( sin(x(1:4)) , cos(x(1:4))); %normalize the 2*pi periodicity
firstloop=0;
end
solutions{i}=x;
i=i+1;
end
solutions=cell2mat(solutions);
solutions(:,1:20)
ans = 6×20
0.2505 0.2273 0.2054 0.1852 0.1665 0.1495 0.1342 0.1205 -1.3914 -1.3886 -1.3832 -1.3755 -1.3657 -1.3539 -1.3404 0.0657 0.0650 -1.2913 -1.2727 -1.2534 1.1719 1.1649 1.1619 1.1628 1.1675 1.1756 1.1870 1.2013 -2.5013 -2.5284 -2.5536 -2.5767 -2.5979 -2.6170 -2.6342 1.3896 1.4184 -2.6733 -2.6823 -2.6893 -1.1402 0.8784 0.7734 2.8885 -0.6993 -2.6619 -0.4288 1.4046 2.4737 -1.4770 1.7734 0.2960 -0.9963 -0.8606 1.4129 -0.8061 -2.7819 -2.7265 -1.8485 0.0196 0.9171 0.9085 0.8983 0.8866 0.8737 0.8598 0.8450 0.8294 0.7709 -2.6653 -2.6950 -2.7217 0.6895 2.9977 0.6579 -0.1126 -0.9554 3.0019 0.6113 0.6041 19.2667 18.8273 18.4260 18.0661 17.7507 17.4828 17.2651 17.1002 21.3498 -36.4085 -36.4210 -36.5029 21.1271 -62.8851 20.4115 119.0125 60.3767 -62.0026 18.8311 18.3472 6.4689 27.3019 31.5546 8.4895 3.5565 10.3017 1.9542 45.0152 31.9151 32.4906 51.2072 24.6759 6.1926 138.3313 -16.2680 8.5029 -95.8681 524.2912 -2.9438 89.7521
function [F,M]=preanalysis()
%%%Symbolic stuff
syms phi1 phi2 phi3 phi5 d s theta phi4 real
f1=25-s+d*cos(phi2)+3.1*cos(phi3);
f2=-5+d*sin(phi2)+3.1*sin(phi3);
f3=-25+6.3*cos(theta)+27*cos(phi1)-19.2*cos(phi2);
f4=11+6.3*sin(theta)+27*sin(phi1)-19.2*sin(phi2);
f5=-40+6.3*cos(theta)+(d-19.2)*cos(phi2)+29.1*cos(phi5)+16*cos(phi4);
f6=-24+6.3*sin(theta)+(d-19.2)*sin(phi2)+29.1*sin(phi5)+16*sin(phi4);
f=[f1, f2, f3, f4, f5, f6]';
x=[phi1 phi2 phi3 phi5 d s]';
J=jacobian(f, x);
M=matlabFunction(-J\f,'Vars',{x,theta,phi4});
F=matlabFunction(f,'Vars',{x,theta,phi4});
end

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by