I am having some problems with my code on numerical differentiation. We are supposed to use the two point, three point, four point, and five point formulas to find the first derivative of x^3 - sin x.
It works for the first two formulas, but doesn't work after that. THere doesn't seem to be anything wrong with the formulas I input, but it won't run properly. There is a given value for errors, but it doesn't match up. For example, for the three point formula, it must be 0.0001. However, my code is giving 2.43.
Your code doesn't even compile, so it's hard to tell what you're actually doing wrong. My guess is that there's some typo in all the math you have, perhaps a wrong sign or misplaced parenthesis.
Please post your actual code, and use code formatting tags.
Add [code] and [/code] around your code.
What does "it works"/"doesn't work" mean? Be specific.
Agree with @dhayden: h1 and h2 are missing from the denominators in the 3, 4 and 5-point schemes.
You are also calculating the error (i.e. subtracting from the actual derivatives) in the first four schemes, and then just displaying the estimate of the derivative in the last two.
and similarly for the other approximations. Then you won't be calculating essentially the same thing twice, and you won't be tied to x=1.
Your 3-point schemes should be divided by 2h, not 2.
Your 4-point schemes should be divided by 6h, not 6.
Your 5-point scheme has an error of sign in the last term on each line and you have put h1 where it should be h2 in one part of the last expression. It should also be divided by 12h (I think). Something like