Anda di halaman 1dari 91

Matlab /Simulink

Fundamentals
Dr. U D Dwivedi
Assistant Professor (Electrical Engineering)
Room No. 202, Rajiv Gandhi Institute of Petroleum Technology,
Ratapur Chowk,
Rae Bareli -229316 INDIA

Getting Started with


Simulink

What is SIMULINK?

SIMULINK is an object oriented dynamic


simulation package

Contents

Getting Started with Simulink


Launch Simulink
Basic Concepts
Mass Spring Damper Example
Initial Conditions
Run Simulation

First of all, launch Matlab. Press the


Simulink button

Basic Concepts

Discusses the basics of operating Simulink,


including starting the program, using blocks
and lines, building models, and running
simulations

Starting Simulink
Simulink is started from the MATLAB
command prompt by entering the following
command: simulink
Alternatively, you can click on the "Simulink
Library Browser" button at the top of the
MATLAB command window as shown below:

The Simulink Library Browser window should


now appear on the screen. Most of the blocks
needed for modeling basic systems can be
found in the subfolders of the main "Simulink"
folder (opened by clicking on the "+" in front
of "Simulink"). Once the "Simulink" folder
has been opened, the Library Browser
window should look like

Click to open Simulink

10

11

Basic Elements
There are two major classes of elements in
Simulink: blocks and lines.
Blocks are used to generate, modify, combine,
output, and display signals.
Lines are used to transfer signals from one
block to another.
12

Blocks

The subfolders underneath the "Simulink" folder indicate


the general classes of blocks available for us to use:

Continuous: Linear, continuous-time system elements


(integrators, transfer functions, state-space models, etc.)

Discrete: Linear, discrete-time system elements


(integrators, transfer functions, state-space models, etc.)

Functions & Tables: User-defined functions and tables for


interpolating function values

Math: Mathematical operators (sum, gain, dot product, etc.)

13

Contd...

Nonlinear: Nonlinear operators (coulomb/viscous friction, switches,


relays, etc.)

Signals & Systems: Blocks for controlling/monitoring signal(s) and for


creating subsystems

Sinks: Used to output or display signals (displays, scopes, graphs, etc.)

Sources: Used to generate various signals (step, ramp, sinusoidal, etc.)

Blocks have zero to several input terminals and zero to several


output terminals. Unused input terminals are indicated by a small
open triangle. Unused output terminals are indicated by a small
triangular point. The block shown below has an unused input
terminal on the left and an unused output terminal on the right.

14

Lines

Lines transmit signals in the direction indicated by the arrow.


Lines must always transmit signals from the output terminal of one
block to the input terminal of another block. One exception to this is
that a line can tap off of another line. This sends the original signal
to each of two (or more) destination blocks, as shown below:

Lines can never inject a signal into another line; lines must be
combined through the use of a block such as a summing junction.

A signal can be either a scalar signal or a vector signal. For SingleInput, Single-Output systems, scalar signals are generally used. For
Multi-Input, Multi-Output systems, vector signals are often used,
consisting of two or more scalar signals. The lines used to transmit
scalar and vector signals are identical. The type of signal carried by
a line is determined by the blocks on either end of the line.

15

Building a System

This model will consist of three blocks: Sine Wave, Gain, and
Scope. The Sine Wave is a Source Block from which a
sinusoidal input signal originates. This signal is transferred
through a line in the direction indicated by the arrow to the Gain
Math Block. The Gain block modifies its input signal (multiplies
it by a constant value) and outputs a new signal through a line to
the Scope block. The Scope is a Sink Block used to display a
signal (much like an oscilloscope).

16

Running Simulations

Now that our model has been


constructed, we are ready to simulate the
system. To do this, go to the Simulation
menu and click on Start, or just click on
the "Start/Pause Simulation" button in the
model window toolbar (looks like the
"Play" button on a VCR). Because our
example is a relatively simple model, its
simulation runs almost
instantaneously. With more complicated
systems, however, you will be able to see
the progress of the simulation by
observing its running time in the the
lower box of the model window. Doubleclick the Scope block to view the output
of the Gain block for the simulation as a
function of time. Once the Scope window
appears, click the "Autoscale" button in
its toolbar (looks like a pair of binoculars)
to scale the graph to better fit the
window. Having done this, you should
see the following:

17

18

19

Mass Spring Damper


Example using Simulink

You will see the Simulink window

Open a new Simulink window by clicking on the


New button.

21

Mass Spring Damper Example

First of all, we will simulate a mass-springdamper system.

22

We assume that there is no force involved. Lets get started.


Open a new Simulink file. Our system involves two integrators
(from acceleration to velocity and from velocity to position). In the
Simulink window, click on Continuous.

23

In the right hand side you can see the integrator. Click on it and
drag the block inside the window just opened. Repeat this
operation as, we need two integrators. By now, your window
should look like below:

24

To model our system we need two more gain blocks and one
sumator block. We can find these two blocks in the Math section.

25

Right click on the first Gain block. Choose Format->Rotate


Block. As this operation will rotate the block with 90 degrees
(clockwise), you have to do it once more time.

26

Do the same for the second Gain block. Add


a scope from Sinks and connect the blocks
with lines.

27

If you take a look on the block diagram you


have just constructed, it is easy to see that it
represents the relationship:

where, the first gain represents the c/m term and the second
one represents the term k/m

28

Initial Conditions

Now, we do have our model in Simulink. It is easy to imagine that the mass will not move because there
is no force acting on it. With no force motion is possible if the initial conditions are not zero. As the
equation of motion is a second order differential equation, there are two initial conditions. Right click on
the second integrator. Choose "Block Parameters".

29

In the pop-up window set "initial condition" to


"1".

30

This will set the initial position of the masss to 1. Basically, it is


exactly the same as the mass will be forced to stay in this
position by an external force. The force vanished at moment
zero. Add another scope, to visualize the velocity (the output of
the first integrator).

31

The next step is tu run the simulation and


visualize the results.Now, we do have our
model in Simulink. All we need to do is to
press the "Run" button

32

OR

33

Run the simulation. To see the results, double click on the two
scopes. The results can be seen below It can be seen that the
initial value for position is 1 while the initial value for velocity is 0.

34

Introduction to Symbolic
Toolbox

This section introduces you to the Symbolic Math Toolbox, and


describes how to create and use symbolic objects. The section
covers the following topics:
What Are the Symbolic Math Toolboxes?

Symbolic Objects?

How to substitute numbers for variables

Symbolic and Numeric Conversions

How to create symbolic objects

The subs Command

Describes symbolic objects and how they differ from standard


MATLAB data types

Creating Symbolic Variables and Expressions

Overview of the toolboxes

How to convert between symbolic objects and numeric values

Creating Symbolic Math Functions

How to create functions that operate on symbolic objects

36

What Are the Symbolic Math Toolboxes?

The Symbolic Math Toolboxes incorporate symbolic computation into the


numeric environment of MATLAB. These toolboxes supplement MATLAB
numeric and graphical facilities with several other types of mathematical
computation, which are summarized in following table.

37

Symbolic Objects

The Symbolic Math Toolbox defines a new MATLAB data type


called a symbolic object.
The following example illustrates the difference between a
standard MATLAB data type, such as double, and the
corresponding symbolic object.

On the other hand, if we convert 2 to a symbolic object using


the sym command, and then take its square root by entering a
= sqrt(sym(2))

The MATLAB command sqrt(2)

returns a floating-point decimal number:

ans =1.4142

the result is

a =2^(1/2)

We can always obtain the numerical value of a symbolic


object with the double command:

double(a) results in

ans = 1.4142
38

Creating Symbolic Variables and


Expressions

The sym command lets you construct


symbolic variables and expressions. For
example, the commands
>> x = sym('x')
>>a = sym('alpha')

create a symbolic variable x that prints as x and a


symbolic variable a that prints as alpha.

39

We can write a quadratic equation in


symbolic form
>> f = sym('a*x^2 + b*x + c')
f=
a*x^2 + b*x + c
If we want to perform symbolic math operations (e.g., integration,
differentiation, substitution, etc.) then first we define symbols by:
>> syms a b c x
Then we define function f by
>>f = sym('a*x^2 + b*x + c')
f=
a*x^2 + b*x + c

40

The findsym Command

To determine what symbolic variables are present in an


expression, use the findsym command.

For example, given the symbolic expressions f and g defined


by
>> syms a b n t x z
f = x^n; g = sin(a*t + b);

we can find the symbolic variables in f by entering findsym(f)


ans =
n, x

Similarly, we can find the symbolic variables in g by entering


findsym(g)
ans =
a, b, t

41

The subs Command

You can substitute a numerical value for a


symbolic variable using the subs command.

For example, to substitute the value x = 2 in the symbolic


expression,
f = 2*x^2 - 3*x + 1
enter the command
subs(f,2)
This returns f(2):
ans =
3

42

When expression contains more than one variable,


we can specify the variable for which we want to
make the substitution.

For example, to substitute the value x = 3 in the symbolic


expression,
>>syms x y
f = x^2*y + 5*x*sqrt(y)

enter the command subs(f, x, 3)


This returns
ans =
9*y+15*y^(1/2)

On the other hand, to substitute y = 3, enter


>>subs(f, y, 3)
ans =
3*x^2+5*x*3^(1/2)

43

Symbolic and Numeric Conversions

Consider the ordinary MATLAB quantity t = 0.1

The sym function has four options for returning a symbolic


representation of the numeric value stored in t.

First Option is : The 'f' option sym(t,'f')

returns a symbolic floating-point representation

'1.999999999999a'*2^(-4)

Second Option is : The 'r' option sym(t,'r')

returns the rational form 1/10

This option is the default setting for sym.

44

Third Option is: option 'e' that returns


the rational form of t plus the difference between the
theoretical rational expression for t and its actual (machine)
floating-point value in terms of eps (the floating-point
relative accuracy):
>>sym(t,'e')

ans =
1/10+eps/40
Fourth Option is: option 'd' that returns
the decimal expansion of t up to the number of significant
digits specified by digits:
>>sym(t,'d')
ans =
.10000000000000000555111512312578
Default value of d is 32

45

The sym command allow us to specify the


mathematical properties of symbolic variables by
using the 'real' option.

That is, the statements

x = sym('x','real');
y = sym('y','real');

or more efficiently syms x y real

z = x + i*y

create symbolic variables x and y that have the


added mathematical property of being real variables.

Specifically this means that the expression f = x^2 + y^2 is


strictly nonnegative.

Hence, z is a complex variable and can be manipulated as


such.

Thus, the commands conj(x), conj(z), expand(z*conj(z))

return x, x-i*y, x^2+y^2 respectively.

46

Example: Creating a Symbolic circulant matrix A that has the property


that each row is obtained from the previous one by cyclically permuting
the entries one step forward. We can create the circulant matrix A
whose elements are a, b, and c, using

>>syms a b c
>>A = [a b c; b c a; c a b]
which return A =
[ a, b, c ]
[ b, c, a ]
[ c, a, b ]

Since A is circulant, the sum over each row and column is the same. To
check this for the first row and second column, enter the command

>>sum(A(1,:))
which returns ans =
a+b+c
The command sum(A(1,:)) == sum(A(:,2)) % This is a logical test.
returns ans =
1

47

Now replace the (2,3) entry of A with beta and the


variable b with alpha. The commands
>>syms alpha beta;
A(2,3) = beta;

Then command
>>A = subs(A,b,alpha)

return
A=
[ a, alpha, c]
[ alpha, c, beta]
[ c, a, alpha]

From this example, we can see that using symbolic objects is


very similar to using regular MATLAB numeric objects.

48

Using the Symbolic Math Toolbox

Calculus:

Simplifications and Substitutions:

Inverses, determinants, eigenvalues, singular value decomposition, and


canonical forms of symbolic matrices

Solving Equations

Numerical evaluation of mathematical expressions to any specified accuracy

Linear Algebra:

Methods of simplifying algebraic expressions

Variable-Precision Arithmetic:

Differentiation, integration, limits, summation, and Taylor series

Symbolic and numerical solutions to algebraic and differential equations

Special Mathematical Functions:

Special functions of classical applied mathematics

49

Calculus

Differentiation
To illustrate how to take derivatives using the Symbolic
Math Toolbox, first create a symbolic expression:
syms x
f = sin(5*x)

The command diff(f)


differentiates f with respect to x:
ans =
5*cos(5*x)

50

Derivatives of Expressions with Several Variables


To differentiate an expression that contains more than one
symbolic variable, we must specify the variable that we want to
differentiate with respect to. The diff command then calculates the
partial derivative of the expression with respect to that variable.
For example, given the symbolic expression
syms s t
f = sin(s*t)
the command diff(f,t)
calculates the partial derivative df/dt
ans =
cos(s*t)*s
To differentiate f with respect to the variable s, enter diff(f,s)
which returns:
ans =
cos(s*t)*t

51

Limits

The fundamental idea in calculus is to make


calculations on functions as a variable "gets close
to" or approaches a certain value.

The Symbolic Math Toolbox enables you to calculate the


limits of functions directly.
The commands syms h n x
limit( (cos(x+h) - cos(x))/h,h,0 )
which return ans =
-sin(x)
and limit( (1 + x/n)^n,n,inf )
which returns
ans =
exp(x)

52

Integration

If f is a symbolic expression, then int(f)

attempts to find another symbolic expression, F, so that


diff(F) = f. That is, int(f) returns the indefinite integral or
antiderivative of f (provided one exists in closed form).

Similar to differentiation, int(f,v)

uses the symbolic object v as the variable of integration,


rather than the variable determined by findsym.

53

Example

54

In many cases, MATLAB can perform symbolic


integration successfully.

For example, create the symbolic variables


syms a b theta x y n u z

55

56

57

Taylor approximation vs. actual function


Taylor
Function

1
1

1.2

1.4

1.6

1.8

2
x

2.2

2.4

2.6

2.858

59

You can plot the graph of f by entering

ezplot(f)

(3 x 2+6 x-1)/(x 2+x-3)

-2

-4
-6

-4

-2

0
x

60

61

set the axes limits manually to see the entire


function:

ezplot(f2)
axis([-2*pi 2*pi -5 2])
title('Graph of f2')
Graph of f2
2

-1

-2

-3

-4

-5

-6

-4

-2

0
x

62

Simplifications and Substitutions

Here are three different symbolic expressions.

syms x

f = x^3-6*x^2+11*x-6
g = (x-1)*(x-2)*(x-3)
h = -6+(11+(-6+x)*x)*x

Here are their prettyprinted forms, generated by

pretty(f), pretty(g), pretty(h)

x3 - 6 x2 + 11 x - 6
(x - 1) (x - 2) (x - 3)
-6 + (11 + (-6 + x) x) x

63

Command: collect

The statement collect(f)

views f as a polynomial in its symbolic variable, say x,


and collects all the coefficients with the same power of
x. A second argument can specify the variable in which
to collect terms if there is more than one candidate.
Here are a few examples.

64

expand

distributes products over


sums and applies other
identities involving
functions of sums as
shown in the examples
below.

factor

If f is a polynomial with
rational coefficients, the
statement factor(f)
expresses f as a product
of polynomials of lower
degree with rational
coefficients.

65

Simplify

The simplify function is a powerful command, general


purpose tool that applies a number of algebraic identities
involving sums, integral powers, square roots and other
fractional powers, as well as a number of functional
identities involving trig functions, exponential and log
functions, Bessel functions, hypergeometric functions, and
the gamma function.

66

Substitutions

There are two functions for symbolic substitution:


subexpr and subs.
subexpr

These commands
syms a x
s = solve(x^3+a*x+1)

67

68

SUBS Symbolic substitution.

SUBS(S) replaces all the variables in the symbolic


expression S with values obtained from the calling function, or
the MATLAB workspace.

Examples:
Single input: Suppose a = 980 and C1 = 3 exist in the
workspace. The statement
>> y = dsolve('Dy = -a*y')
y = exp(-a*t)*C1

produces

Then the statement


subs(y)
ans = 3*exp(-980*t)

produces

Single Substitution:

subs(a+b,a,4)

returns

4+b.

Multiple Substitutions:

subs(cos(a)+sin(b),{a,b},{sym('alpha'),2})
cos(alpha)+sin(2)

returns

69

Integral Transforms

Fourier transform

fourier

Inverse Fourier transform

ifourier

Inverse Laplace transform ilaplace

Inverse z-transform

iztrans

Laplace transform

laplace

z-transform

ztrans

70

fourier Fourier integral transform

Syntax
F = fourier(f)
F = fourier(f,v)
F = fourier(f,u,v)

F = fourier(f) is the Fourier transform of the


symbolic scalar f with default independent
variable x. The default return is a function of
w. The Fourier transform is applied to a
function of x and returns a function of w.

71

72

73

Laplace transform
Syntax

laplace(F)
laplace(F,t)
laplace(F,w,z)

74

75

Some Examples ..

For different commands such as

syms
dsolve
solve
pretty
diff

76

MATLAB's vector and matrix notation extends to


symbolic variables.

n = 4;

D = diff(log(A))

A = x.^((0:n)'*(0:n))

D=

A=

[
[
[
[
[

1, 1, 1, 1, 1]
1, x, x^2, x^3, x^4]
1, x^2, x^4, x^6, x^8]
1, x^3, x^6, x^9, x^12]
1, x^4, x^8, x^12, x^16]

[
[
[
[
[

0,
0,
0,
0,
0,

0,
1/x,
2/x,
3/x,
4/x,

0, 0, 0]
2/x, 3/x, 4/x]
4/x, 6/x, 8/x]
6/x, 9/x, 12/x]
8/x, 12/x, 16/x]

77

Demonstrate symbolic equation solving.


The "solve" and "dsolve" functions seek analytic solutions
to algebraic and ordinary differential equations.

% The first example is a simple


quadratic.
% You can either find the
zeros of a symbolic
expression, without quotes:
syms a b c x
x = solve(a*x^2 + b*x + c);
% Or, you can find the roots of
an equation, given in quotes:
x = solve('a*x^2 + b*x + c = 0');

% Both of these produce the


same result:
x

x=

1/2/a*(-b+(b^2-4*a*c)^(1/2))
1/2/a*(-b-(b^2-4*a*c)^(1/2))

78

% The solution to a general cubic is:


x = solve('a*x^3 + b*x^2 + c*x + d')

x=

1/6/a*(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^218*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)-2/3*(3*a*c-b^2)/a/(36*b*c*a-108*d*a^28*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)1/3*b/a
-1/12/a*(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^218*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)+1/3*(3*a*c-b^2)/a/(36*b*c*a-108*d*a^28*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)1/3*b/a+1/2*i*3^(1/2)*(1/6/a*(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^218*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)+2/3*(3*a*c-b^2)/a/(36*b*c*a-108*d*a^28*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3))
-1/12/a*(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^218*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)+1/3*(3*a*c-b^2)/a/(36*b*c*a-108*d*a^28*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)1/3*b/a-1/2*i*3^(1/2)*(1/6/a*(36*b*c*a-108*d*a^2-8*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^218*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3)+2/3*(3*a*c-b^2)/a/(36*b*c*a-108*d*a^28*b^3+12*3^(1/2)*(4*a*c^3-c^2*b^2-18*b*c*a*d+27*d^2*a^2+4*d*b^3)^(1/2)*a)^(1/3))

79

pretty(x)

[
1/3
2
]
[ %1
3ac-b
]
[1/6 ----- - 2/3 ---------- - 1/3 b/a]
[
a
1/3
]
[
a %1
]
[
1/3
2
[
%1
3ac-b
[- 1/12 ----- + 1/3 ---------- - 1/3 b/a
[
a
1/3
[
a %1
/
1/3
2\]
1/2 | %1
3 a c - b |]
+ 1/2 I 3 |1/6 ----- + 2/3 ----------|]
|
a
1/3 |]
\
a %1 /]
[
1/3
2
[
%1
3ac-b
[- 1/12 ----- + 1/3 ---------- - 1/3 b/a
[
a
1/3
[
a %1
/
1/3
2\]
1/2 | %1
3 a c - b |]
- 1/2 I 3 |1/6 ----- + 2/3 ----------|]
|
a
1/3 |]
\
a %1 /]
2
3
%1 := 36 b c a - 108 d a - 8 b
1/2
3 2 2
2 2
3 1/2
+ 12 3 (4 a c - c b - 18 b c a d + 27 d a + 4 d b )

80

% The statment
x = solve('p*sin(x) = r');
% chooses 'x' as the unknown and returns
x

x=

asin(r/p)
81

% A system of two quadratic equations in two unknowns


produces solution vectors.
[x,y] = solve('x^2 + x*y + y = 3','x^2 - 4*x + 3 = 0')

x=

1
3

y=

1
-3/2

82

% The solution can also be returned in a structure.


S = solve('x^2 + x*y + y = 3','x^2 - 4*x + 3 = 0')
S=
x: [2x1 sym]
y: [2x1 sym]
S.x
ans =
1
3

S.y
ans =
1
-3/2

83

% The next example regards 'a' as a parameter and solves two


% equations for u and v.
[u,v] = solve('a*u^2 + v^2 = 0','u - v = 1')

u=

1/2/(a+1)*(-2*a+2*(-a)^(1/2))+1
1/2/(a+1)*(-2*a-2*(-a)^(1/2))+1

v=

1/2/(a+1)*(-2*a+2*(-a)^(1/2))
1/2/(a+1)*(-2*a-2*(-a)^(1/2))

84

% Add a third equation and solve for all three unknowns.


[a,u,v] = solve('a*u^2 + v^2','u - v = 1','a^2 - 5*a + 6')

a=

2
2
3
3

u=

1/3+1/3*i*2^(1/2)
1/3-1/3*i*2^(1/2)
1/4+1/4*i*3^(1/2)
1/4-1/4*i*3^(1/2)

v=

-2/3+1/3*i*2^(1/2)
-2/3-1/3*i*2^(1/2)
-3/4+1/4*i*3^(1/2)
-3/4-1/4*i*3^(1/2)

85

% If an analytic solution cannot be found, "solve" returns a numeric


solution.
digits(32)
[x,y] = solve('sin(x+y)-exp(x)*y = 0','x^2-y = 2')

x=

-.66870120500236202933135901833637
-.33129879499763797066864098166363

y=

-1.5528386984283889912797441811191
-1.8902411084331130499424622177919

86

% Similar notation, with "D" denoting


differentiation, is used for
% for ordinary differential equations by the
"dsolve" function.
y = dsolve('Dy = -a*y')

y=

C1*exp(-a*t)

87

% Specify an initial condition.


y = dsolve('Dy = -a*y','y(0) = 1')

y=

exp(-a*t)

88

% The second derivative is denoted by "D2'.


y = dsolve('D2y = -a^2*y', 'y(0) = 1, Dy(pi/a) =
0')

y=

cos(a*t)

89

% A nonlinear equation produces two


solutions in a vector.
y = dsolve('(Dy)^2 + y^2 = 1','y(0) = 0')

y=

-sin(t)
sin(t)

90

Thank you.......

Anda mungkin juga menyukai