Project

Week 3.3 - Discretization basics

Week 3.3 - Discretization basics

Aim : Compareing the first, second and fourth order approximations of the first derivative against the analytical or exact derivative.

The given program is about discretizing a specific function, and showing the error in approximation between analytical and numerical derivatives.

To begin with, the program begins by defining an analytical function, computing its derivative x=pi/3 and numerical first order derivative, by forward differencing at dx=pi/30. Secondly, the second order numerical derivative is calculated using central differencing and finally the fourth order numerical derivative, from given relation.

In the next stage, the error in first order approximation, as a difference of "forward differencing and analytical derivative" is evaluated using absolute difference ("abs' command"), similarly, same procedure is followed for finding the error in second order approximation, using calculated central differencing and fourth order, by fourth order approximation formula.

In order to plot the errors , an 'error' array is created which comprises of all three calculated error approximations, and is displayed as bar graph, showing the "magnitude of error approximation" vs "order of approximation". The fourth order error approximation is the least of all.

Program:

close all
clear all
clc

%analytical function = sin(x)/x^3

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

%numerical derivative for first order approximation
dx =  pi/30;
forward_differencing = ((sin(x+dx)/(x+dx)^3)-(sin(x)/(x)^3))/dx;

%numerical derivative for 2nd order approximation by central differencing
central_differencing = ((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx);

%numerical derivative for 4th order approximation by central differencing
fourth_order_approxiation= ((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);

error_1st_order_approximation = abs(forward_differencing - analytical_derivative); 
error_2nd_order_approximation = abs(central_differencing - analytical_derivative); 
error_4th_order_approximation = abs(fourth_order_approxiation - analytical_derivative); 

figure(1)
error =[error_1st_order_approximation,error_2nd_order_approximation,error_4th_order_approximation];
bar(0.4,error, 'm');
xlabel('Order of approximation');
ylabel('Error');
legend('Error approximation for order of derivatives');

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.