Comp Sci Finding Optimal h for Truncation Error O(h2)

  • Thread starter Thread starter ver_mathstats
  • Start date Start date
  • Tags Tags
    Error
AI Thread Summary
The discussion focuses on determining the optimal step size h for minimizing truncation error in numerical differentiation using the central difference formula. The participants clarify that while the truncation error is O(h^2), the evaluation on a computer introduces complexities due to the trade-off between truncation and rounding errors. It is emphasized that the choice of h is not straightforward and depends on the specific function and point of evaluation. For the function f(x) = sin(x)e^(cos(x)), participants suggest experimenting with different values of h to observe their effects on the error. Ultimately, the optimal h is influenced by both the function's characteristics and the numerical precision limits.
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 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
Views
1K
Replies
17
Views
3K
Replies
7
Views
2K
Replies
5
Views
3K
Replies
23
Views
3K
Back
Top