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 derivativef x varargin
DERIVATIVE computes numerical derivative using chosen method:
fwd diff, bkwd diff, centered diff,
and Richardson extrapolation.
Syntax: approx derivativef x 'Method', 'forward', 'StepSize', e
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
zero derivative@sin pi 'Method', 'richardson'
zero derivative@x expx 'stepsize', e
data structure
data.f f;
data.x x;
data.h sqrteps;
method @forwarddiff;
unknown order of arguments
stepSizeSet false;
varargin has some optional arguments!
if nargin
loop through the field names odd indexes
for k::lengthvarargin
compare the field name to constants
switchlowervarargink
case 'method'
use the even following element of varargin for the
name of the method
switchlowervarargink
case backwardb
method @forwarddiff;
if ~stepSizeSet
data.h sqrteps;
end
if data.h
data.h data.h;
end
case centered 'center', 'central', c
method @centered;
if ~stepSizeSet
data.h eps;
end
case richardsonr
method @richardson;
if ~stepSizeSet
data.h eps;
end
end
case charstepsize
use the next value in varargin for the floating point
step size h
data.h varargink;
stepSizeSet true;
end
end
end
approx methoddata;
end
function approx forwarddiffdata
derivate computes a numerical derivative using fwd diff, or bkwd if data.h
Syntax: approx fowarddiffdata
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 forwarddiffdata
fxfxhfxh
if h then the sign of the denominator will reverse the difference
in the numerator.
approx datafdatax data.h data.fdataxdatah;
end
function approx centereddata
CENTERED computes numerical derivative using centered diff
Syntax: approx centereddata
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 centereddata
approx ; replace this
end
function approx richardsondata
RICHARDSONcomputes numerical derivative using Richardson Extrapolation
Syntax: approx richardsondata
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 richardsondata
approx ; replace this
end
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
