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

Write a Matlab function y-fprime (E, a, h, c, b) that will approximate the deriv

ID: 3184157 • Letter: W

Question

Write a Matlab function y-fprime (E, a, h, c, b) that will approximate the derivative f(a) with the following formula: f'(a) For example, EprimeE,a, h, ,1,,0,11) would give you the 2-point forward-difference formula: f(a +h)-f(a) f(a) and fprime (E, a, h, 1,-,,-1,2]) would give you the 3-point centered-difference formula: f'(a) 2h Test your function with the following values: > y fprime ( (x) log (x),1,0.01,1,-1,0, 0.995033085316809 >> y-fprime ( (x) log(x),1,0.01,11,-11.11,-1.21) y- 1.000033335333477 a) What are the actual errors and the eror bounds for the above approsximations? Remember that in Matlab log (x) represents ln b) Use your function to determine the order (O(h), O(h), etc)of the following approximation P(a) Hint Let f(r) In(r) and compare the actual errors for the approximation of (1) when h takes on the following values: h = 10-i, 10-2, , .. , lo-s

Explanation / Answer

Please copy this code into a m-file and name it as fprime.m

function y = fprime(f,a,h,c,b)

f_dash = 0;

for i = 1:length(b) -1

f_dash = f_dash + c(i)*f(a+b(i)*h);

end

f_dash = f_dash/(b(i+1)*h);

fprintf('for h = %f, f_dash = %f ',h,vpa(f_dash));

fprintf('actual error = %f ',vpa(abs(f_dash-a)));

end

a. The code calculates the actual error and the error bounds

b. by changing h from 0.01 iteratively, we find that the order as O(h^2)

Please run this script to support this inference:

h = [1e-2 1e-3 1e-4 1e-5 1e-6 1e-7];

for j = 1:length(h)

fprime(@(x) log(x),1,h(j),[1,-1],[1,0,1])

end

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