We will now explore different numerical differentiation methods. Here we will lo
ID: 3679450 • Letter: W
Question
We will now explore different numerical differentiation methods. Here we will look at the function f(x) = ex , and we want to estimate the derivative at x = 0.5 to work out what are the differences between the proposed methods. The two methods we will use are:
• Backward difference: fx(x = xi) fifi1 /(x )
• Central difference: fx(x = xi) fi+1fi1 /(2x)
where x is the grid spacing and fi = f(xi). In your code you will need to work out the values of fi which you need to estimate the derivative, e.g. for x = 0.1 you need f(0.5) and f(0.4) for the Backward difference formula. Create a simple code to print the two estimated gradients out, along with the % error and actual error. Start with x = 0.1, then also compute the error for x = 0.05, 0.025, 0.0125. Tabulate the results and see if you can spot a trend for the two methods.
Using the Taylor Series, can you show why you have found what you have found?
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);
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(xForward,dFForward,'k');
plot(xBackward,dFBackward,'g');
legend('Analytic','Forward','Backward')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.