MATLAB Examples - Mathematics

Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

https://www.halvorsen.

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𝑥 ! + 𝑥 ! + 𝑦 ! + 𝑒 "#(%)

We will test with different values for 𝑥 and 𝑦


We create the function:
function z=calcexpression(x,y)

z=3*x^2 + sqrt(x^2+y^2)+exp(log(x));

Testing the function gives:


>> x=2;
>> y=2;
>> calcexpression(x,y)
ans =
16.8284
Statistics Functions
• MATLAB has lots of built-in functions for Statistics
• Create a vector with random numbers between 0 and 100.
Find the following statistics: mean, median, standard deviation,
minimum, maximum and the variance.
>> x=rand(100,1)*100;

>> mean(x)
>> median(x)
>> std(x)
>> mean(x)
>> min(x)
>> max(x)
>> var(x)
Trigonometric functions

sin(𝑥) cos(𝑥) tan(𝑥)


Trigonometric functions
It is quite easy to convert from radians to degrees or from degrees to radians.
We have that:
2𝜋 𝑟𝑎𝑑𝑖𝑎𝑛𝑠 = 360 𝑑𝑒𝑔𝑟𝑒𝑠𝑠
This gives:
180
𝑑 𝑑𝑒𝑔𝑟𝑒𝑒𝑠 = 𝑟[𝑟𝑎𝑑𝑖𝑎𝑛𝑠] 1
𝜋
𝜋
𝑟[𝑟𝑎𝑑𝑖𝑎𝑛𝑠] = 𝑑[𝑑𝑒𝑔𝑟𝑒𝑒𝑠] 1
180

→ 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;

Testing the functions:

>> 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 𝐴 = , 𝐴 = 𝑎𝑟𝑐𝑡𝑎𝑛
𝑏 𝑏

The Pythagoras' theorem:


𝑐 ! = 𝑎! + 𝑏!
The function becomes as follows:
function angleA = right_triangle(x,y, Testing the function:
type)
>> a=5
switch type a =
case 'sin' 5
angleA=asin(x/y); >> b=8
case 'cos' b =
angleA=acos(x/y); 8
case 'tan' >> c=sqrt(a^2+b^2)
angleA=atan(x/y); c =
end 9.4340
>> right_triangle(a,c,'sin')
% Convert from radians to degrees ans =
angleA = r2d(angleA); 32.0054
>> right_triangle(b,c,'cos')
ans =
32.0054
>> right_triangle(a,b,'tan')
ans =
32.0054
Law of cosines

Given:

Create a function where you find c using the law of cosines.

𝑐 # = 𝑎# + 𝑏 # − 2𝑎𝑏 𝑐𝑜𝑠𝐶
The function becomes as follows:
function c = law_of_cosines(a,b,C)

c = sqrt(a^2 + b^2 - 2*a*b*cos(C));

Testing the function:


>> a=2;, b=3;, C=pi;,

>>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

1 Rectangular form of a complex number 2 Exponential/polar form of a complex number


𝐼𝑚𝑎𝑔𝑖𝑛𝑎𝑟𝑦 𝐼𝑚𝑎𝑔𝑖𝑛𝑎𝑟𝑦
𝐴𝑥𝑖𝑠 (𝐼𝑚) 𝐴𝑥𝑖𝑠 (𝐼𝑚)

𝑏 𝑧 = 𝑎 + 𝑗𝑏 𝑧 = 𝑟𝑒 !"
𝑏
𝑟
𝑅𝑒𝑎𝑙
𝜃
𝑅𝑒𝑎𝑙
0 𝑎 𝐴𝑥𝑖𝑠(𝑅𝑒) 𝑎 𝐴𝑥𝑖𝑠(𝑅𝑒)
0

Length (“Gain”): Angle (“Phase”):


𝑏
𝑟= 𝑧 = 𝑎! + 𝑏! 𝜃 = 𝑎𝑡𝑎𝑛
𝑎
Rectangular form → Exponential/polar form
Given the complex numbers (Rectangular form ):
𝑧 = 𝑎 + 𝑗𝑏
Exponential/polar form:
𝑧 = 𝑟𝑒 '(
z = a + bj
Where r = sqrt(a^2 + b^2)

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.

Given the complex numbers:


𝑧! = 𝑟! 𝑒 "#$ and 𝑧$ = 𝑟$ 𝑒 "#%
Multiplication:

𝑧% = 𝑧! 𝑧$ = 𝑟! 𝑟$ 𝑒 "(#$ '#% )
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

complex modulus (magnitude) of the elements of X. >>abs(z)


Phase angle. angle(z) returns the phase angles, in radians >>z=2+4i
angle
>>angle(z)
Complex imaginary part. imag(z) is the imaginary part of z. >>z=2+4i
imag
>>b=imag(z)
real Complex real part. real(z) is the real part of z. >>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;

the complex result A + Bi >>b=3;

>>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)

% r and angle + complex conungate


r=abs(z) disp('c/d')
theta=angle(z) %Directly-------------
complconj=conj(z)
z = c/d
disp('c-d') %Manually-------------
%Directly-------------- z_abs = abs(c)/abs(d);
z = c - d z_angle = angle(c) - angle(d);
%Manually-------------- z_real = z_abs*cos(z_angle);
z_real = real(c) - real(d); z_imag = z_abs*sin(z_angle);
z_imag = imag(c) - imag(d); z = complex(z_real,z_imag)
z = complex(z_real,z_imag)
%or: z = z_real + z_imag*i
This gives: c+d
z =
6.0000 + 2.0000i
z =
6.0000 + 2.0000i
r =
6.3246
theta =
0.3218
complconj =
6.0000 - 2.0000i
c-d
z =
4.0000 + 4.0000i
z =
4.0000 + 4.0000i
c*d
z =
8.0000 - 2.0000i
z =
8.0000 - 2.0000i
c/d
z =
1.0000 + 4.0000i
z =
1.0000 + 4.0000i
Complex Roots
Find the roots of the equation:
𝑥 ! + 4𝑥 + 13

The roots are given by:


𝑥 ! + 4𝑥 + 13 = 0

Find also the Sum and Difference of the roots.


We can e.g., use the solveeq function we created in a previous example:
function x = solveeq(a,b,c) a=1;
if a~=0
x = zeros(2,1); b=4;
x(1,1)=(-b+sqrt(b^2-4*a*c))/(2*a); c=13;
x(2,1)=(-b-sqrt(b^2-4*a*c))/(2*a);
elseif b~=0
x=-c/b; solveeq(a,b,c)
elseif c~=0
disp('No solution')
else
disp('Any complex number is a solution')
end

Or we can use the root function: a=1;


b=4;
c=13;

p=[a,b,c]
roots(p)
Note! the solution is complex conjugate.

The sum of two complex conjugate numbers is always real.


In our case:
ans =
-4

While the difference is imaginary (no real part).


ans =
0 + 6.0000i
Polynomials
A polynomial is expressed as:
𝑝 𝑥 = 𝑝) 𝑥 + + 𝑝! 𝑥 +,) + ⋯ + 𝑝+ 𝑥 + 𝑝+-)
where 𝑝) , 𝑝! , 𝑝* , … are the coefficients of the polynomial.

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 the roots of the polynomial (𝑝 𝑥 = 0)

→ Find 𝑝 𝑥 = 2

Use the polynomial functions listed above.


MATLAB Code:
p = [-2.1, 2, 0, 5, 11]

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.

The answers shall then of course be 0 (or at least a very


small number).
Polynomials
Given the following polynomials:
𝑝) 𝑥 = 1 + 𝑥 − 𝑥 !
𝑝! 𝑥 = 2 + 𝑥 *
→ Find the polynomial 𝑝(𝑥) = 𝑝) (𝑥) N 𝑝! (𝑥) using MATLAB and
find the roots
→ Find the roots of the polynomial (𝑝 𝑥 = 0)
→ Find 𝑝 𝑥 = 2
→ Find the differentiation/derivative of 𝑝! 𝑥 , i.e., 𝑝! /
We will use the polynomial functions listed above.
Note!
The polynomials may be rewritten as:
𝑝) 𝑥 = −𝑥 ! + 𝑥 + 1
𝑝! 𝑥 = 𝑥 * + 0𝑥 ! + 0𝑥 + 2
The MATLAB code becomes:
P1 = [-1, 1, 1]; This gives:
P2 = [1, 0, 0, 2];
p =
p = conv(p1,p2) -1 1 1 -2 2 2
r =
r = roots(p)
1.6180
polyval(p,2) 0.6300 + 1.0911i
0.6300 - 1.0911i
-1.2599
-0.6180
ans =
-10
The Polynomial becomes:

𝑝 𝑥 = −𝑥 * + 𝑥 + + 𝑥 , − 2𝑥 # + 2𝑥 + 2
Polynomial Fitting
Find the 6.order Polynomial that best fits the following function:
𝑦 = sin(𝑥)

→ Plot both the function and the 6. order Polynomial to compare


the results.
x=0:0.1:2*pi;
y=sin(x);

figure(1)
plot(x,y)

% Finding a 6. order polynomial


p=polyfit(x,y,6)

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

You might also like