Discretization basics for range of dx
Aim:
To create a code that compute the first, second and fourth order approximations for a given value of x for a range of dx values using functions in MATLAB.
Objectives:
- To create a matrix that has a range of dx values.
- To create a separate functions to calculate the errors in first,second and fourth order approximations.
- To analyse the errors in first, second and fourth order errors by plotting their values using 'loglog' command.
Discretization:
Discretization is a process of transferring the function or equation into discrete points. This discretization process helps to convert the partial differential equations into algebraic function. Discretization plays a pivotal role in the computational fluid dynamics domain. There are various methods of discretization,
- Finite element method.
- Finite difference method.
- Finite volume method.
The exact discretization may not be found due to heavy matrix exponential and other factors. This can be prevented by finding the approximation of the function and find its error from the exact or analytical value of the function. Based on the consideration of number of discretized points, the order of approximation varies.
Main code:
clear all
close all
clc
% Initializing x value and the function
x=pi/3;
fn = sin(x)/x^3;
%dx value
dx = linspace(pi/4,pi/4000,30);
% error calculation
for i=1:length(dx);
error1(i) = first_order(x,dx(i))
error2(i) = second_order(x,dx(i))
error4(i) = fourth_order(x,dx(i))
end
%plotting the errors
loglog(dx,error1,'color','b')
hold on
loglog(dx,error2,'color','r')
hold on
loglog(dx,error4,'color','g')
xlabel('dx')
ylabel('Errors')
title('Representation of errors (dx vs errors plot)')
legend('error1','error2','error4')
Function codes:
a) First order error:
function error1 = first_order(x,dx)
%Analytical value calculation
analytical = (x^3*cos(x) - 3*x^2*sin(x))/x^6;
%first order approximation
first_order = ((sin(x+dx)/(x+dx)^3) - (sin(x)/x^3))/dx;
%error
error1 = abs(first_order-analytical);
end
b) Second order error:
function error2 = second_order(x,dx)
%analytical value calculation
analytical = (x^3*cos(x) - 3*x^2*sin(x))/x^6;
%second order approximation
second_order = ((sin(x+dx)/(x+dx)^3) - (sin(x-dx)/(x-dx)^3))/(2*dx);
%error
error2 = abs(second_order-analytical);
end
c) Fourth order error:
function error4 = fourth_order(x,dx)
% analytical value calculation
analytical = (x^3*cos(x) - 3*x^2*sin(x))/x^6;
%separating the approximation equation into various terms
a = (sin(x-dx-dx))/(x-dx-dx)^3;
b = 8*((sin(x-dx))/(x-dx)^3);
c = 8*((sin(x+dx))/(x+dx)^3);
d = (sin(x+dx+dx))/(x+dx+dx)^3;
%fourth order approximation with error
fourth_order = (a-b+c-d)/(12*dx);
error4 = abs(fourth_order-analytical);
end
Code explanation:
- Initially, x value and the function is assigned.
- A range of 30 values starting from pi/4 to pi/4000 is assigned for dx using linspace command.
- Using the for loop, the error values for first, second and fourth order approximations are obtained for the range value of dx.
- The errors of the various approximations are found using functions.
- Finally, all the error values obtained in first, second and fourth order approximations are plotted and analysed.
- Since, the error values of various orders differs in magnitude, 'loglog' command is used to make the plot which make sense.
Function code:
- The function was named based on the order of approximation with proper input parameters.
- The analytical values are calculated in the function.
- Then, the respective order approximation equations are solved and obtained the respective values in the function itself.
- Finally, the error value is obtained by the difference between approximation values and the analytical value.
Output plot:
Conclusion:
- From the plot, the error decreases with increase in the order of approximation.
- The error in the fourth order approximation has a minimum value with respect to dx, followed by second order and first order errors.
- Similarly, the slop of the line that represent the fourth order approximation error is higher when compared with other two lines.
- As the dx value decreases, the error drops down in the line that represent the fourth order approximation error as it has a high value of slope.
- So, it represent a conclusion that, when the order of approximation increases for the range of value of dx, the error value decreases with high value of slope.