Project

Comparison of numerical derivative and analytical derivative

The purpose of this project is to compare the exact derivative of the given function with the first order, second order and fourth order approximations of the first derivative of the same function and find out the magnitude of error introduced while using these numerical schemes.

Here the given function is `sin(x)/x^3`

The exact derivative of this function is `(x^4cos(x)-3x^2sin(x))/x^6`

First order approximation

We use the forward differencing scheme and derive the numerical derivative term:

`(delf)/(delx)=(f(x+/_\x)-f(x))/(/_\x`

This is called first order approximation because the error introduced in this is of the order `/_\x`.

The code for calculating first order approximation is shown below:

function out = first_order_approximation(x,dx)analytical_derivative = (x^3*cos(x)-sin(x)*3*x^2)/(x^6);approximation_first_order = (sin(x+dx)/((x+dx)^3)-sin(x)/(x^3))/dx;out = abs(approximation_first_order-analytical_derivative);end

Second order approximation

In this we use central differencing schemes and derive the numerical derivative:

`(delf)/(delx)=(f(x+/_\x)-f(x-/_\x))/(2/_\x)`

This is called second order approximation because the error introduced in this is of the order `/_\x^2`

The code for calculating second order approximation is shown below:

function out = second_order_approximation(x,dx)analytical_derivative = (x^3*cos(x)-sin(x)*3*x^2)/(x^6);approximation_second_order = ((sin(x+dx)/((x+dx)^3))-(sin(x-dx)/((x-dx)^3)))/(2*dx);out = abs(approximation_second_order-analytical_derivative);end

Fourth order approximation

In 4th order approximation scheme we use central differencing technique and the term derived is shown below:

`(delf)/(delx)=(f(x-2/_\x)-8f(x-/_\x)+8f(x+/_\x)-f(x+2/_\x))/(12/_\x)`

This is called fourth order approximation as the error introduced here is of the order od `/_\x^4`

The code for fourth order approximation is shown below:

function out = fourth_order_approximation(x,dx)analytical_derivative = (x^3*cos(x)-sin(x)*3*x^2)/(x^6);approximation_fourth_order = ((sin(x-2*dx)/((x-2*dx)^3))-(8*(sin(x-dx)/((x-dx)^3)))+(8*(sin(x+dx)/((x+dx)^3)))-(sin(x+2*dx)/((x+2*dx)^3)))/(12*dx);out = abs(approximation_fourth_order-analytical_derivative);end

The main function is shown below:

clear allclose allclcx=pi/3;dx=pi/25;%Calulating exact derivativeexact_derivative=(x^3*cos(x)-sin(x)*3*x^2)/(x^6)%Calling functions first_order_error=first_order_approximation(x,dx);second_order_error=second_order_approximation(x,dx);fourth_order_error=fourth_order_approximation(x,dx);error = [first_order_error second_order_error fourth_order_error];c= [first_order_error second_order_error fourth_order_error];bar(c,dx,'FaceColor',[0 0.5 1],'EdgeColor',[0 0.5 1],'BarWidth',0.7)set(gca,'XTickLabel',{'1st order approximation';'2nd order approximation';'4th order approximation'})ylabel('Error')legend('magnitude of error')title('Comparison between numerical derivative and exact derivative')fprintf('\nFirst order error %d\n',first_order_error)fprintf('\nsecond order error %d\n',second_order_error)fprintf('\nFourth order error %d\n',fourth_order_error)

The graph obtained is shown below:

From the above graph and output obatined we observe the following:

  • First order approximation has the maximum error
  • Fourth order approximation has the least error
  • The derivative obtained from fourth order approximation is closed to the exact derivative.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.