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

Write a MATLAB function (IncNR) combining the capabilities of both the Increment

ID: 3805340 • Letter: W

Question

Write a MATLAB function (IncNR) combining the capabilities of both the Incremental Search Method and the Newton-Raphson Method to find all the roots in a given function [xR, err, n] = IncNR(AF, xb, ed) AF = anonymous function xb = initial guess x bracket = [xL xU], where xL = lower x and xU = upper x ed = desired error Outputs: xR = vector of roots err = vector of errors corresponding to the roots n = vector of the number of iterations Suggested Steps: Use the Incremental Search Method to identify the number of roots and the brackets Use the Newton-Raphson Method in each of the brackets starting at either the lower domain or the upper domain

Explanation / Answer

Please find the code below for Bisection Method - a Incremental Search method and Newton Method.

I am not sure from the project description whether you want to combine both the methods or not.

Bisection Method:

bisec.m

function [ r ] = bisection( f, a, b, N, eps_step, eps_abs )
% Check that that neither end-point is a root
% and if f(a) and f(b) have the same sign, throw an exception.

if ( f(a) == 0 )
   r = a;
   return;
elseif ( f(b) == 0 )
   r = b;
   return;
elseif ( f(a) * f(b) > 0 )
error( 'f(a) and f(b) do not have opposite signs' );
end

% We will iterate N times and if a root was not
% found after N iterations, an exception will be thrown.

for k = 1:N
% Find the mid-point
c = (a + b)/2;

% Check if we found a root or whether or not
% we should continue with:
% [a, c] if f(a) and f(c) have opposite signs, or
% [c, b] if f(c) and f(b) have opposite signs.

if ( f(c) == 0 )
r = c;
return;
elseif ( f(c)*f(a) < 0 )
b = c;
else
a = c;
end

% If |b - a| < eps_step, check whether or not
% |f(a)| < |f(b)| and |f(a)| < eps_abs and return 'a', or
% |f(b)| < eps_abs and return 'b'.

if ( b - a < eps_step )
if ( abs( f(a) ) < abs( f(b) ) && abs( f(a) ) < eps_abs )
r = a;
return;
elseif ( abs( f(b) ) < eps_abs )
r = b;
return;
end
end
end

error( 'the method did not converge' );
end

Newton Method:

function [x,iter]=newton(x0,f,fp)
% newton-raphson algorithm
N = 100; eps = 1.e-5; % define max. no. iterations and error
maxval = 10000.0; % define value for divergence
xx = x0;
while (N>0)
   xn = xx-f(xx)/fp(xx);
   if abs(f(xn))<eps
   x=xn;iter=100-N;
   return;
   end;
   if abs(f(xx))>maxval
   disp(['iterations = ',num2str(iter)]);
   error('Solution diverges');
   break;
   end;
   N = N - 1;
   xx = xn;
end;
error('No convergence');
break;
% end function

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