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!