Finding Optimal h for Truncation Error O(h2)

  • Context: Comp Sci 
  • Thread starter Thread starter ver_mathstats
  • Start date Start date
  • Tags Tags
    Error
Click For Summary
SUMMARY

The discussion centers on determining the optimal step size, h, for minimizing truncation error in the central difference approximation of the derivative of the function f(x) = sin(x)e^cos(x). Participants clarify that the truncation error is of order O(h²) and emphasize the importance of considering both truncation and rounding errors when evaluating on a computer. The correct approach involves using Taylor expansions for f(x + h) and f(x - h) to derive the error expression, and the optimal value of h is suggested to be related to machine epsilon, although this is context-dependent based on the function and point of evaluation.

PREREQUISITES
  • Understanding of Taylor series expansions
  • Familiarity with numerical differentiation techniques
  • Knowledge of truncation and rounding errors in numerical analysis
  • Experience with MATLAB for plotting functions
NEXT STEPS
  • Study Taylor series expansions for functions to derive truncation errors
  • Learn about numerical differentiation methods, specifically central difference formulas
  • Research the implications of machine epsilon on numerical precision
  • Explore MATLAB plotting techniques for visualizing error analysis
USEFUL FOR

Students and professionals in numerical analysis, mathematicians, and software developers working on numerical methods and error analysis in computational mathematics.

ver_mathstats
Messages
258
Reaction score
21
Homework Statement
Part (A) Find the truncation error of the approximation f'(x) by (f(x+h)-f(x-h))/2h.

Part (B) When evaluated on a computer, for what value of h the error of this approximation is the smallest?

Part (C) For the function f(x)=sinxe^cosx plot the error |(f'(x)-(f(x+h)-f(x-h))/2h)| versus h fot the appropriate values of h.
Relevant Equations
f'(x) by (f(x+h)-f(x-h))/2h
Part (A) I have already determined the truncation error of order O(h2).

Part (B) I'm struggling with how to approach this part. I do not really understand where to begin to figure out what value of h is the approximation the smallest. Is this looking for a particular range? Or are we isolating for h? And I do understand that h is the step count, just not entirely positive on how to find the ideal value of h.

Part (C) For plotting the function I am using matlab, I just want to know if I am understanding correctly, I have to substitute my function into the error? And that is what I am plotting?

Thank you.
 
Last edited:
Physics news on Phys.org
Oh dear, I am not sure the question is worded properly: where is it from? Taking it at face value:

ver_mathstats said:
Part (A) Find the truncation error of the approximation f'(x) by (f(x+h)-f(x-h))/2h.
Assuming the question really does mean truncation error and not roundoff (or rounding) error then you need to write the Taylor expansions for ## f(x + h) ## and ## f(x - h) ## (hint: you can end the expansions at ## \dots + \mathcal{O}(h^4) ##), plug them into ## \frac{f(x+h)-f(x-h)}{2h} ## and gather terms to find an expression in the form ## \frac{ah^n}{b}\frac{d^kf}{dx}(x) + \mathcal{O}(h^m) ##. This material is usually introduced by working through a similar exercise for the approximation ## D_{f'} = \frac{f(x+h)-f(x)}{h} ## in class, did you do this?

ver_mathstats said:
Part (B) When evaluated on a computer, for what value of h the error of this approximation is the smallest?
This is where the question starts to worry me: when you introduce "evaluation on a computer" you are looking at the trade-off between truncation error and rounding error, and this is not trivial. Did you consider this for ## D_{f'} = \frac{f(x+h)-f(x)}{h} ## in class? On the other hand if we are still only talking about truncation error then the fact that we are "evaluating on a computer" is irrelevant and there is no value of ## h ## for which the approximation is smallest.

ver_mathstats said:
Part (C) For the function f(x)=sinxe^cosx plot the error |(f'(x)-(f(x+h)-f(x-h))/2h)| versus h fot the appropriate values of h.
So now I'm really confused: are we talking about total error or just truncation error? Both of these depend on ## x ## as well as ## h ## so how do we deal with that? What are the "appropriate values of ## h ##"? Or is the question actually "For the function f(x)=sinxe^cosx plot the error |(f'(x)-(f(x+h)-f(x-h))/2h)| versus h x fot for the appropriate values of h value of h given in Part (B)?"
 
Last edited:
  • Like
Likes   Reactions: DrClaude
pbuk said:
Oh dear, I am not sure the question is worded properly: where is it from? Taking it at face value:Assuming the question really does mean truncation error and not roundoff (or rounding) error then you need to write the Taylor expansions for ## f(x + h) ## and ## f(x - h) ## (hint: you can end the expansions at ## \dots + \mathcal{O}(h^4) ##), plug them into ## \frac{f(x+h)-f(x-h)}{2h} ## and gather terms to find an expression in the form ## \frac{ah^n}{b}\frac{d^kf}{dx}(x) + \mathcal{O}(h^m) ##. This material is usually introduced by working through a similar exercise for the approximation ## D_{f'} = \frac{f(x+h)-f(x)}{h} ## in class, did you do this?This is where the question starts to worry me: when you introduce "evaluation on a computer" you are looking at the trade-off between truncation error and rounding error, and this is not trivial. Did you consider this for ## D_{f'} = \frac{f(x+h)-f(x)}{h} ## in class? On the other hand if we are still only talking about truncation error then the fact that we are "evaluating on a computer" is irrelevant and there is no value of ## h ## for which the approximation is smallest.So now I'm really confused: are we talking about total error or just truncation error? Both of these depend on ## x ## as well as ## h ## so how do we deal with that? What are the "appropriate values of ## h ##"? Or is the question actually "For the function f(x)=sinxe^cosx plot the error |(f'(x)-(f(x+h)-f(x-h))/2h)| versus h x fot for the appropriate values of h value of h given in Part (B)?"
I ended up getting the answers for part a and for part b, my issue is part c, my teacher said to choose my own values of h and I believe I am supposed to be graphing the central difference formula given for f(x)=sinxe^cosx.
 
Sorry I didn't spot that this is a different function but I still don't understand what the teacher is saying. I would tackle this by writing some code to calculate and plot the difference between the exact value and the central difference formula over a range of values of x for a few values of h: experiment to see what works well.
 
ver_mathstats said:
I ended up getting the answers for part a and for part b, my issue is part c, my teacher said to choose my own values of h and I believe I am supposed to be graphing the central difference formula given for f(x)=sinxe^cosx.
For part b, is the answer when h equals machine epsilon?
 
xDocMath said:
For part b, is the answer when h equals machine epsilon?
Yes it is.
 
ver_mathstats said:
Yes it is.
That may be the answer your lecturer expects, but it is not the right answer.

For instance assume ## \epsilon_M = 2^{-53} ##. If we want to approximate ## f'(x) ## at ## x = 10^{17} ## using ## h =\epsilon_M ## then ## 10^{17} \pm 2^{-53} ## both evaluate to ## 10^{17} ##.

The value of ## h ## for maximising precision depends on both ## x ## and ## f(x) ##.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
2
Views
1K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
15
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K