Effect of range of dx on first, second and fourth order approximations for a given value of x.
What is Discretization?
The general idea behind discretization is to break a domain into a mesh, and then replace derivatives in the governing equation with difference quotients.
The discretization process is used to solve different PDE's, by changing them into algebraic terms.
Following code is written to calculate the First, second and fourth-order error for a range of dx and given value of x.
clear all
close all
clc
x=pi/3;
dx= linspace(pi/4,pi/4000,20);
for i= 1:length(dx)
first_order_error(i)=first_order_approximation(x, dx(i));
second_order_error(i)=second_order_approximation(x,dx(i));
fourth_order_error(i)= fourth_order_approximation(x,dx(i));
end
%Plotting
figure(1)
loglog(dx, first_order_error);
hold on
loglog(dx, second_order_error);
hold on
loglog(dx,fourth_order_error);
legend('First order error','Second Order Error','Fourth Order Error');
xlabel('Range of dx');
ylabel('Absolute error');
title('Comparison Of 1st 2nd 4th order approximation for Range of dx');
The following code depicts the function used above in the main code for the First order error:
function output_error= first_order_approximation(x, dx)
% analytical fucntion = sin(x)/x^3;
% analytical derivative
%f'(x)= (x^3*cos(x)- sin(x)*3*x^2)/x^6;
analytical_derivative=(x^3*cos(x)- sin(x)*3*x^2)/x^6;
%numerical derivative
%first order approximation
first_order = (sin(x+dx)/(x+dx)^3-(sin(x)/(x)^3))/dx;
%First order error
output_error = abs(first_order- analytical_derivative);
end
The following code depicts the function used above in the main code for the second-order error:
function output_error= second_order_approximation(x, dx)
% analytical fucntion = sin(x)/x^3;
% analytical derivative
%f'(x)= (x^3*cos(x)- sin(x)*3*x^2)/x^6;
analytical_derivative=(x^3*cos(x)- sin(x)*3*x^2)/x^6;
%numerical derivative
%second order approximation
second_order = (sin(x+dx)/(x+dx)^3-sin(x-dx)/(x-dx)^3)/(2*dx);
%second order error
output_error = abs(second_order- analytical_derivative);
end
The following code depicts the function used above in the main code for the fourth-order error:
function output_error= fourth_order_approximation(x, dx)
% analytical fucntion = sin(x)/x^3;
% analytical derivative
%f'(x)= (x^3*cos(x)- sin(x)*3*x^2)/x^6;
analytical_derivative=(x^3*cos(x)- sin(x)*3*x^2)/x^6;
%numerical derivative
%fourth order aprroximation
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);
%Fourth order error
output_error = abs(fourth_order- analytical_derivative);
end
The codes present view for solving the derivative of a function f(x) = sin(x)/x^3 and solving the 1st order, 2nd order and 4th order approximations of the function.
First Order Approximation= `(f(x+dx)-f(x))/dx`
Second Order Approximation= `(f(x+dx)-f(x-dx))/(2dx`
Fourth Order Approximation= `(f(x-2)-8*f(x-1)+8*f(x+1)-f(x+2))/(12dx)`
The following plot represents the absolute error difference between 1st order, 2nd order, 4th order approximations for a range of dx value.
The error represents that the graph for Fourth order approximation falls drastically compared to the other two different approximation. This is because as we increase the number of approximation. i.e increasing information terms from other surrounding data, the error drops and matches almost to the actual number.
SLOPE OF LINES:
Following code is generated to calculate the slope of all 3 approximations.
%To find the Slope of all 3 approximation
%slope= m*dx+c
slope_first_order= polyfit(dx, first_order_error,1);
slope_second_order= polyfit(dx, second_order_error,1);
slope_fourth_order= polyfit(dx, fourth_order_error,1);
Slopes=[slope_first_order(1) slope_second_order(1) slope_fourth_order(1)];
The Polyfit function for a linear graph fit returns the slope as the first parameter and the intercept as the second parameter.
Slopes =(1.1768 7.0256 39.3472)
The slopes for the fourth-order is large which states that the line is steep and thus the error drops significantly along the range of dx