%Matlab code for Newton method
clear all
close all
%nonlinear function to be solved
syms x1 x2 x3
f1(x1,x2,x3)=6*x1-2*cos(x2*x3)-1;
f2(x1,x2,x3)=9*x2+sqrt(x1.^2+sin(x3)+1.06)+0.9;
f3(x1,x2,x3)=60*x3+3*exp(-x1*x2)+10*pi-3;
%displaying the function
fprintf('The 1st function is f1(x1,x2,x3)=\n')
disp(f1)
fprintf('The 2nd function is f2(x1,x2,x3)=\n')
disp(f1)
fprintf('The 3rd function is f3(x1,x2,x3)=\n')
disp(f1)
%finding the Jacobian matrix
f1_x1(x1,x2,x3)=diff(f1,x1);
f1_x2(x1,x2,x3)=diff(f1,x2);
f1_x3(x1,x2,x3)=diff(f1,x3);
f2_x1(x1,x2,x3)=diff(f2,x1);
f2_x2(x1,x2,x3)=diff(f2,x2);
f2_x3(x1,x2,x3)=diff(f2,x3);
f3_x1(x1,x2,x3)=diff(f3,x1);
f3_x2(x1,x2,x3)=diff(f3,x2);
f3_x3(x1,x2,x3)=diff(f3,x3);
jac1=[f1_x1 f1_x2 f1_x3;...
f2_x1 f2_x2 f2_x3;...
f3_x1 f3_x2 f3_x3];
fprintf('The Jacobian Matrix is\n')
disp(jac1)
%All initial guess for x y z
x11=0;x22=0;x33=0;
fprintf('For initial condition x1=%f, x2=%f,
x3=%f \n',x11,x22,x33)
kmax=1000; %maximum number of iterations
fprintf('\tk\t X_1 \t X_2 \t X_3 \t
|X_k-X_k-1|\n\n')
fprintf('\t%d\t %2.5f \t %2.5f \t %2.5f
\t%2.2e\n',0,x11,x22,x33,NaN)
%loop for Newton method
for i=1:kmax
jac=jac1(x11,x22,x33);
ijac=inv(jac);
xx=double([x11;x22;x33]-ijac*[f1(x11,x22,x33);f2(x11,x22,x33);f3(x11,x22,x33)]);
err=norm(xx-[x11;x22;x33]);
x11=double(xx(1));
x22=double(xx(2));
x33=double(xx(3));
fprintf('\t%d\t %2.5f \t
%2.5f \t %2.5f \t%2.2e\n',i,x11,x22,x33,err)
result(i,1:3)=[x11,x22,x33];
if err<10^-9
break
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Matlab code for Newton method clear all close all nonlinear function to be solved syms x1 x2 x3 f1 ( x 1 , x2,x3)-6*x1-2*cos (x2+x3)-1 ; f2 (xl, x2, x3) 9*x2+sqrt(x1.2+sin (x3) +1.06) +0.9; f3 (xl, x2, x3) 60 x3+3*exp(-x1 x2) +10 pi-3; displaying the function fprintf( The 1st function is fl(xl,x2,x3)-In) disp (E1) fprintf The 2nd function is f2(x1,x2,x3) disp(fl) fprintf(The 3rd function is f3 (x1,x2,x3)-In) disp (fl) %finding the Jacobian matrix fl xl(xl, x2, x3)-diff (f1, x1); f1_x2 ( x1 , x2,x3)-diff(f1,x2); f1_x3 ( x1 , х 2 , x3 ) ediff ( f 1 , x3 ) ; f2_x1(xl, x2, x3)-diff (f2, x1); £2 x2 (xl, x2, x3)-diff(£2, x2) f2_x 3 (x1,x2,x3)-diff ( f2,x3); f3 xl(xl, x2, x3)-diff (E3, x1); f3_x2(xl, x2, x3)-diff (f3, x2); f3 x3(xl, x2, x3)-diff (E3, x3); f3_x1 f3_x2 f3_x 31: fprintf( The Jacobian Matrix is\n') disp jacl) %All initial guess for x y z x11-0;x22-0; x33-0; tprint f ("For initial condition x1-Af, x2-%f, x3-&E kmax-1000; %maximum number of iterations fprint f ("\t%d\t %2.5f \t %2.5f \t %2.5f \t%2.2e %loop for Newton method In',x11, x22,x33) n',0,x11, x22, x33, NaN) for i. 1:kmax
[ jac-jacl (x11,x22, x33 íjac=inv (jac); xx double( [x1l; x22;x33]- ijac [f1 (x11, x22,x33); £2 (x11,x22,x33); f3 (x11,x22, x33) ])i err-norm (xx- [x1l;x22 x33]) x11-double (xx(1)); x22-double (xx (2)); x33-double (xx(3)); fprint f ('\t%d\t %2.5f \t 82.5f \t \t82.2e %2.5f n',,x11, x22, x33,err) result (i,1:3) [x11, x22, x33 ] if err<10-9 break end end 88888888888888888888888888888888888888888888888888888888888888888888옹 The 1st function is f1(x1,x2 , x3)= 6*x1-2*cos (x2+x3) -1 symbolic function inputs: xl, x2, x3 The 2nd function is f2 (x1, x2, x3)- 6*x1-2*cos ( x2 * x3) -1 symbolic function inputs: xl, x2, x3 The 3rd function is f3 (x1, x2, x3)- 6*x1 2*cos (x2*x3) - 1 symbolic function inputs: x1, x2, x3 The Jacobian Matrix is 6, 2*x3*sin(x2*x3), 2*x2*sin(x2*x3) 1 x1/(x1*2 + sin(x3) +53/50) (1/2), -3*x2+exp (-x1+x2 ) , symbolic function inputs: xl, x2, x3 9, cos (x3)/ (2 (x1 2 sin (x3) + 53/50) (1/2))J -3*x1+exp (-x1 112 ), 60] For initial condition x1-0.000000, x2 0.000000, x3=0.000000 0 0.00000 0. 00000 0. 00000 NaN I 0.50000 -0.18614 0.52360 7.48e-01 2 0.49816 -0.19961-0.52883 1.46e-02 3 0.49814 -0.19961 0.52883 1.32e-05 4 0.49814 -0.19961-0.52883 7.58e-12