MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University...

71
MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung Summer School 17th - 30th July 2013

Transcript of MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University...

Page 1: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

MATLAB and Simulink

Prof. Dr. Ottmar Beucher

University of Applied Sciences KarlsruheGermany

National Chung Hsing University, TaichungSummer School 17th - 30th July 2013

Page 2: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

What is MATLAB ?

What is MATLAB ?

A tool for numerical calculation and simulation

a higher programming language

a interpreted programming language

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 1

Page 3: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Elementary MATLAB Operations

Elementary MATLAB operations:

arithmetic operations

logical operations

mathematical functions

graphics functions

I/O-operations (data exchange)

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 2

Page 4: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

The MATLAB User Interface (since Release 2012b)

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 3

Page 5: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Defining Numerical Arrays (MATrices)

�rowvector = [1 5 −3 ]

rowvector =

1 5 −3�

columnvector = [2 ; 4 ; 3 ; −1; 1−4∗ j ]

columnvector =

2.00004.00003.0000−1.0000

1.0000 − 4.0000 i�

aMatrix = [3 1+2∗ i 2 ; 4 0 −5 ]

aMatrix =

3.0000 1.0000 + 2.0000 i 2.00004.0000 0 −5.0000

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 4

Page 6: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Defining Numerical Arrays (MATrices)

�% appending a row vec to r ( note : semicolon )�aMatrix = [ aMatrix ; 1 2 3 ] �

% appending a row vec to r�aMatrix = [ aMatrix ; rowvector ]�

% appending a column vec to r ( note : comma)�v = [ 1 ; 2 ; 3 ] ; �aMatrix = [ aMatrix , v ]�

% access to a component�element23 = aMatrix ( 2 ,3 )�

% s e t t i n g to a component�aMatrix ( 2 , 3 ) = 2 5�

% c a n c e l l i n g a row vec to r�aMatrix ( 1 , : ) = [ ] �

% c a n c e l l i n g a column vec to r�aMatrix ( : , 2 ) = [ ] �

% BUT�aMatrix ( 2 , 3 ) = [ ]

Subscripted assignment dimension mismatch .

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 5

Page 7: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Defining Numerical Arrays (MATrices) - Interactive Access

The Variables Window:

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 6

Page 8: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

MATLABs Memory Stack - The ”Workspace”

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 7

Page 9: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

1 Within MATLAB define the following matrices and vectors and save them incorresponding variables:

M =

1 0 0

0 j 1

j j + 1 −3

,

k = 2.75, ~v =

1

3

−7

−0,5

,

~w =(

1 −5.5 −1.7 −1.5 3 −10.7),

~y =(

1 1.5 2 2.5 · · · 100.5).

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 8

Page 10: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

2 Consider the matrix M of the previous exercise:

Enlarge matrix M to a 6× 6-matrix V, such that

V =

M M

M M

.

Cancel from matrix V the 2nd row and the 3rd column.Save the 4th row of matrix V in a new vector r4.Change component (4, 2) of matrix V to j + 5.

3 Cancel all variables of the workspace and reconstruct matrix V afterwards usingthe automatically saved definition commands for V and the ↑- and ↓-keys.Additionally try to reconstruct other variables using the Command HistoryWindow.

4 Overwrite the 5th row of matrix V with zeros using the Variables Window.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 9

Page 11: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Arithmetic Operations

Matrix algebra operations: M ·N, M + N, λ ·M

�M = [ 1 2 3 ; 4 − 1 2 ] ; % def ines 2 x3−mat r i x M�N = [ 1 2 −1 ; 4 −1 1 ; 2 0 1 ] % def ines 3 x3−mat r i x N�

V = M∗N % product M∗N ALLOWED !

V =

15 0 44 9 −3�

W = N∗M % product N∗M NOT ALLOWED !??? Error using ==> mtimes Inner matrix dimensions must agree .

�K = [1 5 3 ; 2 −1 −2 ] ; % def ines another 2 x3−mat r i x K�W = N+M ; % sums N+M, M+N ALLOWED !

�lambda = 2 ; % def ines a sca la r value ( 1 x1−mat r i x )�V = lambda∗M ; % product lambda∗M, M∗lambda ALLOWED !

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 10

Page 12: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Arithmetic Operations

Field operations: M ·N, Mk, M/N

�M = [ 1 2 3 ; 4 − 1 2 ] ; % def ines 2 x3−mat r i x M�N = [ 1 2 −1 ; 4 −1 1 ; 2 0 1 ] ; % def ines 3 x3−mat r i x N�

V = M.∗N % f i e l d product M.∗N NOT ALLOWED !Error using .∗Matrix dimensions must agree .�

K = [1 5 3 ; 2 −1 −2 ] ; % def ines another 2 x3−mat r i x K�V = M.∗K % f i e l d products M.∗K , K.∗M ALLOWED !

V =

1 10 98 1 −4�

k = 3 ; % def ines a sca la r value ( 1 x1−mat r i x )�M. ˆ k ; % f i e l d powers ALLOWED !

�W = M . / K % f i e l d d i v i s i o n s ALLOWED !

W =

1.0000 0.4000 1.00002.0000 1.0000 −1.0000

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 11

Page 13: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

5 Solve the following problems:

Calculate using matrix- or/and field-operations the standard scalar product of the vectors

~x =(

1 2 12 −3 −1

)and ~y =

(2 0 −3 1

3 2).

Calculate the product of the matrices

A =

−1 3.5 2

0 1 −1.3

1.1 2 1.9

and B =

1 0 −1

−1.5 1.5 −3

1 1 1

.

Starting from

A =

−1 3.5 2

0 1 −1.3

1.1 2 1.9

calculate the matrix

C =

−1 0 0

0 1 0

0 0 1.9

using appropriate field operations!

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 12

Page 14: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Logical Operations

Operations:

and (&)

or (|)

negation (∼)

exclusive Or (xor)

always Field operatorsresult: logical values, logical arrays

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 13

Page 15: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Logical Operations (Examples)

�A=[1 −3 ;0 0 ]

A =

1 −30 0�

B= [ 0 5 ; 0 1 ]

B =

0 50 1�

Res=A&B % Log ica l AND

Res =

0 10 0

�Res=A|B % Log ica l OR

Res =

1 10 1�

Res=xor (A,B ) % Exc lus ive OR

Res =

1 00 1�

Res=˜B % Negation

Res =

1 01 0

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 14

Page 16: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Relational Operations

Operations:

greater than (>)

less than (<)

greater or equal (>=)

less or equal (<=)

equal (==)

not equal (∼=)

always Field operatorsresult: logical values, logical arrays

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 15

Page 17: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Logical Operations (Examples)

�A=[1 −3 ;0 0 ]

A =

1 −30 0�

B= [ 0 5 ; 0 1 ]

B =

0 50 1�

Res=A>B % grea te r

Res =

1 00 0

�Res=A<=B % less or equal

Res =

0 11 1�

Res=A==B % equal

Res =

0 01 0�

Res=A˜=B % not equal

Res =

1 10 1

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 16

Page 18: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Logical and Numerical Indexing

Two types of indexing:

with integer values

�A=[1 −3 ;0 0 ]

A =

1 −30 0�

a = A( 2 , 1 ) % access to element ( 2 , 1 )

a =

0�

z = A ( 1 : 2 , 1 ) % access to elements% ( 1 , 1 ) and ( 2 , 1 )

z =

10

with logical values

�vect =[−2 , 3 , −4 , 5 , 1]

vect =

−2 3 −4 5 1�

select = vect>2 % check elements > 2

select =

0 1 0 1 0�

whosName Size Bytes Class

select 1x5 5 log ica lvect 1x5 40 double�

pick = vect ( select ) % s e l e c t by l o g i c a l% index ing

pick =

3 5

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 17

Page 19: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

6 Consider the following matrix:

C =

1 2 3 4 10

−22 1 11 −12 4

8 1 6 −11 5

18 1 11 6 4

.

Using appropriate relational operators set all entries > 10 and < −10 of thematrix C to 0.Hint: first try to realize the comparisons with > 10 and < −10 and then use the resultsto set the entries to 0 with appropriate field operations.

7 Consider the matrix:

D =

7 2 3 10

−2 −3 11 4

8 1 6 5

18 1 11 4

.

Using logical fields select the diagonal of D and save it to a vector called diag.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 18

Page 20: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Mathematical Functions

Act as field operators:

�t = ( 0 : 1 : 4 ) % c a l l w i th a vec to r

t =

0 1 2 3 4�

s=sin ( t ) % values o f s in ( t )

s =

0 0.8415 0.9093 0.1411 −0.7568�

mess = [ 2 5 . 5 1 6 . 3 1 8 . 0 ; . . . % c a l l w i th a mat r i x2 . 0 6 . 9 3 . 0 ; . . .0 .05 4 . 9 1 . 1 ] ;�

dBmess=20∗log10 (mess )

dBmess =

28.1308 24.2438 25.10556.0206 16.7770 9.5424−26.0206 13.8039 0.8279

call equivalent to loops like:

double s [ 6 ] ;for ( i = 0 ; i <6; i ++){

s [ i ] = sin ( i ) ;}

double dBmess [ 3 , 3 ] ;for ( i = 0 ; i <4; i ++){ for ( k = 0 ; k<4; i ++){

dBmess [ i , k ] = 20∗ log10 (mess [ i , k ] ) ;}

}

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 19

Page 21: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

8 For a time vector between 0 and 10 with equidistant spacing of 0.1 calculate thevalues of the signal (function):

s(t) = sin(2π5t) cos(2π3t) + e−0.1t.

9 For a time vector between 0 and 10 with equidistant spacing of 0.1 calculate thevalues of the signal (function):

s(t) = 20 sin(2π5t).

Then round the values first towards∞ and then towards 0. Find the appropriatefunctions using MATLAB’s help mechanisms.In both cases print the first 6 values of s(t) together with their rounded values ina two row matrix and then interprete the somehow strange result.

10 Using the appropriate elementary mathematical MATLAB function, calculatethe corresponding vector of binary and decadic logarithms for

~b =(

1024 1000 100 2 1).

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 20

Page 22: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Plotting Signals and Functions with MATLAB

MATLAB’s plot facilities:

using plot functions in interactive mode and in MATLABprograms

x− y-plots with plot, stem, stairs, ...

line style options

annotation functions

three-dimensional plots with surf, mesh, ...

plotting multiple functions with subplot

using the Plot Tools Window in interactive mode

customizing plots interactively

documentation of plots

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 21

Page 23: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

MATLAB’s x − y-Plot Commands

Using standard plotcommands

�t = ( 0 : 1 : 5 ) ;�s=sin ( t ) ; �plot ( t , s ) ; % The s imp les t form�

% p l o t t i n g m u l t i p l e s i gna l s�t = ( 0 : 0 . 0 1 : 2 ) ;�s in fk t =sin (2∗pi∗5∗t ) ; �cosfkt=2∗cos(2∗pi∗3∗t ) ; �expfkt=exp(−2∗t ) ; �plot ( t , [ s in fk t ; cosfkt ; expfkt ] ) ; �

% much b e t t e r�plot ( t , sinfkt , ’k−’ , t , cosfkt , . . .

’b−−’ , t , expfkt , ’m. ’ )�

% p l o t t i n g i n l a t t i c e fence s t y l e�t = ( 0 : 0 . 0 5 : 2 ) ;�cosfkt=2∗cos(2∗pi∗t ) ; �stem ( t , cosfkt )

Customizing line styles andannotation

�t = ( 0 : 1 : 5 ) ;�s=sin ( t ) ; �plot ( t , s , ’mo’ ) ; % l i n e s t y l e c i r c l e s

% co lo r magenta�

xlabel ( ’ time / s ’ ) % annota t ing x−�ylabel ( ’ampl . / V ’ ) % and y−ax is

% f i g u r e t i t l e �t i t l e ( ’A sine function ’ )�

grid % show g r i d�axis ( [ 0 , 0 . 5 , 0 , 2 ] ) % show only a sec t ion�

% adding a t e x t�text ( 0 . 7 5 , 0 , ’\le f tarrow signal ’ , . . .

’ FontSize ’ ,18)�

% and much more . . .

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 22

Page 24: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

The Plot Tools Window

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 23

Page 25: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

The colorredsubplot Command

Multiple plots with subplot

�% Example : p l o t t i n g absolu te value�% and argument o f a complex f u n c t i o n�

x = ( 0 : 0 . 0 1 : 1 ) ;�f = ( 1 + j∗x ) . / ( j−2∗x ) ; % the complex f u n k t i o n�

subplot ( 2 1 1 ) % s e t t i n g upper p l o t% window�

plot ( x , abs ( f ) , ’b−’ ) % p l o t abs−value�grid % p l o t a g r i d

% annota t ion�xlabel ( ’x−values ’ )�ylabel ( ’modulus of f ( x ) ’ )�

subplot ( 2 1 2 ) % s e t t i n g lower p l o t�plot ( x , angle ( f ) , ’ r−’ )% p l o t argument�grid % p l o t a g r i d

% annota t ion�xlabel ( ’x−values ’ )�ylabel ( ’phase angle of f ( x ) ’ )

Advantage:

- plot of multiple functions withsame argument but differentunity for function values

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 24

Page 26: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

11 Insert the following MATLAB commands to the command window and try to interprete the somehow strange graphicalresult:

�t = ( 0 : 0 . 5 : 1 0 ) ;�s in fk t =sin (2∗pi∗5∗t ) ; �cosfkt=2∗cos(2∗pi∗3∗t ) ; �expfkt=exp(−2∗t ) ; �plot ( t , [ s in fk t ; cosfkt ; expfkt ] )

12 Try to experiment with the MATLAB functions semilogx, semilogy and loglog.For this purpose define a frequency vector

ω = (0.01, 0.02, 0.03, 0.04, . . . , 5) rad/s

and try to plot modulus and phase angle of the the so called transfer functions

H(ω) =1

and

H(ω) =1

1 + jω.

13 Plot the second transfer function above in a superposed way using a logarithmic scale for the frequency and for themodulus but not for the phase angle (this is called a Bode diagram).

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 25

Page 27: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

MATLAB’s I/O-Operations

MATLAB’s I/O facilities:

using I/O functions in interactive mode and in MATLABprograms

save workspace or variables with save

load or variables with load

the ASCII interface

specialized functions for many formats

using the Import Data Tool in interactive mode

open the Import Data Tool with uiimport

open the Import Data Tool with menu or double click

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 26

Page 28: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

14 Using your MATLAB editor create a vector and/or a matrix of real numbers ansave it to a text file.Delete all matrices of the workspace with clear.Then, using the load command, try to input the contents of the text file into theworkspace and analyze the result.

15 With MATLAB create a column vector of complex numbers. Save this vector inMATLAB’s binary format using the save command.The delete the Workspace with clear and then re-input the contents of the savedfile to MATLAB’s workspace using load or the Import Tool!Compare the result with that of the previous exercise.

16 Search for an appropriate MATLAB function which may be used to insert the*.wav-file Tada.wav of the companion software to the workspace.Then display the audio signal graphically. Take care that the time axis isannotated correctly.Then multiply the signal by factor 10 and save it in a *.wav-file using a differentfile name.Listen to the audio signals.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 27

Page 29: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Elementary Matrix Operations

Useful commands forinitialization inMATLAB programs:

�% I n i t i a l i z i n g wi th zeros�M = zeros ( 2 , 2 ) ;�

% I n i t i a l i z i n g wi th ones�K = ones ( 2 , 2 ) ;�

% I n i t i a l i z i n g wi th a u n i t mat r i x�E5 = eye ( 5 ) ;�

% I n i t i a l i z i n g wi th an empty vec to r�N = [ ] ; �

% Determine leng th and s ize o f ar rays�x1 = [ 1 , 2 , 3 , 4 , 5 , 6 ] ;�k = length ( x1 ) ; �[ n ,m] = size (M) ; �

% Determine l a s t component�l as t = zVec ( end ) ;

Reorganizing vectors andmatrices:

�% Transpose a mat r i x�M = [ 1 2 ; 3 − j ; −1 j + 1 ] ;�N = M ’ ; �

% Transpose a mat r i x ( f i e l d−opera t ion )�M = [ 1 2 ; 3 − j ; −1 j + 1 ] ;�N = M . ’ ; �

% Reorganize as column vec to r�mVec = M ( : ) ; �

% Reorganize as mat r i x�L = repmat (M, 2 , 2 ) ;

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 28

Page 30: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

17 Using MATLAB’s help mechanisms, find a function that turns

~y = (1, 1.1, 1.2, 1.3, 1.4, · · · , 9.8, 9.9, 10)

into

~z = (10, 9.9, 9.8, 9.7, · · · , 1.2, 1.1, 1).

18 Consider the vector

~z = (1, 1.5, 2, · · · , 98.5, 99, 99.5, 100).

Using logical operations and using repmat, form this vectorconstruct a new vector ~w containing every third component of~z !

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 29

Page 31: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

MATLAB Script Files

Example:

% Scr ip t−F i l e funkbsp%% Auf ru f : funkbsp%% A MATLAB−Scr ip t−F i l e example%% Autor : Prof . Dr . Ottmar Beucher% HS Kar lsruhe − Technik und W i r t s c h a f t% Version : 1 . 0 1% Date : 14 .11 .2012

t = ( 0 : 0 . 0 1 : 2 ) ;s in fk t =sin (2∗pi∗5∗t ) ;cosfkt=2∗cos(2∗pi∗3∗t ) ;expfkt=exp(−2∗t ) ;plot ( t , [ s in fk t ; cosfkt ; expfkt ] )xlabel ( ’Time / s ’ )ylabel ( ’ Amplitude ’ )t i t l e ( ’ Three beaut i fu l signals ’ )

% launch i n command window�funkbsp

Features:

- collect MATLAB commands toform a program unit

- are executable in commandwindow with file name ascommand

- may use MATLAB’s helpmechanism

- produce workspace variables,act on workspace variables !

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 30

Page 32: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

MATLAB Functions

Example:

function [ t , sinfkt , cosfkt , expfkt ] = . . .funkbsp2 ( f1 , f2 , damp)

%% Funct ion funkbsp2%% Ca l l : see below%% F i r s t example o f a MATLAB−Funct ion

t = ( 0 : 0 . 0 1 : 2 ) ;s in fk t =sin (2∗pi∗f1∗t ) ;cosfkt=2∗cos(2∗pi∗f2∗t ) ;expfkt=exp(−damp∗t ) ;plot ( t , [ s in fk t ; cosfkt ; expfkt ] )xlabel ( ’Time / s ’ )ylabel ( ’ Amplitude ’ )t i t l e ( ’ Three beaut i fu l signals ’ )

% launch i n command window�[ t , sinfkt , cosfkt , expfkt ] = . . .

funkbsp2 ( 2 , 3 , 0 . 1 )% or s imply�

funkbsp2 ( 2 , 3 , 0 . 1 )

Features:

- collect MATLAB commands toform a program unit capable ofbeing parameterized

- are executable in CommandWindow with file name ascommand and with parameters

- may also use MATLAB’s helpmechanism

- clear input/output-parameterinterface

- act on their own variable stack!

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 31

Page 33: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

MATLAB’s Function Calling Mechanism (Call by Value)

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 32

Page 34: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

19 Change the code of function funkbsp3 in such way, that you mayexternally have access to the contents of componentwise squaredvariable a.

20 Change the code of function funkbsp2 in such way, that you mayadditionally pass colors and line styles for the graphs of thesignals. The function should then plot the signals in that way.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 33

Page 35: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

MATLAB as a Programming Language

MATLAB as a Procedural ProgrammingLanguage:

Programming language constructs .

Control flow .i f − Condit ional ly executeelse statements .e l s e i fend

for − Repeat statements aspeci f ic number of times .

while − Repeat statements ani n d e f i n i t e number of times .

. . .switch − Switch among several

cases based on expressions .. . .

MATLAB as an OOP Language:

classdef exampleclass

properties % def ine c lass p r o p e r t i e send

methods % def ine c lass methodsend

end

Features:

- procedural programmingapproach

- commands and constructssimilar to otherprogramming languages(like C/C++)

- special commands forMATLAB functions withvariable parameter lists

- not treated here.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 34

Page 36: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

21 Write a MATLAB function that determines for a given vector of numbers themaximum of its entries using an appropriate loop construct.The vector should be the only input parameter, the found maximum the onlyoutput parameter.

22 Write a MATLAB function that determines for a given vector of numbers thepositive entries using an appropriate loop construct.The given vector should be the only input parameter of the MATLAB function.The result (the positive entries) should be organized in a vector and this vectorshould be the output parameter of the MATLAB function.

23 Write a MATLAB function that plots the signal sin(x) between 0 and 2π in agiven color.The color should be the input parameter of the MATLAB function having thefollowing strings as allowed values: ’red’, ’blue’, ’green’, ’magenta’. Use aswitch...case construct to determine internally, according to the input parametervalue, which color to use in the plot command.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 35

Page 37: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Function Handles

Function handles- a special kind of pointer:

�my cos = @cos ;�

whosName Size Bytes Class

my cos 1x1 16 function handle�

value = cos ( 1 )

value =

0.5403�

value = my cos ( 1 )

value =

0.5403

Features:

- a ”pointer” to aMATLAB function

- main usage:

pass MATLABfunctions to otherMATLAB functions viaparameter list!

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 36

Page 38: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

24 Revisit the previous exercise (23) and enlarge the functionality ofthe program in a way that enables the user to pass a signal and theplot interval to the MATLAB function. Suppose that the signal isprogrammed in a separate MATLAB function or that it is abuilt-in MATLAB function.

25 The MATLAB function fzero calculates (using an appropriatenumerical procedure) the root of a real valued function. This realfunction, realized as a MATLAB function, has to be passed tofzero as a function handle.Familiarize with the calling convention of fzero and calculate thezeros of example functions calling fzero in the Command Window.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 37

Page 39: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

MATLAB’s ODE Solvers

Calling MATLAB’s ODE solvers:

�help ode23

ode23 Solve non−s t i f f d i f f e r e n t i a l equations ,low order method .

[TOUT,YOUT ] = ode23 (ODEFUN,TSPAN, Y0 ) withTSPAN = [ T0 TFINAL ] integrates the system ofd i f f e r e n t i a l equations y ’ = f ( t , y ) from timeT0 to TFINAL with i n i t i a l conditions Y0 .

ODEFUN is a function handle .

For a scalar T and a vector Y , ODEFUN(T ,Y )must return a column vector correspondingto f ( t , y ) .Each row in the solution array YOUT corres−ponds to a time returned in the columnvector TOUT .To obtain solutions at speci f ic timesT0 , T1 , . . . , TFINAL , use TSPAN =[ T0 T1 . . . TFINAL ] .. . .

See also ode45 , ode113 , ode15s , ode23s ,ode23t , ode23tb , ode15i , odeset , odeplot ,odephas3 , odeprint , . . . .

Reference page in Help browserdoc ode23

Features:

- solve initial value problemsof type y′ = f (t, y)

- solve ODEs of first order orsystems of first order ODEs

- need a user-defined ODEfunction

- need a time range and avector of initial values

- deliver numerical solutionat internally definedsupporting points

- are optimized by using stepsize steering

- Runge-Kutta-basedalgorithms for non-stiff andstiff ODEs

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 38

Page 40: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

The Mathematical Pendulum

Example: Mathematical model:

initial value problem of second order:

α(t) = −gl· sin(α(t)), α(0), α(0).

or equivalently:

α1(t) = α2(t),

α2(t) = −gl· sin(α1(t))

~α(0) =

α(0)

α(0)

=

α1(0)

α2(0)

.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 39

Page 41: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

The ODE-file Structure

Structure of an ODE-File(example):

function [ alphadot ] = pendgl ( t , alpha )%% Funct ion pendgl%% Ca l l : on ly c a l l e d by ODE−so lve rs%% Example o f an ODE−f i l e f o r use wi th% ode23 , ode45 a . s . o .%

l =10; % leng th o f pendulumg=9 . 81 ; % g r a v i t . acce l e ra t i o n

%% I n i t i a l i z a t i o n

alphadot = [ 0 ; 0 ] ;

%% Representat ion o f the f i r s t order ODEs

% f i r s t equat ionalphadot ( 1 ) = alpha ( 2 ) ;

% second equat ionalphadot (2) = − (g / l )∗sin ( alpha ( 1 ) ) ;

Features:

- represents the system of 1storder ODEs

- has a fixed parameterinterface

- does’nt make sense as afunction of it’s own! Iscalled only by MATLAB’ssolvers

- has to be passed toMATLAB’s solvers by afunction handle

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 40

Page 42: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

How Solvers Use ODE-file’s Information

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 41

Page 43: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Resistor-Capacitor Lowpass

Example: Mathematical model:

initial value problem of first order:

ddt

u(t) = − 1RC

u(t) +1

RCu1(t), u(0) = u0.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 42

Page 44: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

28 Solve the initial value problem for the resistor-capacitor lowpassusing different functions as input signals of the lowpass (insteadof u1(t) take for instance a sinusoidal input).

29 Solve the problem:

y1(t) = −2y1(t)− y2(t) y1(0) = 1,y2(t) = 4y1(t)− y2(t) y2(0) = 1.

Compare the numerical solution with an analytical one (whichyou may calculate ”by hand”).

30 Solve the problem:...y + 3y + 3y + y = u1(t), y(0) = 0, y(0) = 1, y(0) = 1.

The function u1(t) denotes the unit step function.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 43

Page 45: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

The Symbolic Math Toolbox

Delivers Computer Algebra facilities

two different interfaces

the MuPAD-Notebook: user interface with it’s own command syntax

the MATLAB-adaption: user interface with MATLAB’s command syntax

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 44

Page 46: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Symbolic Calculus Example

�syms x y v�whos

Name Size Bytes Class Attr ibutesv 1x1 60 symx 1x1 60 symy 1x1 60 sym�

f = sin ( x∗y ˆ2)∗cos ( v∗x∗y ) % def ine a f u n c t i o n

f =

sin ( x∗y ˆ2)∗cos ( v∗x∗y )�

dfy = d i f f ( f , ’ y ’ ) % d e r i v a t i v e w i th respect to y

dfy =

2∗x∗y∗cos ( x∗y ˆ2)∗cos ( v∗x∗y)−v∗x∗sin ( x∗y ˆ2)∗ sin ( v∗x∗y )�

dfv = d i f f ( f , ’ v ’ ) % d e r i v a t i v e w i th respect to v

dfv =

−x∗y∗sin ( x∗y ˆ2)∗ sin ( v∗x∗y )�

pret ty ( f ) % d i sp lay expression ” p r e t t y ”

2sin ( x y ) cos ( v x y )

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 45

Page 47: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Export Symbolic to Numeric

syms x % def ine symbol ic v a r i a b l e sf = xˆ2∗cos ( x ) ; % def ine f u n c t i o ndf = d i f f ( f , ’ x ’ ) % c a l c u l a t e symbol ic d e r i v a t i v e

dt = 0 . 1 ; % def ine sampling step s izet = ( 0 : dt : 1 0 ) ; % def ine sampling po in t sfnum = t . ˆ 2 .∗cos ( t ) ; % c a l c u l a t e f u n c t i o n numer i ca l l y

dfnum = d i f f ( fnum ) / dt ; % d i f f e r e n t i a t e f u n c t i o n numer i ca l l y

% p l o t numer ica l d e r i v a t i v eplot ( ( dt : dt : 1 0 ) , dfnum , ’b−−’ )

holddfnum2 = subs ( df , x , t ) ; % conver t symbol ic s o l u t i o n to

% a numer ica l s o l u t i o n wi th theplot ( t , dfnum2 , ’ r ’ ) % subs−command and p l o t i t

gridxlabel ( ’ x ’ )ylabel ( ’ der ivat ive ’ )

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 46

Page 48: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

31 Integrate the function

g(x) = sin(5x− 2)

two times symbolically.

32 Calculate the 3rd-order Taylor-polynomial in x0 = 1 of thefunction g(x).

33 Using the symbolic math function dsolve calculate the solution ofthe following ordinary differential equation:

y = xy2.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 47

Page 49: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Dynamical Systems - a Known Example

Example: Modelling the dynamics:

y(t) =1C

t∫0

i(τ) dτ,

y(t) = x(t)− i(t) · R(⇐⇒ i(t) =

1R(x(t)− y(t)

).

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 48

Page 50: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

The Simulink Idea

The idea behind Simulink:

draw a block diagram of your dynamical system

let Simulink convert it to a system of differential equations

let Simulink solve this system of differential equations by usingone of MATLAB’s numerical ODE solvers

let Simulink display and save the signals (input signals, outputsignals (normally ODE solutions) and internal states

Simulink may be used to:

directly model a given real dynamical system by its block diagramand simulate the dynamical behavior of that system

solve a system of ODEs by converting the ODEs into a blockdiagram and simulating the resulting dynamical system

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 49

Page 51: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Constructing a Simulink Model - The Library Browser

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 50

Page 52: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Constructing a Simulink Model - The Model Window

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 51

Page 53: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Simulating a Simulink Model - The Parameter Panel

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 52

Page 54: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

34 Check the test system sl test1.slx for different step sizes using asolver with internal step size control (variable step solvers).Compare different calculation times when using a step size of0.00001 directly with the fixed step solver ode3 on one hand andwhen using the variable step solvers ode23 on the other hand.

35 Design a Simulink test system sl LsgDiff.slx for the derivativeblock Derivative.The quickest way to do so is to alter the system sl test1.slx.Then experiment like in the previous exercise.

36 Explain why the result of the test system sl test1.slx representsthe solution of the initial value problem

y(t) = x(t), y(0) = 0.

Which one of the signals is x(t), which one is y(t)?

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 53

Page 55: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Dynamical Systems - Three Examples

Three Examples:

an undamped harmonic oscillation

y(t) = −y(t), y(0) = 1, y(0) = 0.

the resistor-capacitor lowpass

T · y(t) + ·y(t) = x(t), T :=1

RC

rapid mechanical oscillations in air and fluids

mx(t) + b · sgn(x(t)) · x2(t) + cx(t) = 0.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 54

Page 56: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

37 Design a Simulink system to solve the nonlinear differential equation

α(t) = −gl· sin(α(t)), α(0) = a0, α(0) = a1

of the mathematical pendulum (i.e. to simulate the dynamical behaviorof the mathematical pendulum).

38 Creating an appropriate Simulink system solve the following initialvalue problem of third order:

...y (t) + 3y(t) + 4y(t) + 2y(t) = 0, y(0) = 1, y(0) = 1, y(0) = 0.

39 With an appropriate Simulink system solve the ODE-system

y1(t) = −3y1(t)− 2y2(t), y1(0) = 1,y2(t) = 4y1(t) + 2y2(t), y2(0) = 1.

Compare the numerical Simulink solution with an exact one which youmay calculate by hand or using the Symbolic Math Toolbox.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 55

Page 57: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Simulink System Simplification Methods

Methods: The function block (Fcn)

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 56

Page 58: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Simulink System Simplification Methods

Methods: Creating Subsystems

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 57

Page 59: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

40 Simplify the Simulink system for the solution of exercise (39) bydefining appropriate subsystem blocks.

41 Create a Simulink system for the solution of exercise (39) usingappropriate Fcn blocks.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 58

Page 60: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Interaction with MATLAB - an Overview

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 59

Page 61: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Interaction with MATLAB - the Config Parameters Panel

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 60

Page 62: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

42 Design a Simulink system to solve the linear differential equation

my(t) + 2y(t) + 4y(t) = F(t), y(0) = 1, y(0) = 0.

This equation models the movement of an oscillating object with mass mdisturbed by an external force F(t).In the first experiment use a step function to model the force F(t). Thechange from 0 (no disturbing force) to c (constant disturbing force)should occur at time t0 > 0.Use workspace variables to parameterize the system.

43 Solve the problem of the previous exercise (42) once again by creating anew Simulink system that inputs the disturbing external force F(t) witha From Workspace block and that outputs the result with a ToWorkspace block.

44 Solve the problem of the previous exercise (42) once more by creating anew Simulink system that inputs the disturbing external force F(t) viaan Inport and that outputs the result with an Outport.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 61

Page 63: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Interaction with MATLAB - Data Exchange with Functions

Set parameters of a Simulink system with set param:�% Example : se t parameter StopTime of system s l Pende l to 30�set param ( ’ sl Pendel ’ , ’StopTime ’ , ’ 30 ’ )�

% Example : se t parameter Gain o f Block Gain i n system s l Pende l to −1�set param ( ’ sl Pendel / Gain ’ , ’ Gain ’ , ’−1’ ) ; �

% Example : se t parameter Gain o f Block Gain i n system s l Pende l to−g / l �g = 9 . 8 1 ; % g r a v i t y acce le ra t i on�l = 2 5 ; % leng th o f pendulum�

set param ( ’ sl Pendel / Gain ’ , ’ Gain ’ , num2str(−g / l ) ) ;

Get parameters of a Simulink system with get param:�% Example : get parameter StopTime of system s l Pende l�get param ( ’ sl Pendel ’ , ’StopTime ’ )�

% Example : get parameter Gain o f Block Gain i n system s l Pende l�set param ( ’ sl Pendel / Gain ’ , ’ Gain ’ ) ; �

% Example : get i n i t i a l c o n d i t i o n parameter o f the i n t e g r a t o r�% block i n system s l Pende l�

set param ( ’ sl Pendel / Integrator1 ’ , ’ I n i t i a lCond i t ion ’ ) ;

Note: This works also from within MATLAB functions!Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 62

Page 64: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

45 Revisit the Simulink system of the previous exercise (42) and alterthe parameters via set param before starting it.

46 Launch the Simulink system sl Pendel2.slx of the companionsoftware. This system simulates the mathematical pendulum.Alter the parameter pendulum length via set param to 5 m andthen to 8 m before starting the simulation.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 63

Page 65: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Interaction with MATLAB - the sim Command

The sim command

simulates (an opened) Simulink system

may parameterize the Simulink system via its parameter list

returns simulation data via a return parameter (theSimulink.SimulationOutput object)

logged signals (see Data Import/Export panel) may be included inthe SimulationOutput object

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 64

Page 66: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

The sim Command - the Config Parameters Panel

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 65

Page 67: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

The sim Command - Automize Simulink Simulations

Automize Simulink simulations with sim - an example:function [ ze i t , simuerg ] = simPendel2 ( pendellaenge )

% Open Simul ink system i f i t ’ s not open

open ( ’ sl Pendel2 . slx ’ ) ;

% Set Gain b lock parametersg = 9 . 8 1 ; % g r a v i t y acce l e ra t i on

% update wi th set paramset param ( ’ sl Pendel2 / Gain ’ , ’ Gain ’ , num2str(−g / pendellaenge ) ) ;

% Simulate Simul ink system wi th sim% make sure t h a t sim re tu rns a Simul ink . S imulat ionOutput ob jec t% by s e t t i n g the parameter ’ SaveOutput ’ to ’ on ’% ( i f sim i s lauched only w i th the Simul ink system name% only the t ime vec to r w i l l be re turned )

simOut = sim ( ’ sl Pendel2 ’ , ’ SaveOutput ’ , ’on ’ ) ;

% Access the r e s u l t s w i th the get method% and save them i n t o the output v a r i a b l e s% of f u n c t i o n simPendel2

z e i t = simOut . get ( ’ t ’ ) ;simuerg = simOut . get ( ’ yout ’ ) ;

Note: the system sl Pendel2 may be launched several times this way,using a for loop and saving the results in matrices or cell arrays!!Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 66

Page 68: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

47 Write a MATLAB function with which you may simulate theSimulink system sl Pendel2.slx for a given vector of pendulumlengths.

48 Design a Simulink system that creates sinusoidal signals using thesine generator block Sine Wave.Then write a MATLAB function that is able to launch thisSimulink system several times varying the frequency of the sinewave.

49 Design a Simulink system that solves the initial value problem

y(t) + y(t) + y(t) = f (t), y(0) = 0, y(0) = 0.

The function f (t) should be a step function (use block Step) with astep from 0 to h.Then write a MATLAB function that is able to launch thisSimulink system several times varying the parameter h.

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 67

Page 69: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Look Up Tables

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 68

Page 70: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Look Up Tables - An Example

Tension-current characteristic of a solar cell:

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 69

Page 71: MATLAB and Simulink - 國立中興大學 · MATLAB and Simulink Prof. Dr. Ottmar Beucher University of Applied Sciences Karlsruhe Germany National Chung Hsing University, Taichung

Exercises

50 Consider the following characteristic:

k(x) =

0 fur x < 0,√

x fur x ∈ [0, 4],

2 fur x > 4.

Design a Simulink system that realizes this characteristic with a 1-D LookupTable. Input a sine wave signal to the Lookup Table and analyze its outputsignal. Experiment with different amplitudes of the sine wave.

51 Consider the following two-dimensional characteristic:

k(x, y) = x2 + y2 fur x, y ∈ [−1, 1].

Design a Simulink system that realizes this characteristic with a 2-D LookupTable.Then activate the characteristic at points (x, x) with x ∈ [−1, 1]. For this purposedesign an appropriate input signal which you should read from workspace witha From Workspace block.Concerning the parameterization of the 2-D Lookup Table compare the stepsneeded for creating three dimensional plot (e.g. the function meshgrid)!

Prof. Dr. Ottmar Beucher MATLAB and Simulink - Summer School NCHU, Taichung July 2013 70