Question: Use given derivative.m MATLAB code ( listed below ) to add both the centered difference and Richardson extrapolation methods of numerical differentiation. They need to

Use given derivative.m MATLAB code (listed below) to add both the centered difference and Richardson extrapolation methods of numerical differentiation. They need to be subfunctions like forwarddiff. Backward difference is already implemented (forwarddiff with a negative h). Use structures to implement the methods, so refer to forwarddiff for an example of what the formulas for centered difference and Richardson extrapolation should look like.
derivative.m:
function approx = derivative(f, x, varargin)
%DERIVATIVE computes numerical derivative using chosen method:
% fwd diff, bkwd diff, centered diff,
% and Richardson extrapolation.
% Syntax: approx = derivative(f, x, 'Method', 'forward', 'StepSize', 1e-4)
% Inputs:
% f function handle
% x array of values to compute the numerical derivative
%---Optional Arguments---
% field name name of target parameter 'Method', 'StepSize'
% field parameter value 'forward', 'backward', 'centered',
% 'Richardson', for field name 'Method'. Default method
% is forward difference.
%
% floating point step size h for field name 'StepSize'. The
% default step size depends on the difference method.
% Outputs:
% approx the numerical approx. to the derivative of function
% Example:
% zero = derivative(@cos, pi/3)
% zero = derivative(@sin, pi/2, 'Method', 'richardson')
% zero = derivative(@(x) exp(x)-1,-1, 'stepsize', 1e-6)
% data structure
data.f = f;
data.x = x;
data.h = sqrt(eps);
method = @forwarddiff;
% unknown order of arguments
stepSizeSet = false;
% varargin has some optional arguments!
if nargin >2
% loop through the field names (odd indexes)
for k=1:2:length(varargin)
% compare the field name to constants
switch(lower(varargin{k}))
case 'method'
% use the even (following) element of varargin for the
% name of the method
switch(lower(varargin{k+1}))
case {'backward','b'}
method = @forwarddiff;
if ~stepSizeSet
data.h =-sqrt(eps);
end
if data.h >0
data.h =-data.h;
end
case {'centered', 'center', 'central', 'c'}
method = @centered;
if ~stepSizeSet
data.h = eps^(1/3);
end
case {'richardson','r'}
method = @richardson;
if ~stepSizeSet
data.h = eps^(1/5);
end
end
case char('stepsize')
% use the next value in varargin for the floating point
% step size h
data.h = varargin{k+1};
stepSizeSet = true;
end
end
end
approx = method(data);
end
function approx = forwarddiff(data)
%derivate computes a numerical derivative using fwd diff, or bkwd if data.h
%<0.
% Syntax: approx = fowarddiff(data)
% Inputs:
% data structure with following fields:
% f function handle
% x array of values to compute the numerical derivative
% h step size for difference methods
% Outputs:
% approx the numerical approximation to the derivative of function
% Example:
% zero = forwarddiff(data)
% f'(x)=(f(x+h)-f(x))/h
% if h<0, then the sign of the denominator will reverse the difference
% in the numerator.
approx =(data.f(data.x + data.h)- data.f(data.x))./(data.h);
end
function approx = centered(data)
%CENTERED computes numerical derivative using centered diff
% Syntax: approx = centered(data)
% Inputs:
% data structure with the following fields:
% f function handle
% x array of values to compute the numerical derivative
% h step size for difference methods
% Outputs:
% approx the numerical approximation to the derivative of function
% Example:
% zero = centered(data)
approx =0; % replace this
end
function approx = richardson(data)
%RICHARDSONcomputes numerical derivative using Richardson Extrapolation
% Syntax: approx = richardson(data)
% Inputs:
% data structure with the following fields:
% f function handle
% x array of values to compute the numerical derivative
% h step size for difference methods
% Outputs:
% approx the numerical approximation to the derivative of function
% Example:
% zero = richardson(data)
approx =0; % replace this
end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!