Project

Numerical derivative vs exact derivative

Objective:

Comparison between first, second and fourth order approximations for first order derivative of a function, and showing in bar graph.

given analytical fucnction f(x) = `(sin(x))/x^3`

Steps involved:

=> Deriving analytical derivarive of given function.

=> Difining 'x' value `pi/3`

=> deriving numarical derivatives of 1st order, 2nd order and fourth order using taylor series.

=> find out derivative errors

=> let us assume dx value. dx = pi/40

=> plot the results in bar graph.

=> then able check comparison between approximations

Program:

clear all
close all
clc

% Analytical fucntion f(x) = sin(x)/x^3

% Analytical derivative of f(x)
% f'(x) = (x*cos(x)-3*sin(x))/x^4;

x = pi/3;
analytical_derivative = (x*cos(x)-3*sin(x))/x^4;

% numarical derivative
% first order approximation using forward diferencing
% f'(x) = (f(x+dx)-f(x))/dx
dx = pi/40;
first_order = (((sin(x+dx))/(x+dx)^3)-((sin(x))/x^3))/dx;
first_order_error = abs(first_order-analytical_derivative);

% second order approximation
% f''(x) = (f(x+dx)-f(x-dx))/2*dx

second_order = (((sin(x+dx))/(x+dx)^3)-((sin(x-dx))/(x-dx)^3))/(2*dx);
second_order_error = abs(second_order-analytical_derivative);

% fourth order approximation
% fourth order = (f(x-2dx)-8*f(x-dx)+8*f(x+dx)-f(x+2dx))/(12*dx)
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);
fourth_order_error = abs(fourth_order-analytical_derivative);

% plotting bar graph

a = categorical({'first order','fourth order','second order'});
error = [first_order_error second_order_error fourth_order_error];
bar(a,error)

xlabel('order of approximation')
ylabel('error')
legend('order of derivative approx errors')
title('order of derivatives against errors')

Results:

Graph:

Comparison between first, second and fourth order approximations for first order derivative .

Conclusion:

By increasing the order information taken from its nighbouring values error decreases. Fourth order approximation has taken its four nighbour values.

and its error value close to the analytical value.( see in results).

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.