The following nonlinear equations contain terms that are oft
Solution
clc,clear
% Newton Raphson solution of two nonlinear algebraic equations
% set up the iteration
error1 = 1.e-4;
xx(1) = 1; % initial guesses
xx(2) = 0;
iter=0;
maxiter=40
% begin iteration
while error1>1.e-12
iter=iter+1;
x1 = xx(1);
x2= xx(2);
% calculate the functions
f(1) = 10*x1*sin(x2)+2;
f(2) = 10*x1^2-10*x1*cos(x2)+1;
% calculate the Jacobian
J(1,1) = 10*sin(x2);
J(1,2) = 10*x1*cos(x2);
J(2,1) = 20*x1-10*cos(x2);
J(2,2) = 10*x1*sin(x2);
% solve the linear equations
y = -J\\f\';
% move the solution, xx(k+1) - xx(k), to xx(k+1)
xx = xx + y\';
% calculate norms
error1=sqrt(y(1)^2+y(2)^2);
error(iter)=sqrt(f(1)^2+f(2)^2);
ii(iter)=iter;
if (iter > maxiter)
error1 = 0;
s=sprintf(\'****Did not converge within %3.0f iterations.****\',maxiter);
disp(s)
end
% check if error1 < 1.e-12
end
x1 = xx(1);
x2 = xx(2);
f(1) = 10*x1*sin(x2)+2;
f(2) = 10*x1^2-10*x1*cos(x2)+1;
% print results
f
xx
iter
% plot results
semilogy(ii,error)
xlabel(\'iteration number\')
ylabel(\'norm of functions\')
clear ii
clear error
RESULT:
maxiter =
40
xx =
0.8554 -0.2360
iter =
6

