Project

Numerical discretization: Effect of grid size on the absolute error for First, Second and Fourth order Approximation.

Objective

The main objectice of this numerical dicsretization is to compute in matlab, a program that compares the first, second and fourth order approximation error of the first derivative against the analytical or real derivative for a range of grid size.

This project is a continuation and elaboration of a main project called

Numerical Discretization- First, Second and Fourth Order Approximation Error, Numerical vs Analytical.

This project can be found on:

https://projects.skill-lync.com/projects/Numerical-Discretization-First-Second-and-Fourth-Order-Approximation-Error-Numerical-vs-Analytical-44898

The matlab codes in this project are very similar to the codes used previously. The only main difference here is that the codes are written to include many values of dx using the command linspace and defining an extra variable 'i' in the function, which is nothing but an array form 1 to the length of dx. A log plot was created using 'loglog' command so that the results were displayed in the best possible way.

clear all
close all
clc

%Function f(x)= sin(x)/x^3
x=pi/3;

%Analytical first order derivative

analytical_derivative=(x^3*(cos (x))-sin(x)*3*x^2)/x^6;

%Grid size
dx=linspace(pi/4, pi/4000, 1000);
for i=1:length(dx)

% first order approximation
first_order(i)= ((sin(x+dx(i))/(x+dx(i)).^3)-(sin(x)/(x).^3))/dx(i);
% first order approximation error
first_error(i)= abs(first_order(i)- analytical_derivative);
%second order approximation
second_order(i)= (((sin(x+dx(i))/(x+dx(i)).^3)-(sin(x-dx(i))/(x-dx(i))^3)))/(2*dx(i));
%second order approximation error
second_error(i)= abs(second_order(i)- analytical_derivative);
%fourth order approximation
fourth_order(i)= (((((sin(x-2*dx(i))/(x-2*dx(i)).^3))-(8*((sin(x-1*dx(i))/(x-1*dx(i)).^3)))+(8*((sin(x+1*dx(i))))/(x+1*dx(i)).^3)))-((sin(x+2*dx(i))/(x+2*dx(i)).^3)))/(12*dx(i));
%fourth order approximation error
fourth_error(i)= abs(fourth_order(i)- analytical_derivative);

end

%plot of Grid Size vs Absolute error
figure(1)
loglog(dx, first_error,'r')
hold on
loglog(dx, second_error,'g')
hold on
loglog(dx, fourth_error,'b')
grid on
xlabel('Grid size')
ylabel('Absolute error')
legend('First Order Approximation Error','Second Order Approximation Error','Fourth Order Approximation Error')
title ('Effect of Grid Size on First, Second and Fourth Order Approximation Error')

Results

A log plot was generated with grid size on the x-axis and its asbsolute error on the-y axis. The errors reduced for all orders as the grid size became smaller, except there were a few dispropotionality in the error was larger grid size of the fourth order. The slope of the curve for the first and the second orders were linearly proportional irrespective of th size of the grid. The errors were the least for the fourth order for the same reason discussed in the previous project- higher the order of method, lower the error will be for the same grid size. The rate of reduction in error was also highest for the fourth order approximation after a certain value of dx. Similarly second order approximation had a higher rate of reduction in error as compared to the first order approximation.

Conclusion

The code was computed into matlab and a log plot was acheived which was used to evaluate the result. We can conclude that it was more useful to plot dx w.r.t. error as it also displayed the rate of change in error. Some discrepancy was also observed for the fourth order when the grid size was higher.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.