Clases i Mu Link
description
Transcript of Clases i Mu Link
Matlab: Symbolic Math Toolbox y Simulink
M.Sc. Luis Sánchez
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)^2ans = cos(theta)^2 + sin(theta)^2>> y = m*x + by = b + m*x
Expresiones simbólicasThe subs function substitutes values or expressions for variables in a symbolic expression. >> clear>> syms m x b>> y = m*x + b → y = b + m*x>> subs(y,x,3) → ans = b + 3*m>> subs(y, [m b], [2 3]) → ans = 2*x + 3>> subs(y, [b m x], [3 2 4])→ ans = 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) → f = cos(th)>> subs(f,pi) → ans = -1 Expressions can be substituted into variables. >> subs(f, z*pi) → ans = cos(pi*z)
Diferenciación
Use diff to do symbolic differentiation. >> clear>> syms m x b th n y
>> y = m*x + b;>> diff(y, x) → ans = m>> diff(y, b) → ans = 1
>> p = sin(th)^n → p = sin(th)^n>> diff(p, th) → ans = n*cos(th)*sin(th)^(n - 1)
Integración
Indefinite integrals
>> int(y, x) → ans = (m*x^2)/2 + b*x>> int(y, b) → ans = (b + m*x)^2/2>> int(1/(1+x^2)) → ans = atan(x) Definite integrals
>> int(y,x,2,5) → ans = 3*b + (21*m)/2>> int(1/(1+x^2),x,0,1) → ans = pi/4
>> clear>> syms m b x >> y = m*x + b;
Graficando expresiones simbólicas
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) )
Solución de Ecuaciones diferenciales con Matlab
2
2
n
n
y
dy
dt
d y
dt
d y
dt
• y
• Dy
• D2y
• Dny
1 2(0) and '(0)y C y C
2
2 1 02sin
d y dyb b b y A atdt dt
• >> 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
2 12dy
ydt
• >> y = dsolve('Dy + 2*y = 12', 'y(0)=10')
• y =
• 6+4*exp(-2*t)
• >> ezplot(y, [0 3])
• >> axis([0 3 0 10])
(0) 10y
• >> 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])
2 12sin 4dy
y tdt
(0) 10y
Ejemplo. Resolver la ED usando Matlab
• >> 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])
2
23 2 24
d y dyy
dt dt
(0) 10y '(0) 0y
Ejemplo. Resolver la ED usando Matlab
• >> 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]}
2
22 5 20
d y dyy
dt dt
(0) 0y '(0) 10y
Ejemplo. Resolver la ED usando Matlab
La trasformada de Laplace simbólica con Matlab
• Establezca s y t como variables simbólicas.•>> syms t s•La trasformada de laplace de una función f(t) se obtiene como:•>> F = laplace(f)•Algunas simplificaciones utilies son:•>> pretty(F)•>> simplify(F)
•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.
La trasformada inversa de Laplace simbólica con Matlab
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
2 2( ) 3 sin 5 4 cos5t tv t e t e t • >> 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. Determine la trasformada de Laplace de v(t) usando Matlab
Ejemplo. Continuación…
• >> 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)•
• >> 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)
2
100( 3)( )
( 1)( 2)( 2 5)
sF s
s s s s
Ejemplo. Determine la trasformada Inversa de Laplace de F(s) usando Matlab
• >> 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. Continuación…
2
10 48( )
2 ( 2)( 16)Y s
s s s
• >> syms t s• >> Y = 10/(s+2) + 48/((s+2)*(s^2+16)) • Y =• 10/(s+2)+48/(s+2)/(s^2+16)
Ejemplo. Determine la trasformada Inversa de Laplace de F(s) usando Matlab
• >> 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)
Ejemplo. Continuación…
Simulink Basics
click the Simulink button
the Simulink window
click the “new” button
the simulink model window
Simulink Basics
create a new model or open an existing one
• Build a Simulink model that solves the differential equation
• Initial condition
• First, sketch a simulation diagram of this mathematical model (equation)
(3 min.)
tx 2sin3
.1)0( x
Ejemplo. Modelo Simple
Diagrama de la Simulación
• Input is the forcing function 3sin(2t)• Output is the solution of the differential
equation x(t)
• Now build this model in Simulink
xxs1
3sin(2t)(input)
x(t)(output)
1)0( x
integrator
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 analytical result,
matches the plot (the simulation result) exactly.
ttx 2cos)( 23
21
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
)(tfkxxcxm
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.)
)(tfkxxcxm
(continue)
• First, solve for the term with highest-order derivative
• Make the left-hand side of this equation the output of a summing block
kxxctfxm )(
xm
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 highest-derivative alone
xm m1 x
summing block
Drag a Gain block from the Math library
Double-click to change the block parameters.Add a title.
The gain is 4 since 1/m=4.
(continue)
• Add integrators to obtain the desired output variable
xm m1
summing block
s1
s1x xx
Drag Integrator blocks from the Continuous library
Add a scope from the Sinks library.Connect output ports to input ports.Label the signals by double-clicking on the leader line.
ICs on the integrators are zero.
(continue)
• Connect to the integrated signals with gain blocks to create the terms on the right-hand side of the EOM
xm m1
summing block
s1
s1x x x
c
k
xc
kx
Drag new Gain blocks from the Math library
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.
To flip the gain block, select it and choose Flip Block in the Format pull-down menu.
c=0.5
k=1.0
Complete the model• Bring all the signals and inputs to the
summing block.• Check signs on the summer.
xm m1
s1
s1x x
c
k
xc
kx
f(t)input
+-
-x
x
xx(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