EEE282
Numerical Techniques Laboratory
Lab Report
Section:02 Group No:07
Experiment no:03
Name of the experiment: Numerical Differentiation
All Group members:
Sl. Name ID Signature
1. Md. Wajed Al Zaman 22221074 Wajed
2. Lubana Naware Zakaria 22221088 Lubana
3. Raisha Islam Shuchi 22221092 Raisha
4. Forid Uz Jaman Mim 22221141 Forid
5. Intisar Ahmad 22221111 Intisar
Task 01:
Write a MATLAB code for numerical differentiation using the forward
difference method.
Function Code:
function value = forward_difference(f,x,h,o,i)
%f = derivative function
%x = the value at which the derivative has to be evaluated
%h = divided difference
%o = Order of the derivative
%i = takes the values either 1 or 2; 2 denotes more accurate
results
if(o == 1 && i == 1)
value = (f(x+h)-f(x))/h;
end
Main Code:
clc,clear all,close all,
x=1;
f=inline('exp(x)','x');
h=[1/10 1/100 1/1000 1/10000 1/100000 1/1000000];
f_accurate= inline('exp(x)','x');
accurate = f_accurate(x)
for i=1:length(h)
t=h(i)
differen = forward_difference(f,x,t,1,1)
error = abs((differen-accurate)*100/accurate)
end
Output :
Discussion: We can observe from the result of forward differentiation that
as the value of h increases the more accurate result we get in the last value
where h=10^-6 the error we got is 4.99*10^-5 which is very close to the
accurate value.
Task 02:
Function Code for differentiation:
function value = central_difference(f,x,h,o,i)
%f = derivative function
%x = the value at which the derivative has to be evaluated
%h = divided difference
%o = Order of the derivative
%i = takes the values either 1 or 2; 2 denotes more accurate
results
if(o == 1 && i == 1)
value = (f(x+h)-f(x-h))/(2*h);
end
For base Code:
clc,clear all,close all,
x=1/sqrt(2);
f=inline('sin(cos(1/x))','x');
h=[1/10 1/100 1/1000 1/10000 1/100000 1/1000000];
f_accurate= inline('cos(cos(1/x))*(-sin(1/x))*(-1/x^2)','x');
accurate = f_accurate(x)
for i=1:length(h)
t=h(i)
differen = central_difference(f,x,t,1,1)
error = abs((differen-accurate)*100/accurate)
end
ANS:
Discussion: We can observe from the result that as the value of h increases
the more accurate result we get in the last value where h=10^-6 the error we
got is 3.135*10^-9 which is very close to the accurate value.
Problem 01:
Derivation:
is –
Problem 02:
Forward difference
function value = forward_difference(f,x,h,o,i)
%f = derivative function
%x = the value at which the derivative has to be evaluated
%h = divided difference
%o = Order of the derivative
%i = takes the values either 1 or 2; 2 denotes more accurate
results
if(o == 1 && i == 1)
value = (f(x+h)-f(x))/h;
elseif(o == 1 && i == 2)
value = (-f(x + 2*h) + 4*f(x+h) - 3*f(x))/(2*h);
end
Backward difference
function value = backward_difference(f,x,h,o,i)
%f = derivative function
%x = the value at which the derivative has to be evaluated
%h = divided difference
%o = Order of the derivative
%i = takes the values either 1 or 2; 2 denotes more accurate results
if(o == 1 && i == 1)
value = (f(x)-f(x-h))/h;
elseif(o == 1 && i == 2)
value = (3*f(x) - 4*f(x-h) + f(x-2*h))/(2*h);
end
Central difference
function value = central_difference(f,x,h,o,i)
%f = derivative function
%x = the value at which the derivative has to be evaluated
%h = divided difference
%o = Order of the derivative
%i = takes the values either 1 or 2; 2 denotes more accurate results
if(o == 1 && i == 1)
value = (f(x+h)-f(x-h))/(2*h);
elseif(o == 1 && i == 2)
value = (-f(x + 2*h) + 8*f(x+h) - 8*f(x-h) + f(x-2*h))/(12*h);
end
Problem 03:
Analytical differentiation:
f_analytical = @(x) (sin(3*x) ./ sqrt(x)) + 6*sqrt(x).*cos(3*x) - (2.5 *
exp(-2*x) ./ sqrt(x));
f_analytical_value = f_analytical(3.5);
disp(['Analytical derivative at x = 3.5 is ', num2str(f_analytical_value)]);
Numerical differentiation:
f = @(x) (2*x.*sin(3*x) + exp(-2*x))./sqrt(x);
x0 = 3.5;
h = 0.01;
f_numerical = (f(x0 + h) - f(x0 - h)) / (2*h);
disp(['Numerical derivative at x = ', num2str(x0), ' is ',
num2str(f_numerical)]);
Discussion:
We have solved the problem Analytically and numerically using MATLAB as
calculating tool.We can observe that the answer is around -5.809 analytically
and -5.91 in average numerically.The error is around 0.1 and the answer is
around somewhere -5.9 and hopefully this is more precise value which is
comparatively close to exact value.
Problem 04:
Function Code for Differentiation:
function [dy_dx]= derivative_1st(x,y)
dy_dx=[];
dy_dx_F=(y(2)-y(1))/(x(2)-x(1));
dy_dx=[dy_dx, dy_dx_F];
for i=2:length(x)-1
dy_dx_M=(y(i+1)-y(i-1))/(x(i+1)-x(i-1));
dy_dx=[dy_dx, dy_dx_M];
end
dy_dx_L=(y(end)-y(end-1))/(x(end)-x(end-1));
dy_dx=[dy_dx dy_dx_L];
End
Main Code for Differentiation:
clc,clear all,close all,
y=[0 4 8 12 16 20];
x= [0 34.7 61.8 82.8 99.2 112.0];
derivative_1st(x,y)
Discussion: In this problem there is given a set of points of distance vs time
and to calculate the velocity we need to differentiate each of the points. So we
used the function that differentiates each of the points and called the function
in our main code (where we initialized all the values of s and t to y and x) and
we got velocity for the points respectively.