How to get my code for Newtons method for non-linear equationsystems working?

3 views (last 30 days)
I'm trying to make my code for Newtons method for non-linear equationsystems working. I have three equations and three variables, u(1), u(2), u(3):
cos(u(1))+cos(u(2))+L3*cos(u(3))-2
sin(u(1))+sin(u(2))+L3*sin(u(3))
m2*tan(u(1))-(m1+m2)*tan(u(2))+m1*tan(u(3))
We have also been given several different values of L3, m1 and m2 to work with and the code should be able to find the solution for these different values. My code looks like this:
function g = uppgift12(L3, m1, m2)
u=[pi/4 pi/4 pi/4]';
iter=0;, dunorm=1;
while dunorm>0.5e-4 & iter<10
f=[cos(u(1))+cos(u(2))+L3*cos(u(3))-2
sin(u(1))+sin(u(2))+L3*sin(u(3))
m2*tan(u(1))-(m1+m2)*tan(u(2))+m1*tan(u(3))];
J=[-sin(u(1)) -sin(u(2)) -L3*sin(u(3))
cos(u(1)) cos(u(2)) L3*cos(u(3))
m2*sec(u(1))^2 -m1*sec(u(2))^2-m2*sec(u(2))^2 m1*sec(u(3))^2];
du=-J/f;
u=u+du;
dunorm=norm(du,inf), iter=iter+1;
end
u, iter
My startguesses is pi/4 for all three variables (that can change if necessary of course). I can't get the code to work however. I'm really bad at coding and I don't know what to do so I would be really grateful for any help I can get.

Answers (0)

Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!