comparing the approximation error of first, second and fourth order approximations of first derivative against range of dx
Aim: To write a code that compares the approximation error of first, second and fourth order approximations of first derivative against range of dx
Description: The function that I'm using is `f(x) = sin(x)/x^3` by taking the first derivative at `x = pi/3`.
In this i will show how the output of 1st, 2nd and 4th order derivatives varies with respect to grid size (dx) and ploting the outputs to know the error using a log graph.
Procedure:
We can consider any value for dx (where dx is grid spacing in approximation equations)
let us consider some range of dx (ie..pi/30 to pi/3000) with 30 values of dx in between the range.
we shall also check how the graph changes if the value of dx is increased by considering the range of dx "pi/3 to pi/3000" with 30 values of dx in between the range.
Matlab code with explaintion:
clear all
close all
clc
%function to compute is f(x) = sin(x)/x^3 at x = pi/3
%analytical solution for given function is = (x^3*(cos(x)) - sin(x)*3*x^2)/x^6
x = pi/3;
analytical_solution = (x^3*(cos(x)) - sin(x)*3*x^2)/x^6;
%Let us take 25 values for dx in between pi/30 to pi/5000
dx=linspace(pi/30,pi/3000,30);
for i=1:length(dx) %for inserting different vlaue of dx in equation
%first order approximation from Taylors series is (f(x+dx) - f(x))/dx
first_order_approximation (i)= (sin(x+dx(i))/(x+dx(i))^3 -sin(x)/x^3)/dx(i)
%To compare the above solution with analytical solution
first_error(i) = abs(first_order_approximation(i) - analytical_solution);
%second order approximation from Taylors series is (f(x+dx)-f(x-dx)/2dx
second_order_approximation (i) = (sin(x+dx(i))/(x+dx(i))^3-sin(x-dx(i))/(x-dx(i))^3)/(2*dx(i))
second_error(i) = abs(second_order_approximation(i) - analytical_solution);
%fourth order approximation from Taylors series is (f(x-2dx) -8*f(x-dx) +8*f(x+dx) -f(x+2dx))/12dx
fourth_order_approximation (i) = ((sin(x-(2*dx(i)))/(x-(2*dx(i)))^3) - 8*(sin(x-dx(i))/(x-dx(i))^3) + 8*(sin(x+dx(i))/(x+dx(i))^3) - (sin(x+(2*dx(i)))/(x+(2*dx(i)))^3))/(12*dx(i))
fourth_error(i) = abs(fourth_order_approximation(i) - analytical_solution);
end
figure(1)
%To plot a log graph between Error vs Range of dx
loglog(dx,first_error,'r')
hold on
loglog(dx,second_error,'g')
hold on
loglog(dx,fourth_error,'b')
title(' Error vs Range of dx')
grid on
xlabel('Range of dx');
ylabel('Error');
legend('first order error','second order error','fourth order error','Location','southeast');
output:
let us check for range of dx (ie..pi/3 to pi/3000) with 30 values of dx in between the range by just replacing the value at dx as shown below
%Let us take 25 values for dx in between pi/3 to pi/5000
dx=linspace(pi/3,pi/3000,30);
Justification:
If we observe the values of dx in workspace, we calculated the error for function f(x) with 30 different values of dx which results 30 values of 'first error','second error','fourth error'.
Observations from output graph:
1) Slope of the first order error curve is the lowest and the slope of the fourth order error curve is the highest
2)As the grid size dx and approximation is increasing, these curves are bending towards the orgin
Result:
i) for 4th order error curve:
Small change in dx value will produce a large change in the error values.
The errors are reducing with increase in the value of dx (lower values of grid size)
For the same value of dx, the errors in fourth order approximation are less
ii) for 1st order error curve:
Small change in dx value will not considerably change the error values.
The increase in the value of dx is effecting less on errors rectification than 4th order approx.
For the same value of dx, the errors in first order approximation are more than the fourth order approximation.
Conclusion:
Higher order approximations of a function will produce minimal errors and the solution obtained is closer to the analytical solution.
Higher the order of approxiamtion ∝ larger range of dx ∝ minimal error
Link for Zip file
https://drive.google.com/file/d/1vPNyXfWQBDIWYn23j150-3HKwuVLfNpq/view?usp=sharing
Attachments:
Matlab file "error_vs_gridsize.m"
Output graph images