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

Use an initial guess of (1, 0). (a) Write a MATLAB function that uses the Newton

ID: 3565486 • Letter: U

Question

Use an initial guess of (1, 0).
(a) Write a MATLAB function that uses the Newton-Raphson method to solve a nonlinear
system of equations. The function should accept a function handle for a the nonlinear
system, a function handle for the Jacobian of the nonlinear system, a stopping tolerance,
and maximum number of iterations. The function should return the solution and the
error estimate for each iteration. You may use the command to solve the linear
system. Use the following as the stopping criterion:

Consider the nonlinear system below for unknowns x1, x2.

Explanation / Answer

save the first program as ,, main and second as Newton

first

function [solution] = Newton(MyFunc,Jacobian,Guess,tol)
% solves the non-linear vector equation F(x)=0
% set using Newton Raphson iteration
% INPUTS;
% Myfunc=Handle to the function which returns the vector
F(x)
% Jacobian=Handle to the function which returns the Jacobian
Matrix
% Guess = Initial Guess (a vector)
% tol = Tolerance
%
% OUTPUTS
% solution = The solution to F(x) = 0
x = Guess;

%set the error 2*tol to make sure the loop runs at least
once
error = 2*tol
while error > tol

%calculate the function values at the current
iteration
F = feval(MyFunc,x);

%calculate the jacobian matrix
J = feval(Jacobian,x);

%calculate the update (solve the linear system)
dx = J(-F);

%update the x value
x = x+dx;

%calculate the error
F = feval(MyFunc,x);
error = max(abs(F));

end %while loop
solution = x;

return

2nd program

function [solution] = Newton(MyFunc,Jacobian,Guess,tol)
% solves the non-linear vector equation F(x)=0
% set using Newton Raphson iteration
% INPUTS;
% Myfunc=Handle to the function which returns the vector
F(x)
% Jacobian=Handle to the function which returns the Jacobian
Matrix
% Guess = Initial Guess (a vector)
% tol = Tolerance
%
% OUTPUTS
% solution = The solution to F(x) = 0
x = Guess;

%set the error 2*tol to make sure the loop runs at least
once
error = 2*tol
while error > tol

%calculate the function values at the current
iteration
F = feval(MyFunc,x);

%calculate the jacobian matrix
J = feval(Jacobian,x);

%calculate the update (solve the linear system)
dx = J(-F);

%update the x value
x = x+dx;

%calculate the error
F = feval(MyFunc,x);
error = max(abs(F));

end %while loop
solution = x;

return