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

  • Context: MATLAB 
  • Thread starter Thread starter bobsmiters
  • Start date Start date
  • Tags Tags
    Matlab Scale
Click For Summary
SUMMARY

The discussion focuses on approximating the derivative of the function f(x) = sin(x) at x = 1 using both forward and central divided differences in MATLAB. The correct formula for the central difference is clarified as f'(x_0) = [f(x_0 + h/2) - f(x_0 - h/2)] / h. Participants emphasize the importance of computing the convergence of the error by comparing numerical results to the known analytic derivative, cos(1). Key MATLAB functions such as logspace are recommended for generating the vector of h values needed for the calculations.

PREREQUISITES
  • Understanding of numerical differentiation techniques, specifically forward and central divided differences.
  • Familiarity with MATLAB programming, particularly plotting functions and using subplots.
  • Knowledge of error analysis in numerical methods.
  • Basic understanding of trigonometric functions and their derivatives.
NEXT STEPS
  • Learn how to implement MATLAB's logspace function for generating logarithmically spaced vectors.
  • Research error analysis techniques in numerical differentiation to understand convergence behavior.
  • Explore MATLAB's plotting capabilities, focusing on loglog scale and subplot functionalities.
  • Study the theoretical foundations of numerical differentiation to reinforce understanding of forward and central differences.
USEFUL FOR

Students in mathematics or computer science courses, MATLAB users seeking to improve their numerical analysis skills, and anyone interested in understanding the convergence of numerical differentiation methods.

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
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K