Project

Discretization basics for range of DX

Discretization basics for range of dx.

Effect of dx on error:

Here we observe the effect of dx over error for 1st order, 2nd order, & 4th order approximation using octave.

Anallytical function = sin(x)/x^3

First order approximation:

function out = first_order_approx(x,dx)

% analytical function = sin(x)/x^3;

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

% first order approximation
forward_differencing=((sin(x+dx)/(x+dx)^3)-(sin(x)/(x)^3))/dx;

% output/ error term.
out = abs(forward_differencing - analytical_derivative);

end 

Second order approximation:

function out = second_order_approx(x,dx)

% analytical function = sin(x)/x^3;

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

% second order approximation
central_differencing= ((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx);


% output
out = abs(central_differencing - analytical_derivative);

end

Fourth order approximation:

function out = fourth_order_approx(x,dx)

% analytical function = sin(x)/x^3;

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

% fourth order approximation
central_differencing_1=(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);

% output
out=abs(central_differencing_1 - analytical_derivative);

end

Main Program:

clear all
close all
clc

x = pi/3;
dx = linspace(pi/4,pi/4000,30);



 for i= 1:length(dx) 
    
    first_order_error(i) = first_order_approx(x,dx(i));
    second_order_error(i) = second_order_approx(x,dx(i));  
    fourth_order_error(i) = fourth_order_approx(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')
  

EXPLANATION:

>> For the first order approximation we have to derive the analytical derivative for the above mentioned function and then wee have to derive the forward differencing for the same function using the taylor series.Compute for the absolute error which is the difference for forward differencing and analytical derivative.

>>Similarly this is done for the second ordrer and fourth order approximations.

>> In the main programme initoially we have to declair the value of x ,t with range of dx, then call the functions that we have return as shown above. use loglog operate to plot the errors. Give the correct labels for both x & y axis. Mention the legends clearly so that the obtained graph is much clear.

OUTPUT:

From the above plot it is obserbved that as the value of dx increases the error also increases gradually. for the 1st order we can notice that the curve is flat ,the slope of the plot is increasing gradually. The error is same index as that of value of dx. And for the 2nd order it is observed that there is a steep curve , the slope of plot increases gradually initially and then idecreases slowly then raises exponentially. Similarly for the 4th order there is a steep flat trend is observed and gradually it increases exponentially.

For the larrger range of dx fourth order approximation can be considered.

Finally it is observed that - if the order of the approximation is higher then there is the large range of dx where as there is less change in the error value.

Note: For this kind of programmes to obtain the exact plottings all the equations should be arranged with correct notaions including element wise operations. While writing the derivative terms and the differencing terms we should be careful with the parenthesis.

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.