DISCRETIZATION BASICS - NUMERICAL DERIVATIVE VS EXACT DERIVATIVE
OBJECTIVE:
In this project, the first order derivative is discretized to obtain the desired order of accuracy. It is then discretized with forward differencing scheme to compare the results.
Formula for the fourth order approximation of the first derivative:
THE MATLAB CODE:
close all
clear all
clc
%Given function sin(x)/x^3;
%Derivative of Given function ((x^3*cos(x))-(sin(x)*3x^2))/x^6;
%Let x= pi/3;
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_differenc(Or second order)=(f(x+dx)-f(x-dx))/2dx
%fourth_order_differencing =(f(x-(2*dx)))-(8*f(x-dx))+(8*f(x+dx))-(f(x+(2*dx)))/(12*dx)
dx=pi/400000;
Forward_difference = (sin_x(x+dx)-sin_x(x))/dx;
Central_diffrence = (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_diffrence - analytical_derivative_solution)
fourth_order_error = abs(Fourth_order_difference - analytical_derivative_solution)
y = [first_order_error ,second_order_error ,fourth_order_error];
%2D bar graph
bar(y,0.4);
title('Comparison of first,second and fourth order approximations');
xlabel('Approximation');
RESULTS:
THE FOLLOWING GRAPH WAS OBTAINED;