Lab 4

Write a code to approximate the derivative of a functionf(x)usingforwardfinite difference quotientf( x + h ) - f( x ) f'(x) ≈ ------------------- (for small h). hFor the functionf(x) = sin(x), at x=1, compute the FD quotients forh = 1/2and compare with the exact derivative^{k}, k=5,...,N, with N=30cos(x). Outputk , 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.his 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. outputk , log(h) , log(error)and replot. 2. What is the minimum error ? at what k ? Why does the error get worse for smaller h ? Repeat, usingcenteredfinite differences [copy your code to a another file and modify it]f( x + h ) - f( x - h ) f'(x) ≈ ----------------------- (for small h). 2 h3. Which formula performs better ? in what sense ? Submit the following, in a plain text file lab4.txt Lab 4: NAME , date ************************************************* a. Forward FD: Answers to 2. ************************************************* b. Centered FD: Answers to 2. ************************************************* c. Answers to 3. ************************************************* d. your code for centered FD (cleaned up!)