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

Discussion Overview

The discussion revolves around determining the optimal value of h for minimizing truncation error in the context of numerical differentiation using the central difference formula. Participants explore the implications of truncation error versus rounding error, the formulation of Taylor expansions, and the appropriate values of h for plotting errors in MATLAB.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants have calculated the truncation error of order O(h²) but express uncertainty about how to find the optimal value of h.
  • One participant suggests using Taylor expansions for f(x+h) and f(x-h) to derive the truncation error, indicating that the question may not be properly worded.
  • Concerns are raised about the distinction between truncation error and rounding error, particularly when evaluating approximations on a computer.
  • There is confusion regarding whether to plot total error or just truncation error, and what constitutes "appropriate values of h."
  • One participant suggests experimenting with different values of h to see what works well for the central difference formula.
  • A later reply indicates that while machine epsilon may be a suggested answer for optimal h, it may not be universally correct due to its dependence on the specific function and point of evaluation.
  • Another participant emphasizes that the optimal value of h for maximizing precision is influenced by both x and f(x).

Areas of Agreement / Disagreement

Participants express various viewpoints on the nature of the errors involved and the optimal choice of h. There is no consensus on the best approach to take, and multiple competing views remain regarding the interpretation of the problem and the implications of using machine epsilon.

Contextual Notes

Participants note that the trade-off between truncation error and rounding error complicates the determination of the optimal h. Additionally, the dependence of the optimal h on the specific function and point of evaluation introduces further complexity.

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
2K
  • · 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