Discretization Basics
AIM:
To write a program that compares the first,second and fourth order approximations of the first derivative against the analytical or exact derivative.
OBJECTIVE:
1.The function that to compute if f(x) = sin(x)/x^3
2.To compute the first derivative at x = pi/3 for dx = pi/30.
3.To compute the derivative both numerically and analytically.
4. Define an error parameter as the absolute difference in numerical and exact derivative at x = pi/3
5. Plot a bar graph that shows the error.
GIVEN:
function f(x) = sinx/x^3
first derivative at x=pi/3
Analytical first derivative = (x^3*cosx-3*sinx*x^3)/x^6
Assuming infinitesimal quantity, dx=pi/15
Forward and central differencing methods are used to find the numerical derivative.
First order approximation, forward differencing = [f(x+a)-f(x)]/ΔΔx
Second order approximation, central differencing =[f(x+a)-f(x-a)]/2ΔΔx
Fourth order approximation, central differencing
STEPWISE EXPLANATION:
clear all
close all
clc
%analytical derivative = sin(x)/x^3
% f'(x) = (x^3(cos(x))-sin(x)*3*x^2)/x^6
x = pi/3;
analytical_derivative = (x^3*(cos(x))-sin(x)*3*(x^2)/(x^6));
- Always start the programe with clear all close all and clc command,
- The analytical derivative is given which is stored as f'(x)
- The x value is also given as pi/3
- Now the analytical derivative is caluculated and stored in it
%forward differencing(1st order approximation)
%forward difference approximation = (f(x+dx)-f(x))/dx
%numerical derivative
dx = pi/30
%forward differencing
forward_differencing = ((sin(x+dx)/(x+dx)^3)-(sin(x)/(x)^3))
%(2nd order approximation)
central_differencing=((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx)
% (4th order approximation)
fourth_order_approximation=((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)
- The forward differencing approximation is done using the provided formula
- The derivative dx is given as pi/30 and is stored
- Now the forward differencing is done using the provided formula
- The same procedure is repeated for the central differnecing whereas the formula changes
- And finally the fourth order approximation is done using the given formula.
%errors
error_first_order=abs(forward_differencing - analytical_derivative);
error_second_order=abs(central_differencing - analytical_derivative);
error_fourth_order=abs(fourth_order_approximation - analytical_derivative);
- The absolute value of errors are calucluated for first,second and fourth orders.
% bar graph showing error
error = [error_first_order, error_second_order, error_fourth_order];
bar(error,0.5)
xlabel('Order of approximation')
ylabel('Truncation Error')
legend('order of derivative approximation errors')
- Plotting the bar graph for tranucation error and the order of approximation
- labels and legends are given for the respective variables
FINAL CODE:
clear all
close all
clc
%analytical derivative = sin(x)/x^3
% f'(x) = (x^3(cos(x))-sin(x)*3*x^2)/x^6
x = pi/3;
analytical_derivative = (x^3*(cos(x))-sin(x)*3*(x^2)/(x^6));
%forward differencing(1st order approximation)
%forward difference approximation = (f(x+dx)-f(x))/dx
%numerical derivative
dx = pi/30
%forward differencing
forward_differencing = ((sin(x+dx)/(x+dx)^3)-(sin(x)/(x)^3))
%(2nd order approximation)
central_differencing=((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx)
% (4th order approximation)
fourth_order_approximation=((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)
%errors
error_first_order=abs(forward_differencing - analytical_derivative);
error_second_order=abs(central_differencing - analytical_derivative);
error_fourth_order=abs(fourth_order_approximation - analytical_derivative);
% bar graph showing error
error = [error_first_order, error_second_order, error_fourth_order];
bar(error,0.5)
xlabel('Order of approximation')
ylabel('Truncation Error')
legend('order of derivative approximation errors')
OUTPUT:

On X axis 1 is represented as first order approximation,2 as second order approximation and fourth order approximation respectively.

RESULT:
As it is observed that on increasing the order of derivative the error is decreasing from 1st order derivative to Fourth order derivative.