Matlab code for non linear equation using newton Method
Show older comments
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
Nameer Ahmad
on 19 Dec 2021
Edited: Torsten
on 19 Dec 2021
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.
Nameer Ahmad
on 19 Dec 2021
Answers (0)
Categories
Find more on Programming 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!