Question: please use this MatLab code for (d), fixing the code where there are question marks clc; clear; close all; % Finite Difference Approximation of slope

please use this MatLab code for (d), fixing the code where there are question marks
clc; clear; close all;
% Finite Difference Approximation of slope (High Accuracy formulas)
x=2;
fx=exp(-2*x)-x;
dfdx2=-2*exp(-2*x)-1;
%approximation
dx=0.5: ????? :.01;
for i=1:length(dx)
%x-values
xp(i)=x+dx(i);
x2p(i)= ?????;
xn(i)=x-dx(i);
x2n(i)= ?????;
%f(x)-values
fp(i)=exp(-2*xp(i))-xp(i);
f2p(i)= ?????;
fn(i)=exp(-2*xn(i))-xn(i);
f2n(i)= ?????;
%Finite Diff. Approximations
Cdfdx(i)= (-f2p(i)+8*fp(i)-8*fn(i)+f2n(i))/(12*dx(i)); ????
Fdfdx(i)= ?????;
Bdfdx(i)= ?????;
end
dx0=0;
plot(dx,Fdfdx,'--',dx,Bdfdx,'-.',dx,Cdfdx,'-',dx0,dfdx2,'*')
grid
title('Forward, Backward, and Centered Finite Difference approximation')
xlabel('Delta x')
ylabel('df/dx')
gtext('Centered'); gtext('Forward'); gtext('Backward')
The objective of this problem is to compare second-order accurate forward, backward, and centered finite-difference approximations of the first derivative of a function to the actual value of the derivative. This will be done for f(x)=e2xx (a) Use calculus to determine the correct value of the derivative at x=2. (b) To evaluate the centered finite-difference approximations, start with x=0.5. Thus, for the first evaluation, the x values for the centered difference approximation will be x=20.5 or x=1.5 and 2.5. Then, decrease in increments of 0.01 down to a minimum value of x=0.01. (c) Repeat part (b) for the second-order forward and backward differences. (Note that these can be done at the same time that the centered difference is computed in the loop.) (d) Plot the results of (b) and (c) versus x. Include the exact result on the plot for comparison
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
