Discretization basics
Discretization basics
Objective :-
Function programe :-
1.First order approximation :-
% function for first order error calculation
function output1 = first_order_approximate(x,dx)
analytical_derivative = (x^3*(cos(x))- sin(x)*3*x^2)/x^6;
num_first_order = (sin(x+dx)/(x+dx)^3-sin(x)/x^3)/dx;
output1 = abs(analytical_derivative - num_first_order);
end
- In the 1st order approximation method forward differencing will taken from one forward point to calculate the error value of the function.
2.Second order approximation :-
% function for second order error calculation
function output2 = second_order_approximate(x,dx)
analytical_derivative = (x^3*(cos(x))- sin(x)*3*x^2)/x^6;
num_second_order = (sin(x+dx)/(x+dx)^3-sin(x-dx)/(x-dx)^3)/(2*dx);
output2 = abs(analytical_derivative - num_second_order);
end
- In the second order approximation method center differencing wil taken from analytical derivation point to calculate the error value of the function.
3.Fourth order approximation :-
% function for fourth order error calculation
function output3 = fourth_order_approximate(x,dx)
analytical_derivative = (x^3*(cos(x))- sin(x)*3*x^2)/x^6;
num_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);
output3 = abs(analytical_derivative - num_fourth_order);
end
- In the fourth order approximation method we have to calculate the error from analytical derivative to fourth order approximation then error value will be finded over.
Main programe :-
% Programe for discretization basics
clear all
close all
clc
% analytical function fx = sin(x)/x^3
% analytical derivative f'(x) = (x^3*(cos(x))- sin(x)*3*x^2)/x^6;
x = pi/3;
dx = linspace(pi/4,pi/4000,20);
for i = 1:length(dx)
error1(i) = first_order_approximate(x,dx(i));
error2(i) = second_order_approximate(x,dx(i));
error3(i) = fourth_order_approximate(x,dx(i));
end
errorgraph = [error1(15) error2(15) error3(15)];
a = categorical({'1st_o_approx','2nd_o_approx','4th_o_approx'});
bar(a,errorgraph);
title('Comparison of first,second and fourth order approximation');
ylabel('error');
xlabel('approximation');
Explanation :-
- In main programe we have to call function for first, second , fourth order approximation
- After that we have to create the analytical function of fx = sin(x)/x^3.
- Then create analytical derivative f'(x) = (x^3*(cos(x))- sin(x)*3*x^2)/x^6.
- we have to compute the first derivative at x = pi/3 , ather that we have to create linspace command .
- Create for loop command to analysis the error .
- By using Bar command we have to create the bar value error.
Error while facing in programe :-
No error
Result :-
Bar Graph :