Project

Discretization Basics

Objective:

To write a program that compares the first, second and fourth order approximations of the first derivative against the analytical or exact derivative.

The given function is f(x) = sin(x)/x^3

close all
clear all
clc
%given function sin(x)/x^3
%derivative of given function ((x^3*cos(x))-(sin(x)*3*x^2))/x^6
x=pi/3;
analytical_derivative_solution=(((x^3)*cos(x))-(sin(x)*3*(x^2)))/(x^6);

function out= sin_x(x)
out=sin(x)/x^3;
end
%forward difference =(f(x+dx)-f(x))/dx
%central difference(or second order) = (f(x+dx)-f(x-dx))/2*dx
%fourth order difference =(f(x-(2*dx)))-(8*f(x-dx))+(8*f(x+(2*dx)))/(12*dx)
dx=pi/20;
forward_difference=(sin_x(x+dx)-sin_x(x))/dx;
central_difference=(sin_x(x+dx)-sin_x(x-dx))/(2*dx);
fourth_order_difference=((sin_x(x-(2*dx)))-(8*sin_x(x-dx))+(8*sin_x(x+dx))-(sin_x(x+(2*dx))))/(12*dx);
%error

first_order_error=abs(forward_difference-analytical_derivative_solution)

second_order_error=abs(central_difference-analytical_derivative_solution)

fourth_order_error =abs(fourth_order_difference-analytical_derivative_solution)

y=[first_order_error, second_order_error, fourth_order_error];
%bar graph

bar(y,0.4);
title('comparison of first, second and fourth order approximation');
xlabel('approximation');

we have to compare first, second and fourth order aproximation error of the first derivative against analytical derivative or real derivative.

analytical derivative of the given function is f'(x) =(x^3*cos(x)-(sin(x)*3*x^2))/x^6

the above derivative equation is used as input for analytical equation and will be compared with first, second and fourth order approximation equation.

first order approximation is obtained by applying forward difference method for the given function f(x), i.e. d(f(x))/dx= (sin(x+dx)/(x+dx)^3-sin(x)/(x^3))/dx

similarly second porder approximation also known as central difference scheme using taylor series and ignoring higher order terms i.e.

central_difference=(sin(x+dx)-sin(x-dx))/(2*dx);

fourth order approximation is obtained by using central difference scheme and taylor series for the given function i. e.

fourth_order_difference=((sin_x(x-(2*dx)))-(8*sin_x(x-dx))+(8*sin_x(x+dx))-(sin_x(x+(2*dx))))/(12*dx);

absolute error is obtained by equations

first_order_error=abs(forward_difference-analytical_derivative_solution)

second_order_error=abs(central_difference-analytical_derivative_solution)

fourth_order_error =abs(fourth_order_difference-analytical_derivative_solution)

finally bar graph is obtained using command 'bar(y,0.4)'

the graph shows that the error for the first order approximation is higher than comparing with second and fourth order approximation error.

this is because for fourth order scheme, input is considered from more neighboring points, where as in first order forward difference scheme only one input is considered. thus error in case of first order approximation is more.

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.