4 nonlinear equations by using Newton's method
5 views (last 30 days)
Show older comments
I am attempting to solve 4 nonlinear equations with 4 variables but I couldn't find where is my false. can anybody help me?
here is my code:
clc,clear,clear all
format long
a1=64;
b1=64;
c1=64;
a2=96;
b2=96;
c2=96;
d2=160;
d3=160;
d1=((d2^2)+(d3^2))^(1/2);
m=300;
n=100;
r=250;
s=150;
teta1=75*pi/180; %radian for initial angle.
teta3=110*pi/180; %radian for initial angle.
teta5=200*pi/180; %radian for initial angle.
beta=135*pi/180; %radian for initial angle.
Es = 10^-6;
syms teta2 teta4 teta6 tetap
f1=(a1*cos(teta1))+(a2*cos(teta2))+(d1*cos(tetap))-m-(b1*cos(teta3))-(b2*cos(teta4));
f2=(a1*sin(teta1))+(a2*sin(teta2))+(d1*sin(tetap))-n-(b1*sin(teta3))-(b2*sin(teta4));
f3=(b1*cos(teta3))+(b2*cos(teta4))+(d2*cos(tetap+beta))+s-(c1*cos(teta5))-(c2*cos(teta6));
f4=(b1*sin(teta3))+(b2*sin(teta4))+(d2*cos(tetap+beta))-r-(c1*sin(teta5))-(c2*sin(teta6));
error = 1; k= 1; X1(k)=0*pi/180; X2(k)=120*pi/180; X3(k)=250*pi/180; X4(k)=45*pi/180;
while (error > Es)
J= jacobian([f1, f2, f3, f4],[teta2,teta4,teta6,tetap]);
FX = subs(subs(subs([f1; f2; f3; f4], [teta2,teta4,teta6,tetap], [X1(k),X2(k),X3(k),X4(k)])));
delta = subs(subs(subs(J^-1,[teta2,teta4,teta6,tetap], [X1(k),X2(k),X3(k),X4(k)])))*FX;
X1(k+1) = X1(k) - delta(1);
X2(k+1) = X2(k) - delta(2);
X3(k+1) = X3(k) - delta(3);
X4(k+1) = X4(k) - delta(4);
error = vpa((FX(1)^2 + FX(2)^2 + FX(3)^2 + FX(4)^2)/4)
k=k+1;
end
fprintf('At iteration no.%10.d ' ,(k));
fprintf('the root estimates xr= [%10.d] are%10./n')
disp(X1(k));
disp(X2(k));
disp(X3(k));
disp(X4(k));
0 Comments
Answers (1)
John D'Errico
on 17 Nov 2024
Edited: John D'Errico
on 17 Nov 2024
Why are you writing Newton's method code to solve a problem? Never write code to do numerical methods work, if far better code is already available, written by professionals.
But just for kicks, try using a tool like vpasolve.
a1=64;
b1=64;
c1=64;
a2=96;
b2=96;
c2=96;
d2=160;
d3=160;
d1=((d2^2)+(d3^2))^(1/2);
m=300;
n=100;
r=250;
s=150;
teta1=75*pi/180; %radian for initial angle.
teta3=110*pi/180; %radian for initial angle.
teta5=200*pi/180; %radian for initial angle.
beta=135*pi/180; %radian for initial angle.
Es = 10^-6;
syms teta2 teta4 teta6 tetap
f1=(a1*cos(teta1))+(a2*cos(teta2))+(d1*cos(tetap))-m-(b1*cos(teta3))-(b2*cos(teta4));
f2=(a1*sin(teta1))+(a2*sin(teta2))+(d1*sin(tetap))-n-(b1*sin(teta3))-(b2*sin(teta4));
f3=(b1*cos(teta3))+(b2*cos(teta4))+(d2*cos(tetap+beta))+s-(c1*cos(teta5))-(c2*cos(teta6));
f4=(b1*sin(teta3))+(b2*sin(teta4))+(d2*cos(tetap+beta))-r-(c1*sin(teta5))-(c2*sin(teta6));
Put vpasolve to work at it.
vpasolve([f1,f2,f3,f4],[teta2,teta4,teta6,tetap],[0 120 250 45]*pi/180)
Note that vpasolve finds no solutions at all. Maybe it is just your starting values?
vpasolve([f1,f2,f3,f4],[teta2,teta4,teta6,tetap],rand(1,4)*2*pi)
vpasolve([f1,f2,f3,f4],[teta2,teta4,teta6,tetap],rand(1,4)*2*pi)
vpasolve([f1,f2,f3,f4],[teta2,teta4,teta6,tetap],rand(1,4)*2*pi)
vpasolve([f1,f2,f3,f4],[teta2,teta4,teta6,tetap],rand(1,4)*2*pi)
Nope. Different random start points are no more lucky. That suggests your problem just has no solution.
Looking at your equations,...
vpa(f1,4)
ans =
96.0*cos(teta2) - 96.0*cos(teta4) + 226.3*cos(tetap) - 261.5
vpa(f2,4)
ans =
96.0*sin(teta2) - 96.0*sin(teta4) + 226.3*sin(tetap) - 98.32
vpa(f3,4)
ans =
160.0*cos(tetap + 2.356) + 96.0*cos(teta4) - 96.0*cos(teta6) + 188.3
vpa(f4,4)
ans =
160.0*cos(tetap + 2.356) + 96.0*sin(teta4) - 96.0*sin(teta6) - 168.0
If I just focus on the first equation (f1), it looks like only a rather narrow set of values for teta2, teta4, and tetap will possibly satisfy the first equation, since the constant term is rather large. Combine that with the other equations, and it appears no solution exists. This is probably why your Newton code fails.
The odds are good your equations are faulty, a common consequence of writing code with so many numbered variables. Somewhere, you made a mistake. Where, we have no clue, since we see no derivation for them.
2 Comments
John D'Errico
on 17 Nov 2024
Again though, it appears there is no solution to this system of equations. At least not for real sets of parameters. So you will need to check them carefully and see where they are not correct.
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!