Project

Discretization Basics

Code is as follows



clear all
close all
clc

x = pi /3; % given value of x

dx = pi/25 %grid spacing

%given function f(x) =sin(x)/x^3 = sin_x is saved as function in another
%file

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


%First order approximation using forward differencing scheme

% forward_differincing = (f(x+dx) - f(x))/dx 

forward_differincing = (sin_x(x+dx) - sin_x(x))/dx;

first_order_error = abs(forward_differincing-analytical_derivative);


%Second order approximation using central differencing scheme.

% central_differincing = (f(x+dx) - f(x-dx))/2*dx 


central_differincing_second_order = (sin_x(x+dx)-sin_x(x-dx))/(2*dx);

second_order_error = abs(central_differincing_second_order-analytical_derivative);


%Fourth order approximation using central differencing scheme.

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

fourth_order_error = abs(central_differincing_fourth_order - analytical_derivative);


Y = [first_order_error,second_order_error,fourth_order_error];

bar(Y,\'FaceColor\',\'c\')
set(gca,\'xticklabel\',{\'First order error\',\'Second order error\',\'Fourth order error\'});

saveas(gca,\'discretizaton bar graph\');


Bar graph of error for respective method is as shown below

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.