clases i mu link
DESCRIPTION
uniTRANSCRIPT
![Page 1: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/1.jpg)
Matlab: Symbolic Math Toolbox y Simulink
M.Sc. Luis Sánchez
![Page 2: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/2.jpg)
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, …
![Page 3: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/3.jpg)
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
![Page 4: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/4.jpg)
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
![Page 5: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/5.jpg)
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
![Page 6: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/6.jpg)
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)
![Page 7: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/7.jpg)
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)
![Page 8: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/8.jpg)
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;
![Page 9: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/9.jpg)
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);
![Page 10: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/10.jpg)
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) )
![Page 11: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/11.jpg)
Solución de Ecuaciones diferenciales con Matlab
2
2
n
n
y
dy
dt
d y
dt
d y
dt
• y
• Dy
• D2y
• Dny
![Page 12: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/12.jpg)
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])
![Page 13: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/13.jpg)
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
![Page 14: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/14.jpg)
• >> 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
![Page 15: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/15.jpg)
• >> 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
![Page 16: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/16.jpg)
• >> 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
![Page 17: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/17.jpg)
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)
![Page 18: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/18.jpg)
•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
![Page 19: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/19.jpg)
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
![Page 20: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/20.jpg)
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
![Page 21: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/21.jpg)
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)•
![Page 22: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/22.jpg)
• >> 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
![Page 23: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/23.jpg)
• >> 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…
![Page 24: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/24.jpg)
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
![Page 25: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/25.jpg)
• >> 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…
![Page 26: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/26.jpg)
Simulink Basics
click the Simulink button
the Simulink window
![Page 27: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/27.jpg)
click the “new” button
the simulink model window
Simulink Basics
create a new model or open an existing one
![Page 28: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/28.jpg)
• 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
![Page 29: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/29.jpg)
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
![Page 30: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/30.jpg)
Select an input block
Drag a Sine Wave block from the Sources library to the model window
![Page 31: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/31.jpg)
Select an operator block
Drag an Integrator block from the Continuous library to the model window
![Page 32: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/32.jpg)
Select an output block
Drag a Scope block from the Sinks library to the model window
![Page 33: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/33.jpg)
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.
![Page 34: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/34.jpg)
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)
![Page 35: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/35.jpg)
Select simulation parameters
Double-click on the Integrator block to set initial condition = -1.
This sets our IC x(0) = -1.
![Page 36: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/36.jpg)
Select simulation parameters
Double-click on the Scope to view the simulation results
![Page 37: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/37.jpg)
Run the simulation
In the model window, from the Simulation pull-down menu, select Start
View the output x(t) in the Scope window.
![Page 38: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/38.jpg)
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
![Page 39: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/39.jpg)
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
![Page 40: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/40.jpg)
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
![Page 41: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/41.jpg)
(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
![Page 42: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/42.jpg)
Drag a Sum block from the Math library
Double-click to change the block parameters to rectangular and + - -
![Page 43: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/43.jpg)
(continue)
• Add a gain (multiplier) block to eliminate the coefficient and produce the highest-derivative alone
xm m1 x
summing block
![Page 44: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/44.jpg)
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.
![Page 45: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/45.jpg)
(continue)
• Add integrators to obtain the desired output variable
xm m1
summing block
s1
s1x xx
![Page 46: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/46.jpg)
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.
![Page 47: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/47.jpg)
(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
![Page 48: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/48.jpg)
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
![Page 49: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/49.jpg)
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
![Page 50: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/50.jpg)
Double-click on Step block to set parameters. For a step input of magnitude 3, set Final value to 3
![Page 51: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/51.jpg)
Final Simulink model
![Page 52: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/52.jpg)
Run the simulation
![Page 53: Clases i Mu Link](https://reader035.vdocuments.net/reader035/viewer/2022062518/55cf8f60550346703b9bbc80/html5/thumbnails/53.jpg)
Results