Non linear Newton iteration method
Afficher commentaires plus anciens
Can anyone tell me problem in this code? i want to solve newton Iteration method where
X(i+1) = x(i)-Jcbi inv*F(i)
function NL_Newtons_Iteration(x0,y0,TC)
Tstart = cputime;
if nargin<4, x0=0; y0=0; z0=0; TC=10^-4; end
x1(1)=x0; x2(1)=y0; x3(1)=z0; i=0; error=TC+1;
X{1}=[x1(1); x2(1); x3(1)];
while(error>TC)
Jcbi=jacobian([F1,F2,F3],[x1,x2,x3]);
Jcbi_inv=inv(Jcbi);
X{i+2}=X{i+1}-Jcbi_inv*[F1(x1(i+1), x2(i+1), x3(i+1)); F2(x1(i+1), x2(i+1), x3(i+1)); F3(x1(i+1), x2(i+1), x3(i+1))];
Ex=100*abs((x1(i+1)-x1(i))/x1(i+1));
Ey=100*abs((x2(i+1)-x2(i))/x2(i+1)); %if 3 variables: E=[Ex Ey Ez];error=max(E)
Ez=100*abs((x3(i+1)-x3(i))/x3(i+1));
E=[Ex Ey Ez];
error=max(E);
i=i+1;
end
fprintf('After %d iterations an approimate solution is',i);
soln=[x1(i+1); x2(i+1); x3(i+1)]
TEnd = cputime - Tstart
end
function F1=F1(x1,x2,x3)
F1=((-3/5)*x1)+((1/4)*x2)+((1/4)*cos(x3))+1.43;
end
function F2=F2(x1,x2,x3)
F2=((1/4)*x1)-((3/5)*x2)-((1/4)*sin(x3))-1.24;
end
function F3=F3(x1,x2,x3)
F3=((1/4)*sin(x1))-((1/4)*exp(-x2))-((3/5)*x3)-1.17;
end
I am getting errors when I run the code. The x1 x2 x3 are x y z.
1 commentaire
Syed Abdul Rafay
le 29 Oct 2022
Réponses (1)
Similar to your other question, [F1,F2,F3] is not a function handle, but a vector. Here you call F1, F2 and F3 without inputs.
Better:
function Y = F123(x1,x2,x3)
Y = [((-3/5)*x1)+((1/4)*x2)+((1/4)*cos(x3))+1.43; ...
((1/4)*x1)-((3/5)*x2)-((1/4)*sin(x3))-1.24; ...
((1/4)*sin(x1))-((1/4)*exp(-x2))-((3/5)*x3)-1.17];
end
And in the loop:
Jcbi = jacobian(@F123, [x1,x2,x3]); % Failing!
But this still does not match the needs of the jacobian function, which requires symbolic input.
Catégories
En savoir plus sur Chemistry dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!