0% found this document useful (0 votes)
30 views53 pages

Matlab: Symbolic Math Toolbox y Simulink: M.Sc. Luis Sánchez

The document describes the capabilities of the Matlab Symbolic Math Toolbox and Simulink. The Symbolic Math Toolbox allows users to perform symbolic mathematics like calculus, linear algebra, simplification, equation solving and transforms. Simulink is a block diagram environment for modeling, simulating and analyzing dynamic systems. Examples shown include using Symbolic Math Toolbox to solve equations symbolically and plot expressions, as well as building Simulink models to solve differential equations numerically through integration.

Uploaded by

kyomich
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views53 pages

Matlab: Symbolic Math Toolbox y Simulink: M.Sc. Luis Sánchez

The document describes the capabilities of the Matlab Symbolic Math Toolbox and Simulink. The Symbolic Math Toolbox allows users to perform symbolic mathematics like calculus, linear algebra, simplification, equation solving and transforms. Simulink is a block diagram environment for modeling, simulating and analyzing dynamic systems. Examples shown include using Symbolic Math Toolbox to solve equations symbolically and plot expressions, as well as building Simulink models to solve differential equations numerically through integration.

Uploaded by

kyomich
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 53

Matlab: Symbolic Math Toolbox

y Simulink

M.Sc. Luis Snchez

Symbolic Math Toolbox


Permite:
Calculus integration, differentiation, Taylor series
expansion,
Linear Algebra inverses, determinants, eigenvalues,
Simplification algebraic and trigonometric expressions
Equation Solutions algebraic and differential equations
Transforms Fourier, Laplace, Z transforms and inverse
transforms,

Objeto Symbolic
Use sym to create a symbolic number, and double to
convert to a normal number.
>> sqrt(2)
ans = 1.4142
>> var = sqrt(sym(2))
var = 2^(1/2)
>> double(var)
ans = 1.4142
>> sym(2)/sym(5) + sym(1)/sym(3)
ans = 11/15

Symbolic variables
Use syms to define symbolic variables. (Or use sym to create
an abbreviated symbol name.)
>> syms m n b c x
>> th = sym('theta')
>> sin(th)
ans = sin(theta)
>> sin(th)^2 + cos(th)^2
ans = cos(theta)^2 + sin(theta)^2
>> y = m*x + b
y = b + m*x

Expresiones simblicas
The subs function substitutes values or expressions for
variables in a symbolic expression.
>>
>>
>>
>>
>>
>>

clear
syms m x b
y = m*x + b
y = b
subs(y,x,3)
ans =
subs(y, [m b], [2 3]) ans =
subs(y, [b m x], [3 2 4]) ans

+ m*x
b + 3*m
2*x + 3
= 11

The symbolic expression itself is unchanged.


>> y

y = b + m*x

Substitutions, continued
Variables can hold symbolic expressions.
>> syms th z
>> f = cos(th)
>> subs(f,pi)

f = cos(th)
ans = -1

Expressions can be substituted into variables.


>> subs(f, z*pi) ans = cos(pi*z)

Diferenciacin
Use diff to do symbolic differentiation.
>> clear
>> syms m x b th n y
>> y = m*x + b;
>> diff(y, x)
>> diff(y, b)
>> p = sin(th)^n
>> diff(p, th)
- 1)

ans = m
ans = 1
p = sin(th)^n
ans = n*cos(th)*sin(th)^(n

Integracin
>> clear
>> syms m b x

>> y = m*x + b;

Indefinite integrals
>> int(y, x)
b*x
>> int(y, b)
>> int(1/(1+x^2))

ans = (m*x^2)/2 +

ans = (b + m*x)^2/2
ans = atan(x)

ans = 3*b +

ans = pi/4

Definite integrals
>> int(y,x,2,5)
(21*m)/2
>> int(1/(1+x^2),x,0,1)

Graficando expresiones simblicas


The ezplot function will plot symbolic expressions.
>>
>>
>>
>>
>>

clear; syms x y
ezplot( 1 / (5 + 4*cos(x)) );
hold on; axis equal
g = x^2 + y^2 - 3;
ezplot(g);

Ejemplo
>> clear; syms x
>> digits(20)
>> [x0, y0] = solve(' x^2 + y^2 - 3 = 0', ...
'y = 1 / (5 + 4*cos(x))
')
x0 = -1.7171874987452662214
y0 = 0.22642237997374799957
>> plot(x0,y0,'o')
>> hold on
>> ezplot( diff( 1 / (5 + 4*cos(x)), x) )

Solucin de Ecuaciones
diferenciales con Matlab
y
dy
dt
2
d y
2
dt
n
d y
n
dt

y
Dy

D2y

Dny

d y
dy
b2 2 b1 b0 y A sin at
dt
dt
y (0) C1 and y '(0) C2
>> y = dsolve(b2*D2y+b1*D1y+b0*y=A*sin(a*t),

y(0)=C1, Dy(0)=C2)
>> ezplot(y, [t1 t2])

Ejemplo. Resolver la ED usando


Matlab
dy
2 y 12
dt

y (0) 10

>> y = dsolve('Dy + 2*y = 12', 'y(0)=10')


y=
6+4*exp(-2*t)
>> ezplot(y, [0 3])
>> axis([0 3 0 10])

Ejemplo. Resolver la ED usando


Matlab
dy
2 y 12sin 4t
dt

y (0) 10

>> y = dsolve('Dy + 2*y = 12*sin(4*t)',


'y(0)=10')
y=
-12/5*cos(4*t)+6/5*sin(4*t)+62/5*exp(-2*t)
>> ezplot(y, [0 8])
>> axis([0 8 -3 10])

Ejemplo. Resolver la ED usando


Matlab
2
d y
dy
3 2 y 24
2
dt
dt
y (0) 10 y '(0) 0

>> y = dsolve('D2y + 3*Dy + 2*y = 24',


'y(0)=10', 'Dy(0)=0')
y=
12+2*exp(-2*t)-4*exp(-t)
>> ezplot(y, [0 6])

Ejemplo. Resolver la ED usando


Matlab
2
d y
dy
2 5 y 20
2
dt
dt
y (0) 0

y '(0) 10

>> y = dsolve('D2y + 2*Dy + 5*y = 20',

'y(0) = 0', 'Dy(0) = 10')


y=
4+3*exp(-t)*sin(2*t)-4*exp(-t)*cos(2*t)
>>ezplot(y, [0 5]}

La trasformada de Laplace
simblica con Matlab
Establezca s y t como variables
simblicas.
>> syms t s
La trasformada de laplace de una
funcin f(t) se obtiene como:
>> F = laplace(f)
Algunas simplificaciones utilies son:
>> pretty(F)
>> simplify(F)

La trasformada inversa de
Laplace simblica con Matlab
Establish t and s as symbolic variables.
>> syms t s
The Laplace function F is then formed and
the inverse Laplace transform command is
>> f = ilaplace(F)
The simplification operations may also be
useful for inverse transforms.

Ejemplo. Determine la trasformada


de Laplace de f(t)=5t usando
Matlab

>>syms t s
>> f = 5*t
f=
5*t
>> F = laplace(f)
F=
5/s^2

Ejemplo. Determine la trasformada


de Laplace de v(t) usando Matlab
v(t ) 3e

2 t

sin 5t 4e

2 t

cos 5t

>> syms t s
>> v = 3*exp(-2*t)*sin(5*t)
+ 4*exp(-2*t)*cos(5*t)
v=
3*exp(-2*t)*sin(5*t)+4*exp(-2*t)*cos(5*t)

Ejemplo. Continuacin
>> V = laplace(v)
V=
15/((s+2)^2+25)+4*(s+2)/((s+2)^2+25)
>> V=simplify(V)
V=
(23+4*s)/(s^2+4*s+29)

Ejemplo. Determine la trasformada


Inversa de Laplace de F(s) usando
Matlab
100( s 3)
F ( s)
2
( s 1)( s 2)( s 2 s 5)

>> syms t s
>> F=100*(s+3)/((s+1)*(s+2)*(s^2+2*s+5))
F=
(100*s+300)/(s+1)/(s+2)/(s^2+2*s+5)

Ejemplo. Continuacin
>> f = ilaplace(F)
f=
50*exp(-t)-20*exp(-2*t)-30*exp(-t)*cos(2*t)10*exp(-t)*sin(2*t)
>> pretty(f)

50 exp(-t) - 20 exp(-2 t) - 30 exp(-t) cos(2 t) 10 exp(-t) sin(2 t)

Ejemplo. Determine la trasformada


Inversa de Laplace de F(s) usando
Matlab
10
48
Y ( s)

2
s 2 ( s 2)( s 16)

>> syms t s
>> Y = 10/(s+2) + 48/((s+2)*(s^2+16))
Y=
10/(s+2)+48/(s+2)/(s^2+16)

Ejemplo. Continuacin
>> y = ilaplace(Y)
y=
62/5*exp(-2*t)-12/5*cos(16^(1/2)*t)
+3/10*16^(1/2)*sin(16^(1/2)*t)
>> y=simplify(y)
y=
62/5*exp(-2*t)-12/5*cos(4*t)+6/5*sin(4*t)

Simulink Basics

click the Simulink button

the Simulink window

Simulink Basics

click the new button

create a new model or


open an existing one

the simulink model window

Ejemplo. Modelo Simple


Build a Simulink model that solves the
differential equation

x 3 sin 2t
x(0) 1.
Initial condition
First, sketch a simulation diagram of this
mathematical model (equation)
(3 min.)

Diagrama de la Simulacin
Input is the forcing function 3sin(2t)
Output is the solution of the differential
equation x(t)
x(0) 1

3sin(2t)
(input)

1
s

x(t)
(output)

integrator

Now build this model in Simulink

Select an input block


Drag a Sine Wave
block from the Sources
library to the model
window

Select an operator block


Drag an Integrator
block from the
Continuous library to
the model window

Select an output block


Drag a Scope block from
the Sinks library to the
model window

Connect blocks with signals


Place your cursor on
the output port (>) of
the Sine Wave block
Drag from the Sine
Wave output to the
Integrator input
Drag from the
Integrator output to
the Scope input

Arrows indicate the


direction of the signal
flow.

Select simulation parameters


Double-click on
the Sine Wave
block to set
amplitude = 3
and freq = 2.
This produces
the desired
input of 3sin(2t)

Select simulation parameters


Double-click
on the
Integrator
block to set
initial
condition =
-1.
This sets our
IC x(0) = -1.

Select simulation parameters


Double-click on
the Scope to
view the
simulation
results

Run the simulation


In the model
window, from
the Simulation
pull-down
menu, select
Start
View the output
x(t) in the Scope
window.

Simulation results
To verify that this
plot represents
the solution to the
problem, solve the
equation
analytically.
The
x(t )analytical
12 32 cos 2t
result,
matches the plot
(the simulation
result) exactly.

Ejemplo. EDO 2 orden


Build a Simulink model that solves the
following differential equation
2nd-order mass-spring-damper system
zero ICs
input f(t) is a step with magnitude 3
parameters: m = 0.25, c = 0.5, k = 1

mx cx kx f (t )

Create the simulation diagram


On the following slides:
The simulation diagram for solving the ODE is
created step by step.
After each step, elements are added to the
Simulink model.

Optional exercise: first, sketch the


complete diagram (5 min.)

mx cx kx f (t )

(continue)
First, solve for the term with highestorder derivative
mx f (t ) cx kx
Make the left-hand side of this equation
the output of a summing block
mx

summing
block

Drag a Sum block


from the Math library

Double-click to change
the block parameters
to rectangular and + - -

(continue)
Add a gain (multiplier) block to eliminate
the coefficient and produce the highestderivative alone
mx

summing
block

1
m

Drag a Gain block


from the Math library

The gain is 4 since 1/m=4.

Double-click to change
the block parameters.
Add a title.

(continue)
Add integrators to obtain the desired
output variable
mx

summing
block

1
m

1
s

1
s

Drag Integrator blocks


from the Continuous
library

ICs on the
integrators are
zero.
Add a scope from the Sinks library.
Connect output ports to input ports.
Label the signals by double-clicking on the leader
line.

(continue)
Connect to the integrated signals with gain
blocks to create the terms on the right-hand
side of the EOM
mx

summing
block

1
m

x
cx

1
s

c
kx

1
s

Drag new Gain


blocks from the
Math library

To flip the gain block,


select it and choose Flip
Block in the Format pulldown menu.

Double-click on gain
blocks to set parameters
Connect from the gain
block input backwards up
to the branch point.
Re-title the gain blocks.

c=0.5

k=1.0

Complete the model


Bring all the signals and inputs to the
summing block.
Check signs on the summer.
f(t)
input

+
-

mx 1
m

x
cx
kx

1
s

1
s

c
k

x(t)
output

Double-click on Step
block to set parameters.
For a step input of
magnitude 3, set Final
value to 3

Final Simulink model

Run the simulation

Results

You might also like