[Link]
blog
Numerical Differentiation
with MATLAB
Hans-Petter Halvorsen
Numerical Differentiation
The derivative of a function 𝑦 = 𝑓(𝑥)
is a measure of how 𝑦 changes with
𝑥.
A numerical approach to the derivative of a function 𝑦 = 𝑓(𝑥) is:
Note! We will use MATLAB in order to find the numeric solution – not the analytic solution
Numerical Differentiation
MATLAB Functions for Numerical Differentiation:
diff()
polyder()
MATLAB is a numerical language and do not perform symbolic
mathematics
... well, that is not entirely true because there is “Symbolic
Toolbox” available for MATLAB.
Numerical Differentiation
Given the following equation:
𝑦 = 𝑥 ! + 2𝑥 " − 𝑥 + 3
#$
• Find analytically (use “pen and paper”).
#%
• Define a vector x from -5 to +5 and use the diff() function to
∆$
approximate the derivative y with respect to x ( ).
∆%
• Compare the data in a 2D array and/or plot both the exact value
#$
of and the approximation in the same plot.
#%
• Increase number of data point to see if there are any difference.
Given the following equation:
𝑦 = 𝑥 ! + 2𝑥 " − 𝑥 + 3
Then we can get the analytically solution:
𝑑𝑦
= 3𝑥 " + 4𝑥 − 1
𝑑𝑥
Symbolic Math Toolbox
We start by finding the derivate of f(x) using the Symbolic Math Toolbox:
clear
clc
syms f(x) This gives:
syms x
dfdt(x) = 3*x^2 + 4*x - 1
f(x) = x^3 + 2*x^2 -x +3
dfdt = diff(f, x, 1)
[Link]
x = -[Link];
% Define the function y(x) Exact Solution
y = x.^3 + 2*x.^2 - x + 3; Numerical Solution
% Plot the function y(x)
plot(x,y)
title('y')
% Find nummerical solution to dy/dx
dydx_num = diff(y)./diff(x);
dydx_exact = 3*x.^2 + 4.*x -1;
dydx = [[dydx_num, NaN]', dydx_exact']
% Plot nummerical vs analytical solution to dy/dx
figure(2)
plot(x,[dydx_num, NaN], x, dydx_exact)
title('dy/dx')
legend('numerical solution', 'analytical solution')
𝑦 = 𝑥 ! + 2𝑥 " − 𝑥 + 3 𝑑𝑦
= 3𝑥 " + 4𝑥 − 1
𝑑𝑥
x = -[Link];
𝑑𝑦
= 3𝑥 " + 4𝑥 − 1
𝑑𝑥
x = -5:0.1:5; x = -5:0.01:5;
Differentiation on Polynomials
Given the following equation:
𝑦 = 𝑥 ! + 2𝑥 " − 𝑥 + 3
Which is also a polynomial. A polynomial can be written on the
following general form: 𝑦 𝑥 = 𝑎# 𝑥 $ + 𝑎% 𝑥 $&% + ⋯ + 𝑎$&% 𝑥 + 𝑎$
'(
• We will use Differentiation on the Polynomial to find
')
From previous we know that the Analytically solution is:
𝑑𝑦
= 3𝑥 " + 4𝑥 − 1
𝑑𝑥
p = [1 2 -1 3];
𝑦 = 𝑥 ! + 2𝑥 " − 𝑥 + 3
polyder(p)
ans = 𝑑𝑦
= 3𝑥 " + 4𝑥 − 1
3 4 -1 𝑑𝑥
We see we get the correct answer
Differentiation on Polynomials
Find the derivative for the product:
3𝑥 " + 6𝑥 + 9 𝑥 " + 2𝑥
We will use the polyder(a,b) function.
Another approach is to use define is to first use the conv(a,b)
function to find the total polynomial, and then use polyder(p)
function.
Try both methods, to see if you get the same answer.
% Define the polynomials
p1 = [3 6 9];
p2 = [1 2 0]; %Note!
ans =
12 36 42 18
% Method 1
polyder(p1,p2) p =
3 12 21 18 0
% Method 2 ans =
p = conv(p1,p2) 12 36 42 18
polyder(p)
As expected, the result are the same for the 2 methods used above.
For more details, see next page.
We have that
𝑝! = 3𝑥 " + 6𝑥 + 9
and
𝑝" = 𝑥 " + 2𝑥
The total polynomial becomes then:
𝑝 = 𝑝! - 𝑝" = 3𝑥 # + 12𝑥 $ + 21𝑥 " + 18𝑥
As expected, the results are the same for the 2 methods used above:
𝑑𝑝 𝑑(3𝑥 # + 12𝑥 $ + 21𝑥 " + 18𝑥)
= = 12𝑥 $ + 36𝑥 " + 42𝑥 + 18
𝑑𝑥 𝑑𝑥
Hans-Petter Halvorsen
University of South-Eastern Norway
[Link]
E-mail: [Link]@[Link]
Web: [Link]