Project

To observe the effect of dx on error,for first,second and fourth order approximation by using MATLAB.

Aim:-The main intention to do this project,is to observe the effect of dx on error,for first,second and fourth order approximation by using Matlab.

Theory:-Following program has been written on MATLAB for computing absolute error for first,second and fourth order approximation for a given value of x and for the range of dx, and the output of the program has been shown after the all program and then we are calling all of this program in main program,by using function to see what is the effect of dx on error by plotting and also to see how they are related to each other.

#Function:-

f(x)=sin(x)/x^3

#First order approximation:-In this we have ignore the term which is of first order and since we are ignoring some term there will be an approximation that why its called as first order approximation.

  • In the following code,first we have derive analytical derivative for the above function and then after for the first order approximation we have derive forward differencing for the same function by using Taylor-series of finite difference method.After then we have computed absolute error which is basically the difference of forward differencing and analytical derivative.
function out = first_order_approximation(x,dx)
% analytical function = 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
% forward differncing 
% forward differncing = (f(x+dx)-f(x))/dx

% first_order approximation 
forward_differncing = ((sin(x+dx)/(x+dx)^3)-(sin(x)/x^3))/dx;

out = abs(forward_differncing - analytical_derivative);

end


#Second order approximation:-In this we have ignore the term which is of second order and since we are ignoring some term there will be an approximation that why its called as second order approximation.

  • Similarly,in the following code,first we have derive analytical derivative for the above function and then after for the second order approximation we have derive central differencing for the same function by using Taylor-series of finite difference method.After then we have computed absolute error which is basically the diiference of central differencing and analytical derivative.
function out = second_order_approximation(x,dx)
% analytical function = 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
% central_differncing 
% central_differncing = (f(x+dx)-f(x))/dx

% second_order_approximation 
central_differncing = ((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx);

out = abs(central_differncing - analytical_derivative);

end


#Fourth order approximation:-In this we have ignore the term which is of fourth order and since we are ignoring some term there will be an approximation that why its called as fourth order approximation.

  • Also in the following code,first we have derive analytical derivative for the above function and then after for the fourth order approximation we have derive central differencing for the same function by using Taylor-series of finite difference method.After then we have computed absolute error which is basically the diiference of central differencing and analytical derivative.
function out = fourth_order_approximation(x,dx)
% analytical function = 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
% central_differncing 
% central_differncing = (f(x+dx)-f(x))/dx

% first_order approximation 
central_differncing = ((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(central_differncing - analytical_derivative);

end


#Output:-Below image shows the output of all above code.The output has been calculated for x=pi/3 and dx=pi/4000.

Observation:- From above output its clear that as order of approximation increases the error get reduces for same value of x and dx and also we can say that the order of approximation is inversely proportional to error.

For example,from above figure we have error equal to 0.0019629 for first order as order of approximation increases to fourth order the error reduce to 6.8419e-0.12.

# Numerical discretization:-The process of converting partial differential equation into algebraic form so,we can compute it by aid of computer is called as numerical discretiation.

# Main program:-In the following code,first we have alloted the value of x and the range of dx,after then we have call all above three program by using function,and then we have plot effect of dx on first,second and fourth order error, by using for loop.

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
 
 figure(1)
 loglog(dx,first_order_error,'r')
 hold on
 loglog(dx,second_order_error,'g')
 hold on
 loglog(dx,fourth_order_error,'b')
 
 xlabel('dx')
 ylabel('error')
 legend('first_order_error','second_order_error','fourth_order_error')
 legend('Location','southeast')
 
 

Following image shows the effect of dx on error for first,second and fourth order approximation:-

# Plot:-

Observation:-Following observation made by observing above graph

  • First,of all its clear that as dx increases the error increases.
  • And when the order of approximation increases error get reduce.

Slope of the curve:-Now,lets talk about slope of the curve as its clear from the above image that as order of approximation increases the slope of the curve for gets bends more and direct towards the origin,which means error get reduce.

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.