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

1.7. (a) Write a program to compute an approx- imate value for the derivative of

ID: 3747243 • Letter: 1

Question

1.7. (a) Write a program to compute an approx- imate value for the derivative of a function using the finite-difference formula Test your program using the function tan(x) for x = 1, Determine the error by comparing with the square of the built-in function sec(x). Plot the magnitude of the error as a function of h, for h = 10-K, k = 0, . . . , 16, You should use log scale for h and for the magnitude of the error. Is there a minimum value for the magnitude of the error? How does the corresponding value for h compare with the rule of thumb h ~ Vmach derived in Example 1.3? (b) Repeat the exercise using the centered differ- ence approximation

Explanation / Answer

Answer :

MATLAB CODE:

a)

f = @(x) sin(x);

x = 1;

k = 1:50;

h = 2.^(-k);

df = (f(x+h) - f(x))./h;

err = abs(df - cos(x));

loglog(h,err)

grid on

xlabel('h');

ylabel('error');

for i=1:50

    if(err(i) == min(err))

        H = h(i);

    end

end

ApproxH = sqrt(abs(3*(4/3-1)-1))*abs(x);

fprintf(' h corresponding to minumum error %e rule of thum h = %e ',H,ApproxH)

b)

f = @(x) sin(x);

x = 1;

k = 1:50;

h = 2.^(-k);

df = (f(x+h) - f(x-h))./(2.*h);

err = abs(df - cos(x));

loglog(h,err)

grid on

xlabel('h');

ylabel('error');

for i=1:50

    if(err(i) == min(err))

        H = h(i);

    end

end

ApproxH = sqrt(abs(3*(4/3-1)-1))*abs(x);

fprintf(' h corresponding to minumum error %e rule of thum h = %e ',H,ApproxH);

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