Matlab: Symbolic Math Toolbox y Simulink: M.Sc. Luis Sánchez
Matlab: Symbolic Math Toolbox y Simulink: M.Sc. Luis Sánchez
y Simulink
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
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
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)
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])
y (0) 10
y (0) 10
y '(0) 10
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.
>>syms t s
>> f = 5*t
f=
5*t
>> F = laplace(f)
F=
5/s^2
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)
>> 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)
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
Simulink Basics
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
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.
mx cx kx f (t )
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
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
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
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
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
+
-
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
Results