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
Nameer Ahmad
2021년 12월 19일
편집: Torsten
2021년 12월 19일
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
2021년 12월 19일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!