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

In Matlab: Exercise 1 Write two functions chord method.m and Newton method.m imp

ID: 2267512 • Letter: I

Question

In Matlab:

Exercise 1 Write two functions chord method.m and Newton method.m implementing, respectively, the chord and the Newton methods to find the zeros of nonlinear (scalar) equations. The functions should be of the following form function [z0,iter,res, his] = chord method (fun,a,b,tol, Nmax) function [z0,iter ,res, his] = Newton_method (fun, dfun, x0, tol, Nmax) Inputs: fun: function handle representing f(x) a, b: interval [a, b] in which we believe there is a zero tol: maximum tolerance allowed for the increment |x(k+1) — xlk| Nmax: maximum number of iterations allowed dfun: function handle representing df (x)/dx (Newton method) xo: initial guess for the zero (Newton method) Outputs: z0: approximation of the zero 20 iter: number of iterations to obtain 20 res: residual at z0 (i.e., |f(zo)) his: vector collecting all the elements of the sequence {x{k}k=0,1,.. (convergence history) Both functions should return the numerical approximation of the zero when the increment at iteration k+1 is such that |x(k+1) –rk)|

Explanation / Answer

The below functions are written for a single variable implementing only the basic function for evalutaing the desired solution.

chord_method.m

function [ z0,iter,res,his ] = chord_method( fun,a,b,tol,Nmax )

% This function implements the simple mathematical equation of the chord Method for a single variable.

%define symbol for evaluating differential of fun

syms x;

%Defining the initial variables

xk = a;

xk1 = xk;
his = xk;
df = eval(['@(x)' char(diff(fun(x)))]);
dfun = df(xk);

%looping from 1 to Nmax to keep a check on the number of iterations
for k=1:Nmax
      %evaluate next x(k+1)
xk1 = xk - (fun(xk))/(dfun(xk));
  
if(xk1 > b)
break;
end
  
if (abs(xk1-xk) < tol)
xk = xk1;
his = [his xk];
break;
end
xk = xk1;
his = [his xk];
end
      %return values for function output
z0 = abs(xk1);
iter = k;
res = fun(xk);

end

Newton_method.m

function [ z0,iter,res,his ] = Newton_method( fun,dfun,x0,tol,Nmax )
% This function implements the simple mathematical equation of the Newton Method for a single variable.

%Defining the initial variables

xk = x0;
xk1 = xk;
his = xk;

%looping from 1 to Nmax to keep a check on the number of iterations

for k=1:Nmax
  

%evaluate next x(k+1)
xk1 = xk - (fun(xk))/(dfun(xk));

%check if solution found
if (abs(xk1-xk) < tol)
xk = xk1;
his = [his xk];
break;
end
xk = xk1;
his = [his xk];
end
  

  %return values for function output
z0 = xk1;
iter = k;
res = fun(xk);

end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote