MATLAB Examples - Mathematics
MATLAB Examples - Mathematics
MATLAB Examples - Mathematics
blog
Mathematics
with MATLAB
Hans-Petter Halvorsen
Mathematics with MATLAB
• MATLAB is a powerful tool for mathematical calculations.
• Type “help elfun” (elementary math functions) in the
Command window for more information about basic
mathematical functions.
Mathematics Topics
• Basic Math Functions and Expressions
𝑧 = 3𝑥 ! + 𝑥 ! + 𝑦 ! + 𝑒 "#(%)
• Statistics
– mean, median, standard deviation, minimum, maximum and variance
• Trigonometric Functions
sin() , cos() , tan()
• Complex Numbers
𝑧 = 𝑎 + 𝑗𝑏
• Polynomials
𝑝 𝑥 = 𝑝! 𝑥 " + 𝑝# 𝑥 "$! + ⋯ + 𝑝" 𝑥 + 𝑝"%!
Basic Math Functions
Create a function that calculates the following mathematical
expression:
𝑧 = 3𝑥 ! + 𝑥 ! + 𝑦 ! + 𝑒 "#(%)
z=3*x^2 + sqrt(x^2+y^2)+exp(log(x));
>> mean(x)
>> median(x)
>> std(x)
>> mean(x)
>> min(x)
>> max(x)
>> var(x)
Trigonometric functions
→ Create two functions that convert from radians to degrees (r2d(x)) and from
degrees to radians (d2r(x)) respectively.
Test the functions to make sure that they work as expected.
The functions are as follows:
function d = r2d(r)
d=r*180/pi;
function r = d2r(d)
r=d*pi/180;
>> r2d(2*pi)
ans =
360
>> d2r(180)
ans =
3.1416
Trigonometric functions
Given right triangle:
• Create a function that finds the angle A (in degrees) based on input
arguments (a,c), (b,c) and (a,b) respectively.
• Use, e.g., a third input “type” to define the different types above.
• Use your previous function r2d() to make sure the output of your
function is in degrees and not in radians.
• Test the functions to make sure it works properly.
Trigonometric functions
We have that:
𝑎 𝑎
sin 𝐴 = , 𝐴 = 𝑎𝑟𝑐𝑠𝑖𝑛
𝑐 𝑐
𝑏 𝑏
cos 𝐴 = , 𝐴 = 𝑎𝑟𝑐𝑐𝑜𝑠
𝑐 𝑐
𝑎 𝑎
tan 𝐴 = , 𝐴 = 𝑎𝑟𝑐𝑡𝑎𝑛
𝑏 𝑏
Given:
𝑐 # = 𝑎# + 𝑏 # − 2𝑎𝑏 𝑐𝑜𝑠𝐶
The function becomes as follows:
function c = law_of_cosines(a,b,C)
>>law_of_cosines(a,b,C)
ans =
5
Plotting Trigometric functions
• Plot 𝑠𝑖𝑛(𝜃) and 𝑐𝑜𝑠(𝜃) for 0 ≤ 𝜃 ≤ 2𝜋 in the same plot.
• Make sure to add labels and a legend, and use different line
styles and colors for the plots.
clf Or we can use Subplots:
x=0:0.01:2*pi;
% Define x-values
x=0:0.01:2*pi;
plot(x, sin(x), 'c+:')
hold on % subplot 1
subplot(2,1,1)
plot(x, cos(x), 'r:')
hold off
plot(x, sin(x))
title('Plotting sin(x)')
legend('sin', 'cos') xlabel('x')
xlabel('x') ylabel('sin(x)')
ylabel('f(x)')
% Subplot 2
subplot(2,1,2)
clf
plot(x, cos(x))
x=0:0.01:2*pi; title('Plotting cos(x)')
xlabel('x')
plot(x, sin(x), x, cos(x)) ylabel('cos(x)')
Complex Numbers
A Complex Number is given by:
𝑧 = 𝑎 + 𝑗𝑏
Where
𝑗 = −1
𝐼𝑚𝑎𝑔𝑖𝑛𝑎𝑟𝑦
𝐴𝑥𝑖𝑠 (𝐼𝑚)
We have that:
𝑎 = 𝑅𝑒 𝑧
𝑏 𝑧 = 𝑎 + 𝑗𝑏
𝑏 = 𝐼𝑚(𝑧)
𝑅𝑒𝑎𝑙
0 𝑎 𝐴𝑥𝑖𝑠(𝑅𝑒)
Complex Numbers 𝑗 = −1
Polar form:
𝑧 = 𝑟𝑒 !"
Where:
𝑟= 𝑧 = 𝑎#+𝑏 #
𝑏
𝜃 = 𝑎𝑡𝑎𝑛
𝑎 𝐼𝑚𝑎𝑔𝑖𝑛𝑎𝑟𝑦
𝐴𝑥𝑖𝑠 (𝐼𝑚)
Note!
𝑎 = 𝑟 cos 𝜃 𝑏 𝑧 = 𝑟𝑒 !"
𝑟
𝑏 = 𝑟 sin 𝜃 𝜃
𝑅𝑒𝑎𝑙
0 𝑎 𝐴𝑥𝑖𝑠(𝑅𝑒)
Complex Numbers 𝑗 = −1
𝑏 𝑧 = 𝑎 + 𝑗𝑏 𝑧 = 𝑟𝑒 !"
𝑏
𝑟
𝑅𝑒𝑎𝑙
𝜃
𝑅𝑒𝑎𝑙
0 𝑎 𝐴𝑥𝑖𝑠(𝑅𝑒) 𝑎 𝐴𝑥𝑖𝑠(𝑅𝑒)
0
z = a + bj
𝑟 = 𝑧 = 𝑎! + 𝑏! or: r = abs(z)
z = a + bj
𝑏 theta = atan(b/a)
𝜃 = 𝑎𝑡𝑎𝑛
𝑎 z = a + bj
or: theta = angle(z)
Rectangular form → Exponential/polar form
clear
clc
a = 5;
b = 3;
𝑧 = 5 + 𝑗3
% Rectangular Form:
z = a + b*i
% Polar Form:
r = sqrt(a^2 + b^2)
r = abs(z)
theta = atan(b/a)
theta = angle(z)
𝑧 = 𝑟𝑒 &' = 5.83𝑒 &(.*+
z = r*exp(j*theta)
𝑧 = 5 + 𝑗3
Complex Numbers
To add or subtract two complex numbers, we simply add (or
subtract) their real parts and their imaginary parts.
Given the complex numbers:
𝑧) = 𝑎) + 𝑗𝑏) and 𝑧! = 𝑎! + 𝑗𝑏!
Addition:
𝑧* = 𝑧) + 𝑧! = 𝑎) + 𝑎! + 𝑗 𝑏) +𝑏!
Subtraction:
𝑧* = 𝑧) −𝑧! = 𝑎) − 𝑎! + 𝑗 𝑏) −𝑏!
Complex Numbers
In Division and multiplication, we use the polar form.
𝑧% = 𝑧! 𝑧$ = 𝑟! 𝑟$ 𝑒 "(#$ '#% )
Division:
𝑧! 𝑟! 𝑒 "#$ 𝑟! "(# )# )
𝑧% = = = 𝑒 $ %
𝑧$ 𝑟$ 𝑒 "#% 𝑟$
Complex Numbers – MATLAB Functions
Function Description Example
Imaginary unit. As the basic imaginary unit SQRT(-1), i and j are used to enter >>z=2+4i
i, j
complex numbers. For example, the expressions 3+2i, 3+2*i, 3+2j, 3+2*j and >>z=2+4j
3+2*sqrt(-1) all have the same value.
abs abs(x) is the absolute value of the elements of x. When x is complex, abs(x) is the >>z=2+4i
>>a=real(z)
conj Complex conjugate. conj(x) is the complex conjugate of x. >>z=2+4i
>>z_con=conj(z)
complex Construct complex result from real and imaginary parts. c = complex(a,b) returns >>a=2;
>>z=complex(a,b)
Complex Numbers
Given two complex numbers:
𝑐 = 4 + 𝑗3, 𝑑 = 1 − 𝑗
Find the real and imaginary part of c and d in MATLAB.
→ Use MATLAB to find 𝑐 + 𝑑, 𝑐 − 𝑑, 𝑐𝑑 𝑎𝑛𝑑 𝑐/𝑑.
Use the direct method supported by MATLAB and the specific
complex functions abs, angle, imag, real, conj, complex, etc.
together with the formulas for complex numbers .
→ Find also 𝑟 and 𝜃. Find also the complex conjugate.
c=5+3i; disp('c*d')
d=1-i; %Directly-------------
z = c*d
disp('c+d') %Manually-------------
%Directly-----------------
z_abs = abs(c)*abs(d);
z = c + d
%Manually----------------- z_angle = angle(c) + angle(d);
z_real = real(c) + real(d); z_real = z_abs*cos(z_angle);
z_imag = imag(c) + imag(d); z_imag = z_abs*sin(z_angle);
z = complex(z_real,z_imag) z = complex(z_real,z_imag)
p=[a,b,c]
roots(p)
Note! the solution is complex conjugate.
Example of polynomial:
𝑝 𝑥 = −5.45𝑥 . + 3.2𝑥 ! + 8𝑥 + 5.6
Polynomials in MATLAB
MATLAB represents polynomials as row arrays containing
coefficients ordered by descending powers.
Example:
𝑝 𝑥 = −5.45𝑥 . + 3.2𝑥 ! + 8𝑥 + 5.6
>> p=[-5.45 0 3.2 8 5.8]
p =
-5.4500 0 3.2000 8.0000 5.8000
MATLAB offers lots of functions on polynomials, such as conv, roots, deconv, polyval,
polyint, polyder, polyfit, etc.
→ You should look up these functions in the Help system in MATLAB.
Polynomials
Define the following polynomial in MATLAB:
𝑝 𝑥 = −2.1𝑥 . + 2𝑥 * + 5𝑥 + 11
→ Find 𝑝 𝑥 = 2
roots(p)
x = 2;
polyval(p,x) This gives:
𝑝 𝑥=2 𝑝 𝑥 =0 p =
-2.1000 2.0000 0 5.0000 11.0000
ans =
2.0820
-0.0199 + 1.5193i
-0.0199 - 1.5193i
Instead of using the polyval() function we could -1.0898
of course also found the answer like this: ans =
x = 2; 3.4000
p = -2.1*x^4 + 2*x^3+5*x+11
We can use e.g., the polyval() function to check if the
answers are correct:
>> x = 2.0820;
>> polyval(p,x)
etc.
𝑝 𝑥 = −𝑥 * + 𝑥 + + 𝑥 , − 2𝑥 # + 2𝑥 + 2
Polynomial Fitting
Find the 6.order Polynomial that best fits the following function:
𝑦 = sin(𝑥)
figure(1)
plot(x,y)
y2=polyval(p,x);
figure(2)
plot(x,y2)
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: [email protected]
Web: https://www.halvorsen.blog