Please use MatLab and comment code. df(x) Problem 3: two-point central differenc
ID: 3716290 • Letter: P
Question
Please use MatLab and comment code.
df(x) Problem 3: two-point central difference formula: df (x) foh0-h) ?? 2h where h is a small number relative to xo. Write a user-defined function function that calculates the derivative of a math function f(x) by using the two-point central difference formula. For the user-defined function name, use dfdx= Funder (Func, X0), where Funcis a name for the function that is passed into Funder, and x0 is the point where the derivative is calculated. Use h-xo/100 in the two-point central difference formula. Write a script to use the user-defined function Funder to calculate the following: (a) The derivative of f(x)x3e2* at xo 0.6 (b) The derivative off(x) at x,-2.5 The script will also compare the answer obtained from Funder with the analytical solution (use format long) using the following definition of the relative error AnswerFromFunder AnalyticalDerivativel AnalyticalDerivative RelError-Explanation / Answer
PLEASE CREATE FILE test_funder.m AND PASTE BELOW CODE
close all
clear all
clc
%a) f = x^3e^2x
f = @(x) x^3 * exp(2 * x)
x0 = 0.6;
df_by_dx = Funder(f,x0)
%b) f = 3^x/x^2
f1 = @(x) 3^x / x^2
x0 = 2.5;
df1_by_dx = Funder(f1,x0)
PLEASE CREATE FILE Funder.m AND PASTE BELOW CODE
function dfdx = Funder(Fun,x0)
%find h with given formula as h = x0/100;
h = x0 / 100;
% x0 + h
x0_plus_h = x0 + h;
%x0 - h
x0_minus_h = x0 - h;
%f(x0 + h)
f_a = feval(Fun,x0_plus_h);
%f(x0 - h)
f_b = feval(Fun,x0_minus_h);
%df(x)/dx = f(x0 + h) - f(x0 - h) / 2h
dfdx = (f_a - f_b) / (2 * h);
PLEASE REFER BELOW OUTPUT
f =
function_handle with value:
@(x)x^3*exp(2*x)
df_by_dx =
5.0209
f1 =
function_handle with value:
@(x)3^x/x^2
df1_by_dx =
0.7448