Question

For a given h, a derivative at a point x0 can be approximated using a forward...

For a given h, a derivative at a point x0 can be approximated using a forward difference, a backward difference, and a central difference: f 0 (x0) ≈ f(x0 + h) − f(x0) h forward difference f 0 (x0) ≈ f(x0) − f(x0 − h) h backward difference f 0 (x0) ≈ f(x0 + h) − f(x0 − h) 2h central difference. Using MATLAB or Octave, Write a script that prompts the user for an h value and an x0 value, storing each in a variable. The script should then calculate, save, and print the three approximations for sin0 (x0). (In other words, we take sin(x) as f(x) in this question.) The script should also calculate, save, and print the absolute error in each approximation. Note that to display a single quote inside an fprintf statement, two single quotes (not a double quote) should be used. Sample output: Enter a value for h: 0.1 Enter a value for x0: pi/6 The forward difference approximation of sin’( 0.52 ) is 0.839604 The backward difference approximation of sin’( 0.52 ) is 0.889562 The central difference approximation of sin’( 0.52 ) is 0.864583 The true value of sin’( 0.52 ) is 0.866025 The absolute error in the forward difference approximation is 0.026422 The absolute error in the backward difference approximation is 0.023537 The absolute error in the central difference approximation is 0.001443 Enter a value for h: 0.01 Enter a value for x0: pi The forward difference approximation of sin’( 3.14 ) is -0.999983 The backward difference approximation of sin’( 3.14 ) is -0.999983 The central difference approximation of sin’( 3.14 ) is -0.999983 The true value of sin’( 3.14 ) is -1.000000 The absolute error in the forward difference approximation is 0.000017 The absolute error in the backward difference approximation is 0.000017 The absolute error in the central difference approximation is 0.000017

Homework Answers

Answer #1

h=input('Enter a value for h: ');

x0=input('Enter a value for x0: ');

f=@(x)sin(x);

forw=(f(x0+h)-f(x0))/h;

back=(f(x0)-f(x0-h))/h;

cent=(f(x0+h)-f(x0-h))/(2*h);

fprintf('The forward difference approximation of sin''( %.2f ) is %f \n',x0,forw)

fprintf('The backward difference approximation of sin''( %.2f ) is %f \n',x0,back)

fprintf('The central difference approximation of sin''( %.2f ) is %f \n',x0,cent)

fprintf('The true value of sin''( %.2f ) is %f \n',x0,cos(x0))

fprintf('The absolute error in the forward difference approximation is %f \n',abs(forw-cos(x0)))

fprintf('The absolute error in the backward difference approximation is %f \n',abs(back-cos(x0)))

fprintf('The absolute error in the central difference approximation is %f \n',abs(cent-cos(x0)))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT