I need to write a Matlab code for the following problem. 3. Find the first deriv
ID: 3281590 • Letter: I
Question
I need to write a Matlab code for the following problem.
3. Find the first derivative of a functionf(x)-e*- t (a) Use calculus to determine the correct value of the derivative at x = 2. If h = 0.25, (b) Evaluate the second-order centered finite-difference approximation Evalusic the secononer foce approximation (d) Evaluate the second-order backward difference approximation. (e) Create a MATLAB function program, which gives output up to second order centered n (order of approximation, 1 or 2), xo, and h. ). The input arguments are f,Explanation / Answer
clc;
clear all;
format short
f=@(x)exp(-2*x)-exp(2*x)+x; %function
df=@(x)-2*exp(-2*x)-2*exp(2*x)+1; % derivative of function
x=2;
h=0.25;
disp('Centerd difference')
F_cen=(f(x+h)-f(x-h))/(2*h)
disp('forward difference')
F_ofw=(4*f(x+h)-3*f(x)-f(x+2*h))/(2*h)
disp('backward difference')
F_obw=(-4*f(x-h)+3*f(x)+f(x-2*h))/(2*h)
disp('second derivative second order difference')
F_scen=(f(x+h)-2*f(x)+f(x-h))/(h^2)
Centerd difference
F_cen =
-112.8415
forward difference
F_ofw =
-94.7563
backward difference
F_obw =
-101.8685
second derivative second order difference
F_scen =
-222.9057
>>
function F_scen=finite(f,x,h)
F_scen=(f(x+h)-2*f(x)+f(x-h))/(h^2)
end