Finding Optimal h for Truncation Error O(h2)

  • Comp Sci
  • Thread starter ver_mathstats
  • Start date
  • Tags
    Error
In summary, for the function f(x)=sinxe^cosx, plotting the error |(f'(x)-(f(x+h)-f(x-h))/2h)| versus h x for the appropriate values of h value of h given in Part (B) would be the best way to go about it.
  • #1
ver_mathstats
260
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
  • #2
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
  • #3
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.
 
  • #4
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.
 
  • #5
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?
 
  • #6
xDocMath said:
For part b, is the answer when h equals machine epsilon?
Yes it is.
 
  • #7
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) ##.
 

FAQ: Finding Optimal h for Truncation Error O(h2)

What is the purpose of finding optimal h for truncation error O(h2)?

The purpose of finding optimal h for truncation error O(h2) is to determine the smallest possible value of h that will result in the most accurate approximation of a mathematical function. This is important in numerical analysis and scientific computing, where small errors in calculations can have significant impacts on the accuracy of results.

How is h related to truncation error?

Truncation error is the difference between the exact value of a mathematical function and its numerical approximation. The value of h, which represents the step size in a numerical method, directly affects the magnitude of truncation error. As h decreases, the truncation error also decreases, resulting in a more accurate approximation.

What is the significance of O(h2) in finding optimal h for truncation error?

O(h2) is a notation used to represent the order of magnitude of the truncation error. It indicates that the error is proportional to the square of the step size h. This is important in determining the optimal value of h, as a smaller value of h will result in a smaller truncation error and a more accurate approximation.

How is the optimal value of h determined for a specific function?

The optimal value of h is determined by performing a series of calculations with different values of h and comparing the resulting truncation errors. The value of h that produces the smallest truncation error is considered to be the optimal value for that specific function.

Are there any limitations to finding optimal h for truncation error O(h2)?

Yes, there are limitations to finding optimal h for truncation error O(h2). This method assumes that the function being approximated is smooth and continuous, and that the numerical method being used is accurate. In reality, there may be other factors that affect the accuracy of the approximation, such as round-off error and numerical instability.

Similar threads

Replies
1
Views
1K
Replies
17
Views
1K
Replies
7
Views
2K
Replies
5
Views
2K
Replies
23
Views
2K
Replies
7
Views
1K
Replies
1
Views
1K
Back
Top