Project

A program that compares the first, second and fourth order approximations of the first derivative against the analytical or exact derivative

AIM : A program that compares the first, second and fourth order approximations of the first derivative against the analytical or exact derivative

Taylor series expansion for f(x+a)

`f(x+a) = f(x)+(f'(x))/(1!)Deltax+(f''(x))/(2!)Deltax^2+(f'''(x))/(3!)Deltax^3+.....`

`f(x+a) -f(x)=(f'(x))/(1!)Deltax+(f''(x))/(2!)Deltax^2+(f'''(x))/(3!)Deltax^3+.....`

`(f(x+a) -f(x))/(Deltax)=f'(x)+(f''(x))/(2!)Deltax+(f'''(x))/(3!)Deltax^2+.....`

So, neglecting terms of the order OD(`Deltax`)

so, the resulting equation will be of the error of first order

`(f(x+a) -f(x))/(Deltax)=f'(x)`

as it takes values from forward direction it is called forward difference

Taylor series expansion for f(x-a)

`f(x-a) = f(x)-(f'(x))/(1!)Deltax+(f''(x))/(2!)Deltax^2-(f'''(x))/(3!)Deltax^3+.....`

`f(x+a) -f(x)=-(f'(x))/(1!)Deltax+(f''(x))/(2!)Deltax^2-(f'''(x))/(3!)Deltax^3+.....`

`(f(x+a) -f(x))/(Deltax)=-f'(x)+(f''(x))/(2!)Deltax-(f'''(x))/(3!)Deltax^2+.....`

So, neglecting terms of the order OD(`Deltax`)

so, the resulting equation will be of the error of first order

`(f(x+a) -f(x))/(Deltax)=-f'(x)`

as it takes values from backward direction it is called backward difference

Now we will just subtract the taylor series expansion of f(x+a) and f(x-a)

so, we get

`f(x+a)-f(x-a)=2f'(x)Deltax+2f'''(x)Deltax^3+...`

Dividing by `Deltax`

`(f(x+a)-f(x-a))/(Deltax)=2f'(x)+(2f'''(x)Deltax^2)/(3!)+...`

So, neglecting terms of the order OD(`Deltax^2`)

`(f(x+a)-f(x-a))/(2Deltax)=f'(x)`

Fourth order

`(f(x-2a)-8f(x-a)+8f(x+a)-f(x+2a))/(12Deltax)=f'(x)`

So, using these equations we will write the MATLAB program and compute the error

% A program that compares the first, second and fourth order approximations
% of the first derivative against the analytical or exact derivative

clc % clears the command window
clear % clears the workspace
close all % closes all the windows opened by matlab

% values at which function needed to be found
x=pi/3; 
dx=pi/30;

% Main function
f_x=sin(x)/x^3; 

% Taylor variables f(x+dx),f(x-dx),f(x+2dx),f(x-2dx)
f_xplusdx=sin(x+dx)/(x+dx)^3;
f_xminusdx=sin(x-dx)/(x-dx)^3;
f_xplus2dx=sin(x+2*dx)/(x+2*dx)^3;
f_xminus2dx=sin(x-2*dx)/(x-2*dx)^3;

% Analytically or exact derivative
fdash_x=cos(x)/x^3-3*sin(x)/x^4;

% Numerically 

% first, order approximations of the first derivative
forward_diff=(f_xplusdx-f_x)/dx; 
error1=forward_diff-fdash_x;

% second order approximations of the first derivative
central_diff=(f_xplusdx-f_xminusdx)/(2*dx);
error2=central_diff-fdash_x;

% fourth order approximations of the first derivative
fourth_order=(f_xminus2dx-8*f_xminusdx+8*f_xplusdx-f_xplus2dx)/(12*dx);
error4=fourth_order-fdash_x;

% storing the absolute values of error in a array for plotting
yy=[abs(error1) abs(error2) abs(error4)];

bar(yy,0.4) % Plotting bar graph with bar width 40%
set(gca,'XTick',1:3,'XTickLabel',{'First','Second','Fourth'})
xlabel('DIFFERNTIATION ORDER') % Labelling X-axis
ylabel('ERROR') % Labelling Y-axis
legend('First Derivative') % Legend


OUTPUT:

Here, we can see that as the order of the approximations increases the error decreases

REFRENCES:-

1. numdiff.pdf (brown.edu)

2. MATLAB HELP

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.