Project

Comparision of the first second and fourth order approximations of the first derivative against the analytical solution for various values of dx

Objective: The objective of the present assignment is to find out the numerical derivative of the given function by the use of first order, second order, and fourth order approximations at various values of dx and compare the results obtained with the actual derivative.

In this assignment I have computed the analytical derivative and found out the error between the analytical numerical derivative for various values of dx

where dx=linspace(0.5,0.01,20).

Given function:

Exact Solution:

Numerical Approximations:

The Exact and numerical Solutions of the given function is calculated at x=pi/3 with dx=linspace(0.5,0.01,20).

Here is the code:

clear all
close all
clc
%f(x)=(sin(x)/(x^3));
x=pi/3;
dx=linspace(0.5,0.01,20)
exact_derivative=((cos(x)*(x^3))-(sin(x)*(3*(x^2))))/(x^6)
for i=1:length(dx)
    firstorder_derivative(i)=((sin(x+dx(i))/(x+dx(i))^3)-(sin(x)/(x^3)))/dx(i)
    secondorder_derivative(i)=((sin(x+dx(i))/((x+dx(i))^3))-(sin(x-dx(i))/((x-dx(i))^3)))/(2*dx(i))
    fourthorder_derivative(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))
    %estimation of errors
    error_firstorder(i)=abs((firstorder_derivative(i))-(exact_derivative))
    error_secondorder(i)=abs((secondorder_derivative(i))-(exact_derivative))
    error_fourthorder(i)=abs((fourthorder_derivative(i))-(exact_derivative))
end
    %plotting the results
    figure(1)
    plot(dx,error_firstorder,\'-g\',\'LineWidth\',2)
    hold on
    plot(dx,error_secondorder,\'-b\',\'LineWidth\',2)
    plot(dx,error_fourthorder,\'-r\',\'LineWidth\',2)
    hold off  
    axis([0 0.6 0 1])
    legend(\'first order approx\',\'second order approx\',\'fourth order approx\',\'location\',\'southeast\')
    xlabel(\'Step Size dx\')
    ylabel(\'Absolute value of Error\')
    title(\'Effect of Step Size on the errors\') 
    saveas(gca,\'effect of dx final2.jpeg\')
    
    figure(2)

   loglog(dx,error_firstorder,\'-g\',\'LineWidth\',2)
   hold on
   grid on
   loglog(dx,error_secondorder,\'-b\',\'LineWidth\',2)
   loglog(dx,error_fourthorder,\'-r\',\'LineWidth\',2)
   legend(\'first order approx\',\'second order approx\',\'fourth order approx\',\'location\',\'southeast\')
    xlabel(\'Step Size dx\')
    ylabel(\'Absolute value of Error\')
    title(\'Effect of Step Size on the errors\')
    saveas(gca,\'loglogplot2.jpeg\')

Here are the plots:

The grahs shows the absolute errors between the numerical and analytical derivatives for various values of dx. As the value of dx increases associated errors also keeps on increasing

Since the errors between dx values of 0 and 0.1 are not clearly seen a loglog plot of the same graph is done and it is as shown in the below figure

Here is the link to the m file and the graphs obtained:

https://drive.google.com/open?id=1WTUX1Pw0hXwrZT2ZFWWth_SGcBcL-N3Y

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.