MATLAB Programming Lecture 1
MATLAB Programming Lecture 1
Lecture No.“1”
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction
MATLAB
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
The Language of
Curve fitting Technical Computing
Symbolic math.
Signal processing
Prepared by:
Function m-file
Dr./ Essam Nabil
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
What is MATLAB ?
SIMATIC HMI
MATLAB
The Human
Programming MATLAB :stands for MATrix LABoratory
Machine Interface
Introduction MATLAB® is a high-performance language for
MATLAB Basics
technical computing integrates computation,
Vectors & Arrays
Math. Functions
visualization, and programming in an easy-
Polynomials
to-use environment.
MATLAB plotting
MATLAB® typical uses include:
2-D & 3-D Graphics
Math and computation
Algorithm development
Flow Control
Data acquisition
System analysis
Modeling, simulation, and prototyping
Curve fitting Data analysis, exploration, and visualization
Symbolic math. Scientific and engineering graphics
Signal processing Application development, including graphical user
Function m-file interface building
MATLAB Simulink MATLAB® features a family of add-on
Final Test
application-specific solutions
Faculty of Electronic Engineering
called
Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
What is MATLAB ?
SIMATIC HMI
MATLAB
The Human
Toolboxes: are comprehensive collections
Programming
Machine Interface
of MATLAB functions (M-files) that extend the
Introduction
MATLAB Basics
MATLAB environment to solve particular
Vectors & Arrays classes of problems e.g. Control systems,
Math. Functions Communication, Image processing, Signal
Polynomials processing, Neural networks, Fuzzy logic,
MATLAB plotting Wavelets, Simulation, and many other areas.
2-D & 3-D Graphics In industry, MATLAB is the tool of choice for
Flow Control high-productivity research, development, and
System analysis analysis.
Curve fitting For any information on MATLAB®, contact
Symbolic math.
Mathworks
MATLAB makes coding
Signal processing
Function m-file
www.mathworks.com
fun!
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
MATLAB Basics
ones i.e. A ≠ a
2. Comments can de added to describe
Vectors & Arrays
Math. Functions
specific part or function by using ‘%’
Polynomials
i.e. pause %to stop execution
3. Don’t start variable or file name with
MATLAB plotting
a number.
4. Don’t use MATLAB keywords or
2-D & 3-D Graphics
Flow Control
functions-as variable or file name i.e.
System analysis
if , for, while, case, end, . . . etc
Curve fitting 5. Don’t leave space within the variable
Symbolic math. name and replace it with underscore
Signal processing
‘_’ i.e. user_name = ‘Essam’
Function m-file 6. When saving MATLAB files don’t leave
MATLAB Simulink space within the file name and replace
Final Test itof Electronic
Faculty with underscoreIndustrial
Engineering (_).Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
MATLAB plotting
system memory.
2-D & 3-D Graphics pause :Halt execution temporarily i.e.
Flow Control causes the
System analysis
currently executing M-file to
Curve fitting
stop and wait for
Symbolic math.
Signal processing
you to press any key before
Function m-file
continuing.
MATLAB Simulink keyboard :terminate the control to Input
Final Test from
Faculty keyboard
of Electronic Engineering to Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
>>x=5;
Polynomials >>y=10;
MATLAB plotting
>>x+y
2-D & 3-D Graphics
>>
Flow Control
Signal processing
NaN :The result is not a number.
Function m-file input :To request user input as:
MATLAB Simulink
user_name = input(' Enter
Final Test
your name: ')
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
%e exponential format
Polynomials \n new line character
MATLAB plotting \t tab character
2-D & 3-D Graphics
Example;
Flow Control
>> fprintf( 'Result is %d' , 3 )
System analysis
Result is 3
Curve fitting
Handling Vectors
SIMATIC HMI
MATLAB
The Human
To enter a row vector:
Programming
Machine Interface
Introduction
>> a = [1 2 3 4]; OR >>
MATLAB Basics a = [1,2,3,4];
Vectors & Arrays To enter a column vector:
Math. Functions
>> a = [1;2;3;4];
Polynomials
To enter a regular vector with defined
MATLAB plotting
step size:
2-D & 3-D Graphics
Flow Control
>> vector_name = initial_value: step_size :
System analysis
final_value
Curve fitting
EX.: >>x=0:0.1:1
Symbolic math. x=
Signal processing 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
Function m-file 0.9 1
MATLAB Simulink To enter a regular vector with defined
Final Test No.of Electronic
Faculty of elements:
Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Handling Vectors
SIMATIC HMI
MATLAB To enter a regular logarthimic vector
The Human
Programming
Machine Interface with defined No. of elements:
Introduction
>> vector_name =logspace( initial,final, No.of
MATLAB Basics
Handling Vectors
SIMATIC HMI
MATLAB element-wise operations:
The Human
Programming
Machine Interface >> r1 = [2 5 7 9];
Introduction
>> r2 = [1 3 8 5];
MATLAB Basics
Handling Vectors
SIMATIC HMI
MATLAB
The Human
>> b = min(r2) %to find minimum
Programming
Machine Interface value of vector r2
Introduction b=
MATLAB Basics
1
Vectors & Arrays
Math. Functions
>> b = mean(r2) %to find average
value of vector r2
Polynomials
b=
MATLAB plotting
4.25
2-D & 3-D Graphics
>>z=r2(4)
Flow Control
z=
System analysis
5
Curve fitting
Symbolic math.
>>r2(2)=0 %replace 2nd
element value with 0
Signal processing
Function m-file
r2=
MATLAB Simulink
1 0 8 5
Final Test
>> r2(2)=[ ]
Faculty of Electronic Engineering
%remove 2nd element
Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Handling Vectors
SIMATIC HMI
MATLAB
The Human
Programming
>> x = [1 2 3 4];
Machine Interface
>> plot(x) % Linear 2-D plot of
Introduction
MATLAB Basics
vector x
Vectors & Arrays
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
System analysis
Row 3
Curve fitting
arr(3,2)
Symbolic math.
Row 4
Signal processing
Function m-file
Col 1 Col 2 Col 3 Col 4 Col 5
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Final Test
5 6 7 8
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Function m-file
>> v=[1 2 3];
MATLAB Simulink
>>diag(v)
Final Test
ans= 1 0 0
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
>> sum(A) %returning a row vector of the
sums of each
Polynomials
column of matrix A
MATLAB plotting
ans=
2-D & 3-D Graphics
4 6
Flow Control
>> rank(A) %to find the rank of matrix A
System analysis
ans=
Curve fitting
Symbolic math.
2
Signal processing
Polynomials
ans = 9 7 6
MATLAB plotting
1 3 12
2-D & 3-D Graphics
5 9 4 14 15
Flow Control
>> A(:) 1];
System analysis
ans = >> G(1:3, :)
Curve fitting
1 ans=16 2 3
Symbolic math. 5 13
Signal processing 3 5 11 10
Function m-file 9 8
MATLAB Simulink 9 7 6
Final Test Faculty of Electronic Engineering 12 Electronics and Automatic Control Engineering
Industrial
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Polynomials
0 0 1 >> zeros(2)
MATLAB plotting
>> eye(2,3) ans =
2-D & 3-D Graphics
ans = 0 0
Flow Control
1 0 0 0 0
System analysis
0 1 0 >> zeros(1,4)
Curve fitting
>> ones(3) ans =
Symbolic math. ans = 0 0 0 0
Signal processing 1 1 1 >>rand(n)
Function m-file 1 1 1 >>rand(n,m)
MATLAB Simulink 1 1 1 >>randn(n)
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Curve fitting
Symbolic math.
>>rand(n,m) >> magic(n)
Signal processing
>> rand(2,3) >> magic(4)
Function m-file
ans = ans =
MATLAB Simulink
0.9649 0.9706 16 2 3 13
Final Test 0.4854
Faculty of Electronic Engineering
5 11 10 8
Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
SIMATIC HMI
MATLAB
The Human MATLAB contains all of the standard functions
Programming
Machine Interface
such as sin, cos, log, exp, sqrt, as well as many
Introduction
others.
MATLAB Basics Commonly used constants such as pi, and i or j
Vectors & Arrays
for the square root of -1 (imaginary part), are
Math. Functions also incorporated into MATLAB.
Polynomials
MATLAB plotting
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
MATLAB plotting
asec inverse secant; result in radians
2-D & 3-D Graphics asecd inverse secant; result in degrees
Flow Control
acsc inverse cosecant; result in radians
acscd inverse cosecant; result in degrees
System analysis
acot inverse cotangent; result in radians
Curve fitting acotd inverse cotangent; result in degrees
Symbolic math.
Signal processing
sech hyperbolic secant
csch hyperbolic cosecant
Function m-file
coth hyperbolic cotangent
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
SIMATIC HMI
MATLAB
The Human
Exponential functions:
Programming
Machine Interface exp exponential
Introduction log natural logarithm
MATLAB Basics log10 common (base 10) logarithm
Vectors & Arrays
log2 base 2 logarithm and dissect floating-point
numbers into exponent and mantissa
Math. Functions
pow2 base 2 power and scale floating-point numbers
Polynomials sqrt square root
MATLAB plotting
Math. Functions
SIMATIC HMI
MATLAB
The Human Rounding and remainder
Programming
Machine Interface round round to nearest integer
Introduction fix round toward zero
MATLAB Basics ceil round toward positive infinity
Vectors & Arrays floor round toward negative infinity
Math. Functions
mod modulus after division
rem remainder after division
Polynomials
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Polynomials
SIMATIC HMI
MATLAB
The Human In MATLAB, a polynomial is represented by a
Programming
Machine Interface vector. To create a polynomial in MATLAB,
Introduction simply enter each coefficient of the polynomial
MATLAB Basics into the vector in descending order. For
Vectors & Arrays instance:
Math. Functions
Polynomials ,
MATLAB plotting >>p = [1 3 -15 -2 9]
2-D & 3-D Graphics >>f = [1 0 0 0 1]
Flow Control p=
System analysis f=
Curve fitting 1 3 -15 -2 9
Symbolic math.
1 0 0 0 1
Signal processing
Function m-file
>>roots(p) %calculates the roots of a
MATLAB Simulink
polynomial
Final Test
ans =
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Polynomials
SIMATIC HMI
MATLAB
The Human >> r=[1 2 3];
Programming
Machine Interface
>> P=poly(r) %returns to the polynomial
Introduction
coefficients from roots
MATLAB Basics
P=
Vectors & Arrays
Math. Functions
1 -6 11 -6
Polynomials to multiply and divid two polynomials
MATLAB plotting together:
2-D & 3-D Graphics >>x = [1 2];
Flow Control >> y = [1 4 8];
System analysis >>z = conv(x,y) %polynomial multiplication
Curve fitting z=
Symbolic math. 1 6 16 16
Signal processing >>[q, r] = deconv(z,y) %polynomial division
Function m-file q=
MATLAB Simulink 1 2
Final Test
r = Engineering
Faculty of Electronic Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Polynomials
SIMATIC HMI Symbolic
MATLAB
The Human representation of the polynomial
Programming
Machine Interface whose coefficients are in the numeric vector:
Introduction
>>p= poly2sym([1 3 2])
MATLAB Basics
p=
Vectors & Arrays
Math. Functions
x^2 + 3*x + 2
The default symbolic variable is x. The
Polynomials
required variable symbol can be specified as a
MATLAB plotting
second input argument.
2-D & 3-D Graphics
>>f= poly2sym([1 0 1 -1 2], y)
Flow Control
f=
System analysis
y^4 + y^2 - y + 2
Curve fitting To find the value of a polynomial using the
Symbolic math. polyval function. For example, to find the value
Signal processing of the following polynomial at x=2
Function m-file >>f = [1 0 0 0 1];
MATLAB Simulink >>v = polyval(f , 2)
Final Test vElectronic
Faculty of = Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Polynomials
SIMATIC HMI
MATLAB Polynomial
The Human
Programming
derivative
Machine Interface
Introduction
>>k = polyder(p) %returns the derivative of the
polynomial p.
MATLAB Basics
Symbolic math.
>>a = [3 6 9];
Signal processing >> b = [1 2 0];
Function m-file >>k = polyder( a , b)
MATLAB Simulink k=
Final Test 12
Faculty of Electronic Engineering 36 42 Industrial
18Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Polynomials
SIMATIC HMI Rational
MATLAB
The Human Polynomial derivative
Programming
Machine Interface
[q,d] = polyder(b,a) %returns the derivative of
Introduction
the rational
MATLAB Basics
polynomial quotient
Vectors & Arrays
b/a.
Math. Functions
>>a = [1 3 5];
Polynomials
>>b = [2 4 6];
MATLAB plotting
>>[n,d] = polyder(a,b)
2-D & 3-D Graphics
n=
Flow Control
-2 -8 -2
System analysis
d=
Curve fitting
4 16 40 48 36
Symbolic math.
partial fraction expansion:
Signal processing
Function m-file
>>[r,p,k] = residue( n,d )
MATLAB Simulink
Where n,d vectors that specify the coefficients of the
Final Test numerator and the denominator,
Faculty of Electronic Engineering
r is the column
Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Polynomials
SIMATIC HMI
MATLAB
The Human
Programming
>>n = [ 5 3 -2 7];
Machine Interface
>>d = [-4 0 8 3];
Introduction
MATLAB Basics
>>[r, p, k] = residue(n,d);
Vectors & Arrays r=
Math. Functions -1.4167 -0.6653 1.3320
Polynomials p=
MATLAB plotting 1.5737 -1.1644 -0.4093
2-D & 3-D Graphics k=
Flow Control -1.2500
System analysis Note: to convert the partial fraction expansion
Curve fitting back to polynomial coefficients use;
Symbolic math. >>[n,d] = residue(r,p,k)
Signal processing n = -1.2500 -0.7500 0.5000 -1.7500
Function m-file d = 1.0000 -0.0000 -2.0000 -0.7500
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering