Write a code to approximate the derivative of a function f(x) using forward...

50.1K

Verified Solution

Question

Advance Math

Write a code to approximate the derivative of a function  f(x) using  forward finite difference quotient           f( x + h ) - f( x )      f'(x) ? -------------------   (for small h).                h For the function  f(x) = sin(x), at x=1 , compute the FD quotients for      h = 1/2k, k=5,...,N, with N=30and compare with the exact derivative  cos(x).Output  k , h , error. Where SHOULD the error tend as h ? 0 ?1. Look at the numbers. Does the error behave as expected ?  Output to a file "out" (or to arrays in matlab), and plot it     [ gnuplot> plot "out" u 2:3 with lines ]  Which direction is the curve traversed, left to right or right  to left ? Look at the numbers. h is decreasing  exponentially, so the points pile up on the vertical axis. The  plot is poorely scaled. To see what's happening, use logarithmic  scale, i.e. output  k , log(h) , log(error) and replot.2. What is the minimum error ? at what k ?  Why does the error get worse for smaller h ?3. Repeat, using  centered finite differences  [copy your code to a another file and modify it]          f( x + h ) - f( x - h )      f'(x) ? -----------------------   (for small h).              2 h 4. Which formula performs better ? in what sense ?

Answer & Explanation Solved by verified expert
3.8 Ratings (674 Votes)
Central difference is betterthan forward difference because it achieves faster convergenceMatlab code for forward and central differentialclear allclose allfunction for which    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students