Matlab code for non linear equation using newton Method
Ältere Kommentare anzeigen
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 Kommentare
Nameer Ahmad
am 19 Dez. 2021
Bearbeitet: Torsten
am 19 Dez. 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
am 19 Dez. 2021
Antworten (0)
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!