Project

Effect of dx on error

AIM: To write a MATLAB code which shows the effect of dx on error

MAIN CODE:-

%% 

% code to find the Effect of error on dx
x  = pi/3;
dx = linspace(pi/40, pi/4000, 10);

first_order_error = zeros(1,10);
second_order_error = zeros(1,10); 
Fourth_order_error = zeros(1,10);

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

%PLOTTING
loglog(dx,first_order_error,'linewidth',2,'color','r')
hold on 
loglog(dx,second_order_error,'linewidth',2,'color','g')
loglog(dx,Fourth_order_error,'linewidth',2,'color','m')

title('Effect of dx on the error')
xlabel('(dx)')
ylabel('Error')
legend('First order error', 'Second order error', 'Fourth order error')

%Calculating SLOPES
p = polyfit(dx,first_order_error,1);
q = polyfit(dx,second_order_error,1);
r = polyfit(dx,Fourth_order_error,1);

slopes = [p(1), q(1), r(1)];

%%

FUNCTIONS USED:-

FIRST ORDER APPROXIMATION

%%

function forward_error = first_order_approximation(x,dx)

Analytical_derivative = ((cos(x))/x^3)- ((3*sin(x))/x^4);

%Numerical derivative 
b = (sin(x+dx)/(x+dx)^3);              %f(x+dx) 
a = (sin(x)/x^3);                      %f(x)

forward_difference = (b-a)/dx;        

forward_error = abs(forward_difference - Analytical_derivative);

end

SECOND ORDER APPROXIMATION

%%

function central_error = Second_order_approximation(x,dx)

Analytical_derivative = ((cos(x))/x^3)- ((3*sin(x))/x^4);

%Numerical derivative 
b = (sin(x+dx)/(x+dx)^3);              %f(x+dx) 
c = (sin(x-dx)/(x-dx)^3);              %f(x-dx)

central_difference = (b-c)/(2*dx);

%error
central_error = abs(central_difference - Analytical_derivative);

end

FOURTH ORDER APPROXIMATION

%%

function error = Fourth_order_approximation(x,dx)

Analytical_derivative = ((cos(x))/x^3)- ((3*sin(x))/x^4);

%Numerical derivative 
b = (sin(x+dx)/(x+dx)^3);              %f(x+dx) 
c = (sin(x-dx)/(x-dx)^3);              %f(x-dx)
d = (sin(x-2*dx)/(x-2*dx)^3);          %f(x-2*dx)
e = (sin(x+2*dx)/(x+2*dx)^3);          %f(x+2*dx)

Fourth_order_approx = ( d-8*c+8*b-e)/(12*dx);

%error
error = abs(Fourth_order_approx - Analytical_derivative);

end

PROCEDURE:

  • First, the analytical function we considered is `f(x) = sin(x)/(x^3)`, then the analytical derivative is calculated for it.
  • The forward difference, central difference, and fourth order approximations of f(x) are formulated.
  • The error value is formulated by dividing the above appoximations with analytical derivative.
  • These approximations are taken as functions and are called in the main program to calculate the errors over a given range of dx.
  • For `x = pi/3;` and `dx = pi/40 to pi/4000` 10 values between them and by using for loop, we call the functions computed for 1st, 2nd, and 4th order approximations.
  • Then, plotting is done by using log log graphs as the errors are very small.
  • Plotting is done over dx and approximations.
  • Then, the slopes are calculated.

PLOTS:

RESULTS:

SLOPES

First order - 2.2700

Second order - 0.2539

Fourth order - 0.0076

  • The slope value decreases as the order increases which shows that the distance betweeen the points decreases with increase in the order
  • Error decreases with increase in order.
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.