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.
Is there any way to express an expression in terms of other expressions in MATLAB?
For example, the following expressions have been written as sum (X + Y) and product (XY)
1/X + 1/Y = (X + Y)/XY
1/X^2 + 1/Y^2 + 2/(XY) = (X + Y)^2/(XY)
2*X/Y + 2*Y/X = 2*((X + Y)^2 - 2*X*Y)/(XY)
I know about the rewrite() function but I couldn't find how it can be used to do what I want?
There are a few different functions you can try to change the format of your symbolic expression:
collect: collects coefficients (can specify an expression to collect powers of):
>> collect(1/X + 1/Y)
ans =
(X + Y)/(Y*X)
simplify: perform algebraic simplification:
>> simplify(1/X^2 + 1/Y^2 + 2/(X*Y))
ans =
(X + Y)^2/(X^2*Y^2)
numden: convert to a rational form, with a numerator and denominator:
>> [n, d] = numden(2*X/Y + 2*Y/X)
n =
2*X^2 + 2*Y^2
d =
X*Y
>> n/d
ans =
(2*X^2 + 2*Y^2)/(X*Y)
I have a symbolic function that looks like this
syms x y(x) h
fn(x) = y + (h^2*(diff(y(x), x) + 2))/2 + (h^5*diff(y(x), x, x, x, x))/120 + (h^3*diff(y(x), x, x))/6 + (h^4*diff(y(x), x, x, x))/24 + h*(2*x + y(x) - 1)
I'd like to replace all instances of derivatives of y with its first derivative, i.e.
subs(fn, sym('diff(y(x), x)'), dy)
where dy is already defined as
dy(x) = 2*x + y(x) - 1
The result is the following:
ans(x) =
y + (h^2*(2*x + y(x) + 1))/2 + (h^5*diff(y(x), x, x, x, x))/120 + (h^3*diff(y(x), x, x))/6 + (h^4*diff(y(x), x, x, x))/24 + h*(2*x + y(x) - 1)
It replaces the first derivative, but not the higher derivatives. What I want is for the h^5 term to have (h^5*diff(dy(x), x, x, x). Is there any way to do that?
My current method is pretty hackish and involves converting the sym to a string, replacing first derivatives with dy, then converting back to a sym and evaluating to reduce the order of each term of the series by one, but it has to be recursive because at each stage derivatives of dy are then replaced by something containing diff(y, ...). I was hoping there would be a cleaner way to deal with this.
You need to keep in mind that Matlab treats things like diff(y,x) and diff(y,x,2) as distinct variables. It doesn't know how to substitute diff(y,x) into diff(y,x,2) because such a general operation for an abstract function (one with out an explicit definition, e.g., y(x)) is ill-defined.
How about something like this that performs substitution from the opposite end, starting with the highest order derivatives:
syms y(x) h
dy(x) = 2*x + y - 1
fn(x) = y + (h^2*(diff(y, x) + 2))/2 + (h^5*diff(y, x, 4))/120 + (h^3*diff(y, x, 2))/6 + (h^4*diff(y, x, 3))/24 + h*(2*x + y - 1)
fn2 = subs(fn, diff(y, x, 4), diff(dy, x, 3));
fn2 = subs(fn2, diff(y, x, 3), diff(dy, x, 2));
fn2 = subs(fn2, diff(y, x, 2), diff(dy, x));
fn2 = subs(fn2, diff(y, x), dy);
This returns
fn2(x) =
y(x) + (h^2*(2*x + y(x) + 1))/2 + (h^3*(2*x + y(x) + 1))/6 + (h^4*(2*x + y(x) + 1))/24 + (h^5*(2*x + y(x) + 1))/120 + h*(2*x + y(x) - 1)
Or you can leave dy(x) as an abstract symbolic expression initially:
syms y(x) dy(x) h
fn(x) = y + (h^2*(diff(y, x) + 2))/2 + (h^5*diff(y, x, 4))/120 + (h^3*diff(y, x, 2))/6 + (h^4*diff(y, x, 3))/24 + h*(2*x + y - 1)
fn2 = subs(fn, diff(y, x, 4), diff(dy, x, 3));
fn2 = subs(fn2, diff(y, x, 3), diff(dy, x, 2));
fn2 = subs(fn2, diff(y, x, 2), diff(dy, x));
fn2 = subs(fn2, diff(y, x), dy)
which returns
fn2(x) =
y(x) + (h^4*diff(dy(x), x, x))/24 + (h^2*(dy(x) + 2))/2 + (h^5*diff(dy(x), x, x, x))/120 + (h^3*diff(dy(x), x))/6 + h*(2*x + y(x) - 1)
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.
I have this equation in x and y:
(x + y)^(1/2) - 6*y*(x + y)^5 - (x + y)^6 + (x - 1)/(2*(x + y)^(1/2)) = 0.
Now I call the solver:
R_c = #(y)solve((x + y)^(1/2) - 6*y*(x + y)^5 - (x + y)^6 + (x - 1)/(2*(x + y)^(1/2)), x, 'Real', true);
which gives me the implicit solutions as a function of y. Now try
R_c(.3)
to find the explicit solution at y = 0.3. MATLAB's answer is:
ans =
0.42846617518653978966562924618638
0.15249587894102346284238111155954
0.12068186494007759990714181154349
However, the last entry in this array is NOT a solution. Test:
double(subs(subs((x + y)^(1/2) - 6*y*(x + y)^5 - (x + y)^6 + (x - 1)/(2*(x + y)^(1/2)), x, .12068186494007759990714181154349), y, .3))
yields
-0.0585.
This is not a rounding error. The other 2 solutions work perfectly and solve the equation correctly. I wonder where MATLAB the third value gets from. Can anyone help?