FOURTH ORDER APPROXIMATION
Derivation of FOURTH ORDER APPROXIMATION OF FIRST DERIVATIVE
We use Taylor series approach in order to arrive at fourth order approximation. The theoritical derivation is as follows:
MATLAB CODE:
% Main Body of the program
clear all
close all
clc
x = pi/3;
dx = linspace(pi/4, pi/4000, 20);
%dx = 0.1;
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
figure(1)
loglog(dx, first_order_error)
hold on
loglog(dx, second_order_error,'r')
hold on
loglog(dx, fourth_order_error,'g')
legend('first order error','second order error','fourth order error')
xlabel('dx')
ylabel('Error term')
figure(2)
plot(dx, first_order_error)
hold on
plot(dx, second_order_error,'r')
hold on
plot(dx, fourth_order_error,'g')
legend('first order error','second order error','fourth order error')
xlabel('dx')
ylabel('Error term')
In the main body of the program, all the initial values are declared, i.e., the value of x is declared as pi/3 and dx vary from pi/4 to pi/4000 with equally spaced intervals of 20 points. To calculate the deviation of error for first-order approximation, second-order approximation, and fourth-order approximation.
A graph of error vs dx is plotted on a log-log plot. Log-log plot is used because of a large variation in error terms between them, log-log plot is used.
% MATLAB function to calculate first order approximations
function out = first_order_approximation(x,dx)
%analytical derivative = sin(x)/x^3;
%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
forward_differencing = ((sin(x+dx)/(x+dx)^3)-(sin(x)/x^3))/dx
out = abs(forward_differencing - analytical_derivative)
end
The above function is called by the main function to calculate the first-order approximation. An analytical derivative is calculated by using a forward differencing scheme.
F.D.S = `(f(x+a) - f(x)) / (Delta x)`
% MATLAB function to calculate second order approximations
function out = second_order_approximation(x,dx)
%analytical derivative = sin(x)/x^3;
%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
central_differencing = ((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx)
out = abs(central_differencing - analytical_derivative)
end
The above function is called by the main function to calculate the second-order approximation. An analytical derivative is calculated by using a central differencing scheme.
C.D.S(2) = `(f(x+a) - f(x-a)) / (2*Delta x)`
% MATLAB function to calculate fourth order approximations
function out = fourth_order_approximation(x,dx)
%analytical derivative = sin(x)/x^3;
%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_differencing = (((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);
out = abs(fourth_differencing - analytical_derivative)
end
The above function is called by the main function to calculate the fourth-order approximation. An analytical derivative is calculated by using a central differencing scheme.
C.D.s(4) = `(f(x - 2Deltax) - 8*f(x - Deltax) + 8*f(x - Deltax) - f(x + 2Deltax)) / (12*Delta x)`
Fig 1. Log-log plot
The graph shows the variation of errors along the y-axis and dx along the x-axis using the log-log command.
Fig 2. Graph using the plot command
The graph shows the variation of errors along the y-axis and dx along the x-axis generated using the plot command.