Discretization basics
google link for Matlab program of discretization basics
https://drive.google.com/open?id=1vMOEB6ZRkbz1Xfz5-rGJJ1J-ZP0_pV-8
the above figure shows the comparison of the first order, second order and fourth order approximation of analytical vs exact derivative.
from the figure, it can be concluded that the magnitude of error is the highest in first order approximation and the magnitude of error starts decreasing as we go from second order to fourth order approximations. the error is least in fourth order approximation.
MATLAB CODE
close all
clear all
clc
x = pi/3;
dx= pi/20
for i = 1:length(dx)
first_order_error(i) = first_order_approx(x,dx(i))
second_order_error(i) = second_order_approx(x,dx(i))
fourth_order_error(i) = fourth_order_approx(x,dx(i))
end
c = categorical({'first_order','second_order','fourth_order'})
z = [first_order_error, second_order_error, fourth_order_error];
bar(c,z)
function error_first = first_order_approx(x,dx)
% Analytical Derivative of sinx/x^3
analytical_derivative = ((x^3)*(cos(x)) - sin(x)*3*(x^2))/x^6;
forward_differencing = (sin(x+dx)/(x+dx)^3 - sin(x)/(x^3))/dx;
error_first = abs(forward_differencing - analytical_derivative);
end
function error_fourth = fourth_order_approx(x,dx)
% Analytical Derivative of sinx/x^3
analytical_derivative = ((x^3)*(cos(x)) - sin(x)*3*(x^2))/x^6;
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);
error_fourth = abs(fourth_order - analytical_derivative);
end
function error_sec = second_order_approx(x,dx) % Analytical Derivative of sinx/x^3 analytical_derivative = ((x^3)*(cos(x)) - sin(x)*3*(x^2))/x^6; second_order = (sin(x+dx)/(x+dx)^3 - sin(x-dx)/(x-dx)^3)/(2*dx); error_sec = abs(second_order - analytical_derivative); end