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

function [df] = numericalDerivative(x,f,type); n = length(x); h = x(2)- x(1); if

ID: 3736544 • Letter: F

Question

function [df] = numericalDerivative(x,f,type);

n = length(x);
h = x(2)- x(1);

if strcmp(type, 'FL')                     % Forward Difference
    for i = 1:n-1
        df(i) = (f(i+1) - f(i))/h;           % Forward Difference equation
    end
    df(n) = (f(n) - f(n-1))/h;             %backward difference at last point

elseif strcmp(type,'BL')              % Backward Difference

    for i = 2:n
        df(i) = (f(i) - f(i-1))/h;           % Backward Difference equation
    end
    df(1) = (f(2) - f(1))/h;               % Forward difference at first point

elseif strcmp(type,'CL')               % Centered difference

    for i = 2:n-1
        df(i) = (f(i+1) - f(i-1))/2*h;     % Centered difference equation
....
elseif strcmp(type,'FM')              % Higher Accuracy Forward Difference

....
elseif strcmp(type,'BM')              % Higher Accuracy Backward DIfference

.....

elseif strcmp(type,'CM')               % Higher accuracy Centered difference
.....

I need code to fill in the blanks for CL, FM, BM and CM. It should be very similar to the rest of the code.

Thanks, Like will be given!

Given this discrete approximation to f(sin(r you now seek to compute the derivative of f(r) at each of the n sample points. To accomplish this you will write one function and one main program:

Explanation / Answer

%% Fun = @(x) exp(-x).*sin(3*x); dFun = @(x) -exp(-x).*sin(3*x)+ 3*exp(-x).*cos(3*x); %% x=linspace(0,4,101); F=Fun(x); h=x(2)-x(1); xCentral=x(2:end-1); dFCenteral=(F(3:end)-F(1:end-2))/(2*h); xForward=x(1:end-1); dFForward=(F(2:end)-F(1:end-1))/h; xBackward=x(2:end); dFBackward=(F(2:end)-F(1:end-1))/h; %% plot(x,dFun(x)); hold on plot(xCentral,dFCenteral,'r') plot(xForward,dFForward,'k'); plot(xBackward,dFBackward,'g'); legend('Analytic','Central','Forward','Backward')