Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

function [x,k] = newtons(f,x,tol,nmax) % % function [x,k] = newtons(f,x,tol,nmax

ID: 3209804 • Letter: F

Question

function [x,k] = newtons(f,x,tol,nmax)

%

% function [x,k] = newtons(f,x,tol,nmax)

%

% This function returns in x a column vector x_k such that

% || x_k - x_{k-1} || < tol (1 + ||x_k||)

% and in k the number of iterations (Jacobian evaluations) required.

% On entry, x contains an initial guess.

% If k equals nmax then no convergence has been reached.

%

% The iterates ||f(x_k)|| are recorded. This option

% can be easily turned off

%Initialize

x = x(:); % ensure x is a column vector

fprintf ('k ||f(x_k)|| ')

format long g

%Newton

for k=1:nmax

[fx,Jx] = feval(f,x);

fprintf ('%d %e ',k-1,norm(fx) )

p = -Jx fx;

x = x + p;

if norm(p) < tol*(1+norm(x))

fx = feval(f,x);

fprintf ('%d %e ',k,norm(fx) )

return

end

end

k = nmax;

In class, we considered a special case of nonlinear systems and in particular we de- rived Newton's method for a system consisting of two nonlinear equations. Using a simplified notation, we focused on the system 9(x,y) o where Newton's method takes the form with k - 0,1,... , and subscripts denote partial derivatives with respect to variables denoted therein (f,- f/ f,- f / u. and so on) Write a MATLAB code employing Newton's method given by Eqs. (2)-(3) in order to solve Eq. (1) with f(x,y) g(x, y) x+y-2x , x2 + y2-2x + 2y + 1. = = Consider the initial guess 0.5 and stopping criteria specified by with tol 10-10 and 11-11 stands for the norm. In MATLAB the norm is implemented by the command norm (type help norm in the command window for further details) Attach your code and provide MATLAB output showcasing the expected quadratic convergence too.

Explanation / Answer

clc;
clear all
x=0.1;y=-0.5;%inital guess
err=0.1;
tol=1e-10;

format long

k=0;

while(err>tol*eps)

f=x+y-2*x*y; %function 1
g=x.^2+y.^2-2*x+2*y+1; % function 2

  
    fx=1-2*y; % derivative with x for function 1
    fy=1-2*x; %derivative with y for function 1
  
    gx=2*x-2; %derivative with x for function 2
    gy=2*y+2; % derivative with y for function 2
  
    x1=x-((f*gy-g*fy)/(fx*gy-gx*fy));
    y1=y-((g*fx-f*gx)/(fx*gy-gx*fy));
     err=norm([x y]-[x1 y1],2);
     eps=1+norm([x1 y1],2);
    x=x1;
    y=y1;
   err1(k+1)=err;
  

k=k+1;
end
disp('number of iterations ')
k
disp('solution of system')
x
y
disp('Error of each iteration')
err1'
disp('Convergence rate')
log(err1(3)/err1(2))/log(err1(2)/err1(1))

%%%%%%%%%%%%%%%%% Out put%%%%%%%%%%%%%%%%%%%

number of iterations

k =

     5

solution of system

x =

   0.215760915631622


y =

-0.379541251533151

Error of each iteration

ans =

   0.158557928056235
   0.014651844732945
   0.000146629996165
   0.000000015640846
   0.000000000000000

Convergence rate

ans =

   1.933363517678084