In MATLAB create a function of the form where df is the approximate value of the
ID: 670502 • Letter: I
Question
In MATLAB create a function of the form where df is the approximate value of the derivative df/dx, user fun is the function handle of the user's function handle of the user's function of the form y = f(x), method is character string that determines the finite difference method is the value of the input, and h is the value of the step size. Using your function from (a), compute the approximate values of the derivative df/dx for each of the following functions using forward, backward, and central differences with the step sizes h = 10-6, 10-8, and 10-16 Using the actual value of each derivative computed in part 1, show the absolute error of the approximate value and describe how the step size effects the error?Explanation / Answer
1. Solution of Part A:
Steps : Copy the below code in a new script file and save it as any file name. This file will work as function for your problem.
Code:
function [df] = FiniteDifference(usrfun, method, x, h);
% Matlab implementation of Finite Difference Function
%
%
% input: usrfun : User Function
% method : finite difference method
% x : value of x
% h : value of step size
%
%
% output: df : Finite Difference
%
%
if (strcmpi(method, forward) == 1)
[y1,y2] = feval(usrfun, x, x+h);
df = (y2-y1)/h;
end
if (strcmpi(method, central) == 1)
[y1,y2] = feval(usrfun, x-h, x);
df = (y2-y1)/h;
end
if (strcmpi(method, backward) == 1)
[y1,y2] = feval(usrfun, x-(h/2), x+(h/2));
df = (y2-y1)/h;
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.