Question

In: Advanced Math

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

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=30
and 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 ?

Solutions

Expert Solution


Central difference is better than forward difference because it achieves faster convergence.

%%Matlab code for forward and central differential
clear all
close all

%function for which derivative have to find
f=@(x) sin(x);
%exact derivative
g=@(x) cos(x);
%loop for derivative
cnt=0; x0=1;
for k=5:30
  
    cnt=cnt+1;
    h(cnt)=1/(2^k);
    kk(cnt)=k;
  
    %forward derivative
    val1(cnt)=(f(x0+h(cnt))-f(x0))/h(cnt);
    err1(cnt)=abs(g(x0)-val1(cnt));
  
    %central derivative
    val2(cnt)=(f(x0+h(cnt))-f(x0-h(cnt)))/(2*h(cnt));
    err2(cnt)=abs(g(x0)-val2(cnt));
  
end

mm1=find(err1==min(err1));
fprintf('For forward differential minimum error occured at h=%e and k=%d\n',h(mm1(1)),kk(mm1(1)))


mm2=find(err2==min(err2));
fprintf('For central differential minimum error occured at h=%e and k=%d\n',h(mm2(1)),kk(mm2(1)))

figure(1)
plot(kk,err1)
hold on
plot(kk,err2)
xlabel('k')
ylabel('error')
title('k vs. error plot')
legend('Forward difference','Central difference')


figure(2)
plot(kk,log(err1))
hold on
plot(kk,log(err2))
xlabel('k')
ylabel('log(error)')
title('k vs. log(error) plot')
legend('Forward difference','Central difference')

fprintf('Central difference is better than forward difference\n')

%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%


Related Solutions

A) Find the derivative of the function f by using the rules of differentiation. f(x) =...
A) Find the derivative of the function f by using the rules of differentiation. f(x) = 380 B)Find the derivative of the function f by using the rules of differentiation. f(x) = x0.8 C) Find the derivative of the function f by using the rules of differentiation. D)Find the derivative of the function. f(u) = 10 u E)Find the derivative of the function f by using the rules of differentiation. f(x) = 9x3 - 2x2 + 1 u u
Suppose that f is a differentiable function with derivative f" (x) = (x − 3)(x +...
Suppose that f is a differentiable function with derivative f" (x) = (x − 3)(x + 1)(x + 5). Determine the intervals of x for which the function of f is increasing and decreasing Explain why a positive value for f" (x) means the graph f(x) is increasing For f(x) = 2x2- − 3x2 − 12x + 21, find where f'(x) = 0, and the intervals on which the function increases and decreases Determine the values of a, b, and...
Part A. If a function f has a derivative at x not. then f is continuous...
Part A. If a function f has a derivative at x not. then f is continuous at x not. (How do you get the converse?) Part B. 1) There exist an arbitrary x for all y (x+y=0). Is false but why? 2) For all x there exists a unique y (y=x^2) Is true but why? 3) For all x there exist a unique y (y^2=x) Is true but why?
Derive the 2nd order accurate finite difference approximation for the 1st derivative of a function at...
Derive the 2nd order accurate finite difference approximation for the 1st derivative of a function at the data point Xo. That is, find the coefficients a, b, c such that: af(Xo) + bf(X1) + cf(X2) = f'(Xo) + O(h^2). (Note: there is a prime sign on the f'(Xo) at the right side)
A. Use the Product Rule or the Quotient Rule to find the derivative of the function....
A. Use the Product Rule or the Quotient Rule to find the derivative of the function. g(x) = x3 cot(x) + 6x cos(x) B. Use the Product Rule or the Quotient Rule to find the derivative of the function. f(x) = x2 + x − 7 x2 − 7 C. Use the Product Rule or the Quotient Rule to find the derivative of the function. f(x) = (8x2 + 4)(x2 − 6x − 9)
Question 1 Let f be a function for which the first derivative is f ' (x)...
Question 1 Let f be a function for which the first derivative is f ' (x) = 2x 2 - 5 / x2 for x > 0, f(1) = 7 and f(5) = 11. Show work for all question. a). Show that f satisfies the hypotheses of the Mean Value Theorem on [1, 5] b)Find the value(s) of c on (1, 5) that satisfyies the conclusion of the Mean Value Theorem. Question 2 Let f(x) = x 3 − 3x...
Write a program (fortran 90) that calls a subroutine to approximate the derivative of y=sin(x)+2x^2 using...
Write a program (fortran 90) that calls a subroutine to approximate the derivative of y=sin(x)+2x^2 using a one-sided difference approach fx = (fi-fi-1)/deltaX and a centered difference approach fx = (fi+1-fi-1)/deltaX. The value of the function f and its derivative fx should be evaluated at x=3.75. Your code should print both values tot he screen when it runs.
Let f(x) = −3+(3x2 −x+1) ln(2√x−5) (a) Find the derivative of f. (b) Using the derivative...
Let f(x) = −3+(3x2 −x+1) ln(2√x−5) (a) Find the derivative of f. (b) Using the derivative and linear approximation, estimate f(9.1).
For the following exercises, use the definition of derivative to calculate the derivative of each function. f(x) = 5π
For the following exercises, use the definition of derivative to calculate the derivative of each function.f(x) = 5π
prove using the definition of derivative that if f(x) and g(x) is differentiable than (f'(x)g(x) -...
prove using the definition of derivative that if f(x) and g(x) is differentiable than (f'(x)g(x) - f(x)g'(x))/g^2(x)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT