How to make the response of the solve function symbolic? - matlab

I am solving a fourth order equation in matlab using the solve function.My script looks like this:
syms m M I L Bp Bc g x
m = 0.127
M = 1.206
I = 0.001
L = 0.178
Bc = 5.4
Bp = 0.002
g = 9.8
eqn = ((m + M)*(I + m*L^2) - m^2*L^2)*x^4 + ((m + M)*Bp + (I + m*L^2)*Bc)*x^3 + ((m + M)*m*g*L + Bc*Bp)*x^2 + m*g*L*Bc*x == 0
S = solve(eqn, x)
In the answer, I should get 4 roots, but instead I get such strange expressions:
S =
0
root(z^3 + (34351166180215288*z^2)/7131724328013535 + (352922208800606144*z)/7131724328013535 + 1379250971773894912/7131724328013535, z, 1)
root(z^3 + (34351166180215288*z^2)/7131724328013535 + (352922208800606144*z)/7131724328013535 + 1379250971773894912/7131724328013535, z, 2)
root(z^3 + (34351166180215288*z^2)/7131724328013535 + (352922208800606144*z)/7131724328013535 + 1379250971773894912/7131724328013535, z, 3)
The first root, which is 0, is displayed clearly. Is it possible to make the other three roots appear as numbers as well? I looked for something about this in the documentation for the solve function, but did not find it.

Related

How to solve a system of equations which contains symbols in MATLAB?

I am trying to solve those four equations for "p, x, y, z," , which contains two parameters "b", "a", and I have used solve command as shown in the code:
clc,clear;
syms p x y z b a
eqn1 = (1)*p + (exp(0.5*b^(1/3)))*x + (exp(-0.25*b^(1/3)) * cos(0.25*sqrt(3)*b^(1/3)))*y + (exp(-0.25*b^(1/3)) * sin(0.25*sqrt(3)*b^(1/3)))*z == 0;
eqn2 = (1)*p + (exp(-0.5*b^(1/3)))*x + (exp(0.25*b^(1/3)) * cos(0.25*sqrt(3)*b^(1/3)))*y - (exp(0.25*b^(1/3)) * sin(0.25*sqrt(3)*b^(1/3)))*z == 0;
eqn3 = (b^(1/3)*exp(0.5*b^(1/3))+a*b^(2/3)*exp(0.5*b^(1/3)))*x + (-.866*b^.333*sin(.433*b^.333)*exp(-.25*b^.333)-.5*cos(.433*b^.333)*b^.333*exp(-.25*b^.333) + a(-.5*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)+.866*b^.666*sin(.433*b^.333)*exp(-.25*b^.333)))*y + (.866*cos(.433*b^.333)*b^.333*exp(-.25*b^.333)-.5*b^.333*sin(.433*b^.333)*exp(-.25*b^.333)+a(-.5*b^.666*sin(.433*b^.333)*exp(-.25*b^.333)-.866*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)))*z == 0;
eqn4 = (b^(1/3)*exp(-0.5*b^(1/3))-a*b^(2/3)*exp(-0.5*b^(1/3)))*x + (.866*b^.333*sin(.433*b^.333)*exp(.25*b^.333)-.5*cos(.433*b^.333)*b^.333*exp(.25*b^.333) - a(-.5*b^.666*cos(.433*b^.333)*exp(.25*b^.333)-.866*b^.666*sin(.433*b^.333)*exp(.25*b^.333)))*y + (.866*cos(.433*b^.333)*b^.333*exp(.25*b^.333)+.5*b^.333*sin(.433*b^.333)*exp(.25*b^.333)-a(.5*b^.666*sin(.433*b^.333)*exp(.25*b^.333)-.866*b^.666*cos(.433*b^.333)*exp(.25*b^.333) ))*z == 0;
sol = solve([eqn1, eqn2, eqn3, eqn4 ], [p, x, y, z]);
pSol = sol.p
xSol = sol.x
ySol = sol.y
zSol = sol.z
, but it keeps showing errors, would you please suggest any solution to this issue.
Let's look at this :
eqn3 = (b^(1/3)*exp(0.5*b^(1/3))+a*b^(2/3)*exp(0.5*b^(1/3)))*x + (-.866*b^.333*sin(.433*b^.333)*exp(-.25*b^.333)-.5*cos(.433*b^.333)*b^.333*exp(-.25*b^.333) + a(-.5*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)+.866*b^.666*sin(.433*b^.333)*exp(-.25*b^.333)))*y + (.866*cos(.433*b^.333)*b^.333*exp(-.25*b^.333)-.5*b^.333*sin(.433*b^.333)*exp(-.25*b^.333)+a(-.5*b^.666*sin(.433*b^.333)*exp(-.25*b^.333)-.866*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)))*z == 0;
I pick one of the wrong parts out, 4 in total.
a(-.5*b^.666
You see, if a(index) exists, index must be a positive integer. However, in your code the value of -.5*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)+.866*b^.666*sin(.433*b^.333)*exp(-.25*b^.333) might not be a positive integer. And A may not even be a matrix, let alone an index.
Plus, before you post your question, you' d better check the basic grammar issues to save each other's time.

Extract specific parts of a matlab/ octave symbolic expression?

How does one extract specific parts of an expression in Matlab/ Octave symbolic package? In XCAS, one can use indexing expressions, but I can't find anything similar in Octave/ Matlab.
For instance, with X = C*L*s**2 + C*R*s + 1, is there a way to get C*R*s by X(2) or the like?
It would be nice to do this with factors too. X = (alpha + s)*(beta**2 + s**2)*(C*R*s + 1), and have X(2) give (beta**2 + s**2).
Thanks!
children (MATLAB doc, Octave doc) does this but the order in which you write the expressions will not necessarily be the same. The order is also different in MATLAB and Octave.
Expanded Expression:
syms R L C s;
X1 = C*L*s^2 + C*R*s + 1;
partsX1 = children(X1);
In MATLAB:
>> X1
X1 =
C*L*s^2 + C*R*s + 1
>> partsX1
partsX1 =
[ C*R*s, C*L*s^2, 1]
In Octave:
octave:1> X1
X1 = (sym)
2
C⋅L⋅s + C⋅R⋅s + 1
octave:2> partsX1
partsX1 = (sym 1×3 matrix)
⎡ 2 ⎤
⎣1 C⋅L⋅s C⋅R⋅s⎦
Factorised Expression:
syms R C a beta s; %alpha is also a MATLAB function so don't shadow it with your variable
X2 = (a + s) * (beta^2 + s^2) * (C*R*s + 1);
partsX2 = children(X2);
In MATLAB:
>> X2
X2 =
(a + s)*(C*R*s + 1)*(beta^2 + s^2)
>> partsX2
partsX2 =
[ a + s, C*R*s + 1, beta^2 + s^2]
In Octave:
octave:3> X2
X2 = (sym)
⎛ 2 2⎞
(a + s)⋅⎝β + s ⎠⋅(C⋅R⋅s + 1)
octave:4> partsX2
partsX2 = (sym 1×3 matrix)
⎡ 2 2⎤
⎣C⋅R⋅s + 1 a + s β + s ⎦

How to separate the real and imaginary parts of a transfer function?

Here is a transfer function:
S = [tf([10 2 4],[1 95 2000 3450])];
How can I get real(S) and Imag(S)?
It sounds like you want the Fourier form of your transfer function. As far as I know, there's no builtin function for this so you'll need to use symbolic math:
num = [10 2 4];
den = [1 95 2000 3450];
syms s;
syms omega real; % Define as real-valued
f1 = poly2sym(num,s)/poly2sym(den,s)
f2 = subs(f1,s,1i*omega)
f2_real = simplify(real(f2))
f2_imag = simplify(imag(f2))
which returns
f1 =
(10*s^2 + 2*s + 4)/(s^3 + 95*s^2 + 2000*s + 3450)
f2 =
(- 10*omega^2 + omega*2i + 4)/(- omega^3*1i - 95*omega^2 + omega*2000i + 3450)
f2_real =
(4*(237*omega^4 - 7720*omega^2 + 3450))/(omega^6 + 5025*omega^4 + 3344500*omega^2 + 11902500)
f2_imag =
-(2*omega*(5*omega^4 - 9907*omega^2 + 550))/(omega^6 + 5025*omega^4 + 3344500*omega^2 + 11902500)
You can then use subs and vpa/double to evaluate these for a particular value of omega.

Boolean expression F = x'y + xyz':

Using DeMorgan's theorem show that:
a. (A + B)'(A' +B)' = 0
b. A + A'B + A'B' = 1
Boolean expression F = x'y + xyz':
Derive an algebraic expression for the complement F'
Show that F·F' = 0
Show that F + F' = 1
Please Help me
Assuming you know how DeMorgan's law works and you understand the basics of AND, OR, NOT operations:
1.a) (A + B)'(A' + B)' = A'B'(A')'B' = A'B'AB' = A'AB'B' = A'AB' = 0 B' = 0.
I used two facts here that hold for any boolean variable A:
AA' = 0 (when A = 0, A' = 1 and when A = 1, A' = 0 so (AA') has to be 0)
0A = 0 (0 AND anything has to be 0)
1.b) A + A'B + A'B' = A + A'(B + B') = A + A' = 1.
I used the following two facts that hold for any boolean variables A, B and C:
AB + AC = A(B + C) - just like you would do with numeric variables and multiplication and addition. Only here we work with boolean variables and AND (multiplication) and OR (addition) operations.
A + A' = 0 (when A = 0, A' = 0 and when A = 1, A' = 0 so (A + A') has to be 1)
2.a) Let's first derive the complement of F:
F' = (x'y + xyz')' = (x'y)'(xyz')' = (x + y')((xy)' + z) = (x + y')(x' + y' + z) = xx' + xy' + xz + x'y' + y'y' + y'z = 0 + xy' + xz + x'y' + y' + y'z = xy' + xz + y'(x + 1) + y'z = xy' + xz + y' + y'z = xy' + xz + y'(z + 1) = xy' + y' + xz = y'(x + 1) = xz + y'.
There is only one additional fact that I used here, that for any boolean variables A and B following holds:
A + AB = A(B + 1) = A - logically, variable A completely determines the output of such an expression and part AB cannot change the output of entire expression (you can check this one with truth tables if it's not clear, but boolean algebra should be enough to understand). And of course, for any boolean variable A + 1 = A.
2.b) FF' = (x'y + xyz')(xz + y') = x'yxz + x'yy' + xyz'xz + xyz'y'.
x'yxz = (xx')yz = 0xz = 0
xyy'= x0 = 0
xyz'xz = xxy(zz') = xy0 = 0
xyz'y' = xz'(yy') = xz'0 = 0
Therefore, FF' = 0.
2.c) F + F' = x'y + xyz' + xz + y'
This one is not so obvious. Let's start with two middle components and see what we can work out:
xyz' + xz = x(yz' + z) = x(yz' + z(y + y')) = x(yz' + yz + y'z) = x(y(z + z') + y'z) = x(y + y'z) = xy + xy'z.
I used the fact that we can write any boolean variable A in the following way:
A = A(B + B') = AB + AB' as (B + B') evaluates to 1 for any boolean variable B, so initial expression is not changed by AND-ing it together with such an expression.
Plugging this back in F + F' expression yields:
x'y + xy + xy'z + y' = y(x + x') + y'(xz + 1) = y + y' = 1.

Can someone explain the simplification of this boolean algebra equation?

I think I missed reading a theorem or postulate or something..
The equation is: wy + wxz + xyz
According to my professor, the simplification is (which she didn't explain how):
wy + xz(w'y + wy')
= wy + xz (w XOR y)
Where did that (w'y + wy') came from??
I tried to calculate it but so far I only got: (w+x)(w+y)(w+z)(x+y)(y+z)
In a Boolean expression + is XOR and * is AND. This is the same as identifying true with 1 and false with 0, with the only special convention that 1 + 1 = 0 (or 2 = 0 if you wish.)
With these definitions both + and * are commutative, i.e., a + b = b + a and a * b = b * a. In addition, the distributive law is also valid a * (b + c) = a * b + a * c. Note also that the * operator is usually implicit in the sense that we write ab instead of a * b.
Applying these properties to the expression wy + wxz + xyz, there are some few obvious transformations you can do:
wy + wxz + yxz (commute x with y)
wy + (w + y)xz (prev plus distribute xz)
wy + xz(w + y) (prev plus commute (w + y) with xz
Note that the last one is wy + xz(w XOR y) because + is nothing but XOR.
ADDENDUM
Regarding the expression of your professor, let's recall that a' = 1 - a by definition. So
w'y + wy' = (1 - w)y + w(1 - y) - def
= y - wy + w - wy - distribute
= y + w - simplify (a + a = 0 always)
= w + y - commute
which shows that s/he was right.