Matlab code for non linear equation using newton Method

clc;
clear all;
% Function f1 and f2
func = @(V,S) [V*sin(S) - 0.5; V^2 - V*cos(S)];
% Jacobian
jac = @(V,S) [sin(S), V*cos(S); 2*V - cos(S), V*sin(S)];
maxit=50; % Maximum iteration
tol=10^-6; % Tolerance
% Roots
V = zeros(maxit);
S = zeros(maxit);
% Initial guess
V(1) = 1; S(1) = 0;
% Newton Raphshon method of solving
for i = 1: maxit+1
F = func(V(i),S(i));
J = jac(V(i),S(i));
Dx = -J\F;
V(i+1) = V(i) + Dx(1);
S(i+1) = S(i) + Dx(2);
F = func(V(i+1),S(i+1));
if norm (F)<tol
break
end
disp(V(i+1));
disp(S(i+1));
end
% Output
fprintf('No. of iterations: %d\n',i);
fprintf('Roots: V = %f and S = %f rad \n',V(i+1),S(i+1));
i Want to stop this at 11 ietrations help me to get 11 iteration ansr with same tolernce

4 Comments

Everything's fine with your code.
You need 10 iterations to reach out, and this is the correct answer.
Maybe 11 iterations are required if a different norm is used to evaluate F.
x=[1;0];
f = @(v,q) [v*sin(q)-0.5;((v^2)-v*cos(q))];
df = @(v,q) [sin(q) v*cos(q);((2*v)-cos(q)) v*sin(q)];
tolerance = [1*10^-6;1*10^-6];
for i = 1:20
disp(x);
error = abs(f(x(1),x(2)));
x=x-(inv(df(x(1),x(2)))*f(x(1),x(2)));
%disp(error);
if error<tolerance
break
end
end
disp(x);
With this code it ends of 11 iteration and manualy also 11 plzz help
But you evaluate the error before the iteration here. This does not make much sense.
Furthermore, you use a different error norm (namely componentwise absolute value), but this does not play a role in this case.
And you should not use "inv". Better use "\" as in the first code in order to calculate Dx.
Tell me whch norm should i use to get 11 iterations

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Tags

Asked:

on 19 Dec 2021

Edited:

on 19 Dec 2021

Community Treasure Hunt

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

Start Hunting!