MATLAB Programming Lecture 3
MATLAB Programming Lecture 3
Lecture No.“3”
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
2-D Graphics
bar(Y) :draws one bar for each element
SIMATIC HMI
MATLAB
The Human
2.
in Bar
Y. Graphs
Programming
Machine Interface
bar(X,Y) :draws a bar for each element in
Introduction
MATLAB Basics
Y at locations
Vectors & Arrays specified in X
Math. Functions bar(X,Y,width) :draws a bar for each
Polynomials element in Y at
MATLAB plotting locations specified in X
2-D & 3-D Graphics with relative
Flow Control bar width and controls
System analysis the separation of
Curve fitting bars. The default width
Symbolic math.
is 0.8, If width is
Signal processing
1, the bars within a
Function m-file
group touch one
MATLAB Simulink
another. The value of
Final Test
width must be a
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction x=-
MATLAB Basics 2.9:0.2:2.9;
Vectors & Arrays
y=exp(-
Math. Functions
x.*x);
Polynomials
bar(x,y)
MATLAB plotting
Flow Control
System analysis
Curve fitting
x=-
Symbolic math.
2.9:0.2:2.9;
Signal processing y=exp(-x.*x);
Function m-file barh(x,y,0.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
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction
stem(Y) :plots the data sequence Y
MATLAB Basics as stems.
Vectors & Arrays stem(X,Y) :plots X versus the columns
Math. Functions of Y.
Polynomials stem(X,Y,'fill') :specifies whether to color
MATLAB plotting the circle at
2-D & 3-D Graphics the end of the stem.
Flow Control
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction hist(Y) : produces a histogram plot that
MATLAB Basics distributes the
Vectors & Arrays bins along the x-axis between the
Math. Functions
minimum and
Polynomials
maximum values of Y.
MATLAB plotting
hist(Y,x) :returns the distribution of Y
2-D & 3-D Graphics
among length(x)
Flow Control
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
3. Area Graphs
Programming
Machine Interface
Introduction
area(Y) :plots the vector Y and fills the
MATLAB Basics
area beneath
Vectors & Arrays
Math. Functions
each curve.
Polynomials
area(X,Y) :For vectors X and Y, area(X,Y) is
MATLAB plotting
the same as
2-D & 3-D Graphics
plot(X,Y) except that the area
Flow Control
between 0 and Y
System analysis
is filled.
Curve fitting
t=0:0.01:2*
Symbolic math. pi;
Signal processing y=sin(t);
Function m-file area(t,y)
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction fill(X,Y,’ColorSpec’) :fills two-dimensional
MATLAB Basics plot of X
Vectors & Arrays
and Y with the
Math. Functions
color specified by
Polynomials
’ColorSpec’.
MATLAB plotting
Flow Control
x=[-3 0
System analysis
3];
Curve fitting
Symbolic math.
y=[0 3
Signal processing
0];
Function m-file
fill(x,y,'r')
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming clear all, close
Machine Interface
all, clc
Introduction
x=[0 0 1 1]; clear all, close all,
MATLAB Basics
y1=[0 0.5 0.5 clc
Vectors & Arrays
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Design of traffic stop sign:
Programming
Machine Interface t=(1:2:15)*pi/8;
Introduction x=sin(t);
MATLAB Basics y=cos(t);
Vectors & Arrays fill(x,y,'r')
Math. Functions axis off
Polynomials text(-0.55,0,'STOP',…
MATLAB plotting 'Color',[1 1
2-D & 3-D Graphics
1],'Fontsize',70)
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
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction
MATLAB Basics
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
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction pie(X) :draws a pie chart using the data
MATLAB Basics in X. Each
Vectors & Arrays
element in X is represented as a
Math. Functions
slice in the pie
Polynomials
chart.
MATLAB plotting a=[0.5 1.6 1.2 0.8
2-D & 3-D Graphics 2.1];
Flow Control pie(a)
System analysis pause
Curve fitting pie(a,a==.8)
Symbolic math. pause
Signal processing
pie(a,a==max(a))
Function m-file
pause
MATLAB Simulink
pie(a,a==min(a))
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
pie(X,explode) :offsets a slice from the pie.
explode is a 2-D Graphics
SIMATIC HMI
MATLAB
vector or matrix of zeros
The Human
Programming
Machine Interface and nonzeros
Introduction that correspond to X. A
MATLAB Basics
nonzero value
Vectors & Arrays
offsets the corresponding
Math. Functions
slice from the
Polynomials
center of the pie chart,
MATLAB plotting
explode must be
2-D & 3-D Graphics
the same size as X.
Flow Control
System analysis
a=[0.5 1.6 1.2 0.8
Curve fitting
2.1];
Symbolic math.
explode=[0 1 0 0
Signal processing 0]
Function m-file pie(a,explode)
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
4. Radial Graphs
Programming
Machine Interface
Introduction
MATLAB Basics polar(theta,rho) :creates a polar
Vectors & Arrays
coordinate plot of the
Math. Functions
angle theta versus the
Polynomials
radius rho.
MATLAB plotting
Symbolic math.
y=sin(2*t).*cos(2*t
Signal processing
);
Function m-file polar(t,y,'r*:')
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
2-D Graphics
Application: Radiation pattern of
SIMATIC HMI
MATLAB
The Human
Programming
antenna
Machine Interface
This example represent the radiation of
Introduction
MATLAB Basics
bidirectional antenna that called by” figure of
Vectors & Arrays eight” described by:
Math. Functions E=cos((ᴨ/2)*cos(Ө))
Polynomials
MATLAB plotting
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
2-D Graphics
SIMATIC HMI
MATLAB
The Human
5. Direction Graphs
Programming
Machine Interface
comet(y) :displays an animated comet
Introduction
graph of the
MATLAB Basics
2-D Graphics
SIMATIC HMI
MATLAB
The Human
subplot
Creating ( m,n,p)
custom plots: : divides the current figure
Programming
Machine Interface into rectangular
Introduction panes as a matrix of
MATLAB Basics subfigures (mxn) that are
Vectors & Arrays numbered rowwise as p.
Math. Functions Each pane contains
Polynomials
an axes object.
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
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction
MATLAB Basics
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
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction
MATLAB Basics
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
2-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
t =linspace(-
Machine Interface
10,10,100);
Introduction
y1=sin(t);
MATLAB Basics
y2= sinc(t);
Vectors & Arrays
y3 = square(t);
Math. Functions y4 = sawtooth(t);
Polynomials subplot(221)
MATLAB plotting plot(t,y1),grid on
2-D & 3-D Graphics
title('sin wave')
subplot(222)
Flow Control
plot(t,y2),grid on
System analysis
title('sinc wave')
Curve fitting subplot(223)
Symbolic math. plot(t,y3),grid on
Signal processing title('square wave')
Function m-file subplot(224)
MATLAB Simulink
plot(t,y4),grid on
Final Test
title('sawtooth wave')
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
2-D Graphics
subplot('Position',[left bottom width
SIMATIC HMI
height]) :creates an
MATLAB
The Human
Programming
axes at the position
Machine Interface
specified by a four-element
Introduction
vector. left, bottom, width,
MATLAB Basics
and height are in
Vectors & Arrays
normalized coordinates in
Math. Functions the range from 0 to 1
Polynomials
t =linspace(-10,10,100);
y1=sin(t);
MATLAB plotting
y2= sinc(t);
2-D & 3-D Graphics
3-D Graphics
SIMATIC HMI
MATLAB
The Human
1. Line Graphs:
Programming
Machine Interface
Introduction
MATLAB Basics
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
3-D Graphics
mesh(X,Y,Z) :draws a wireframe mesh
SIMATIC HMI
MATLAB
The Human
2. Mesh & surface Graphs:
with color
Programming
Machine Interface
determined by Z so color
Introduction
MATLAB Basics
is proportional
Vectors & Arrays to surface height. If X and
Math. Functions Y are vectors,
Polynomials length(X) = n and
MATLAB plotting length(Y) = m, where
2-D & 3-D Graphics [m,n] = size(Z).
Flow Control
System analysis
[X,Y,Z] =
Curve fitting
peaks(30);
Symbolic math. mesh(X,Y,Z)
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
3-D Graphics
SIMATIC HMI
MATLAB
The Human meshc(X,Y,Z) :draws a wireframe mesh
Programming
Machine Interface
with a contour
Introduction
MATLAB Basics
plot beneath the mesh.
Vectors & Arrays
Math. Functions
Polynomials
MATLAB plotting
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
3-D Graphics
SIMATIC HMI
MATLAB
meshz(X,Y,Z) :draws a curtain plot
The Human
Programming
Machine Interface (i.e., a reference
Introduction plane) around the
MATLAB Basics
mesh.
Vectors & Arrays
Math. Functions
Polynomials
MATLAB plotting
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
3-D Graphics
SIMATIC HMI
MATLAB
surf(X,Y,Z) :creates a shaded surface
The Human
Programming
Machine Interface using Z for the
Introduction color data as well as
MATLAB Basics
surface height.
Vectors & Arrays
Math. Functions
Polynomials
MATLAB plotting
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
3-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
surfc(X,Y,Z) :draws a contour plot
Machine Interface
Introduction
beneath the shaded
MATLAB Basics surface.
Vectors & Arrays
Math. Functions
Polynomials
MATLAB plotting
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
surfl(X,Y,Z) 3-D:create
Graphics
three-dimensional
SIMATIC HMI
MATLAB
shaded
The Human
Programming
Machine Interface surfaces using the
Introduction default direction for
MATLAB Basics the light source and the
Vectors & Arrays
default lighting
Math. Functions
coefficients for the
Polynomials
shading model.
MATLAB plotting
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
3-D Graphics
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
surfnorm(X,Y,Z) :plot a surface and its
Introduction
surface normals.
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
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
2-D Graphics
SIMATIC HMI
MATLAB
Application
The Human
Programming
Machine Interface :
Introduction
xi=-3:3;
MATLAB Basics
3-D Graphics
[X,Y,Z] = sphere(n) :returns the
SIMATIC HMI
MATLAB
The Human
3.coordinates
Area Graphs: of a sphere
Programming
Machine Interface
in three matrices
Introduction
that are (n+1)-by
MATLAB Basics
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
3-D Graphics
SIMATIC HMI
MATLAB
The Human
4. comet3(z)
Animated 3-d :displays
plots: a 3-D comet graph
Programming
Machine Interface of the vector z.
Introduction
comet3(x,y,z) :displays a comet graph of
MATLAB Basics
Flow Control
z=-
System analysis
10*pi:0.01:10*pi;
Curve fitting
x=cos(2*z);
Symbolic math. y=sin(2*z);
Signal processing comet3(x,y,z)
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
stem3(Z) :plots the data sequence Z
as stems that 3-D Graphics
extend from the x-y plane.
MATLAB 5. 3-d data sequence plots:
SIMATIC HMI
The Human
Programming
Machine Interface x and y are
Introduction generated automatically.
MATLAB Basics stem3(X,Y,Z) :plots the data sequence Z
Vectors & Arrays
at values
Math. Functions
specified by X and Y. X, Y,
Polynomials
and Z must all
MATLAB plotting
be vectors or matrices of
2-D & 3-D Graphics
the same size.
Flow Control
stem3(X,Y,Z,'fill') :specifies whether to
System analysis
color the interior
Curve fitting
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
colormap
colormap(map) :sets the colormap to the
SIMATIC HMI
MATLAB
The Human
matrix map.
Programming
Machine Interface Colormap must have
Introduction
values in [0,1].
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
[X,Y,Z] =
Signal processing
peaks(30);
Function m-file
mesh(X,Y,Z)
MATLAB Simulink
colormap([0.5 0.5
Final Test
0.5])
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
colormap
colormap( type ) :sets the current
SIMATIC HMI
MATLAB
The Human colormap to the built-in
Programming
Machine Interface
colormaps as the
Introduction
MATLAB Basics
following:
Vectors & Arrays
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
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