MATLAB Learning Matlab: Plotting Error in f'(1) Using Loglog Scale & Subplot

  • Thread starter Thread starter bobsmiters
  • Start date Start date
  • Tags Tags
    Matlab Scale
AI Thread Summary
The discussion focuses on using Matlab to approximate the derivative of the function f(x) = sin x at x = 1 using forward and central divided differences. The correct formula for the central difference is clarified, emphasizing the need to compute the error as a function of h across a specified range. Participants suggest setting up a vector for h using logspace and highlight the importance of using element-wise division in Matlab. The main challenge lies in computing the numerical differentials accurately. Overall, the conversation aims to provide guidance on implementing these calculations in Matlab effectively.
bobsmiters
Messages
12
Reaction score
0
I am taking a Math/Cmpt class and I am useless with this program that we need to use Matlab.

Q: The derivative of a function f(x) can be approximated at the point x=x(sub)0 by a forward divided difference

f'(x(sub)0) ≈ [f(x(sub)0 + h) - f(x(sub)0)] / h

or by a central divided difference

f'(x(sub)0) ≈ [f(x(sub)0 + h) - f(x(sub)0)] / 2h

For the function f(x)= sin x, plot the error as a function of h when these divided differences are used to approximate f'(1) = cos1 for h = 10^-1, 10^-2, 10^-3, ..., 10^-16. Use loglog scale and display your results in one window using subplot.

As far and the loglog and subplot is concerned I understand what to do... it is the rest of the question that is causing a lot of difficulties. if there are any pointers on where to begin it would be much appreciated.
 
Physics news on Phys.org
You question is still a bit broad, but I'll bite.

Firstly I don't think the central difference is defined correctly... it should be
f'(x_0) = [f(x_0+h/2) - f(x_0-h/2)] / h

You're being asked to compute the convergence of the error of forward and central differentials, by comparing to the best answer you have available.
Fortunately your function has a well known analytic differential, otherwise you would have to compare to your best numerical estimate (you will we see how this works when you do the analytical comparison).

That aside, it seems that the main part of your question is how to compute the numerical differentials. I'm not going to tell you the answer, but you need to set up a vector for h (perhaps using logspace) and calculate the formulas as written. Just remember to use ./h not /h
 
Back
Top