Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. (20 points) Consider approximating derivatives of f(x) on la, o with finite d

ID: 3730944 • Letter: 2

Question

2. (20 points) Consider approximating derivatives of f(x) on la, o with finite difference formulas. First, divide the function domain la, b] into n equi-distant sub-intervals: where x's are called nodes. Define the grid size h as h = (b-a)/n-zi-Z-1. Next, denote the column vector as function valueWrite a MATLAB function that approximates f'(x) at (xi-o with second-order accuracy . fp fd.fp(a,b,f) f is the column vector of function values. . fp is the column vector of the approximations to the first derivative. Use the three-point centered-difference formula for the interior points. ·Use FDF2 and BDF2 formulas for the two end points.

Explanation / Answer

function f_p=fd_fp(a,b,g)
n=10; % number of intervals
h=(b-a)/n; % step length
x=a:h:b; % x values
for i=1:n
    f(i)=g(x(i));
end
f_cen(1)=(-f(3)+4*f(2)-3*f(1))/(2*h) % FD2

for i=2:n-1
    f_cen(i)=(f(i+1)-f(i-1))/(2*h); % central
end
f_cen(n)=(f(n-2)-4*f(n-1)+3*f(n))/(2*h); % BD2

f_p=f_cen';
end

%%%%% Eample %%%%

function dummy=derivt()
clc;
clear all;
a=1;
b=2
g=@(x)exp(x);
f_p=fd_fp(a,b,g)
function f_p=fd_fp(a,b,g)
n=10; % number of intervals
h=(b-a)/n; % step length
x=a:h:b; % x values
for i=1:n
    f(i)=g(x(i));
end
f_cen(1)=(-f(3)+4*f(2)-3*f(1))/(2*h) % FD2

for i=2:n-1
    f_cen(i)=(f(i+1)-f(i-1))/(2*h); % central
end
f_cen(n)=(f(n-2)-4*f(n-1)+3*f(n))/(2*h); % BD2

f_p=f_cen';
end
end

%%% Solution %%

f_p =

   2.708508438360244
   3.009175471387509
   3.325653218364055
   3.675415220540637
   4.061962013594100
   4.489162287752202
   4.961291606945673
   5.483075200089158
   6.059735252760348
   6.665204304566075

>>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote