Factoring out a smaller equation, using symbolic math, from a larger equation

22 visualizaciones (últimos 30 días)
Greetings All!
Symbolic math is giving me an output here:
u_Version2 = Vin - L*dx1d - rL*x1d - L^2*kR1*x1d + L^2*kR1*x1
How can I tell Matlab to group the term (x1 - x1d) together? I have tried to use the "collect(u_Version2, x1 - x1d)" and "factor" functions, but no luck - Matlab just returns the same equation (no change). What I want is for Matlab to arrange terms like so:
u_Version2 = Vin - L*dx1d - rL*x1d - L^2*kR1*(x1 - x1d) (notice that "x1 - x1d" is now "collected" together)
In my near future, the above expression will get more complicated, so factoring out terms like "x1-x1d" will be very helpful.
Is there a way to do this?

Respuesta aceptada

John D'Errico
John D'Errico el 14 de Mzo. de 2022
Editada: John D'Errico el 14 de Mzo. de 2022
You were using collect in the wrong way. You were thinking in terms of what you wanted to see INSIDE the parens. Instead, you need to think about what those terms have in common. In your question, what they had in common was the sub-expression L^2*kR1.
Note that collect can help, at least to some extent. For example:
syms a b c d x y
U = a + b - c*d + x*a + x*c - y*c + d
U = 
collect(U,x)
ans = 
The idea of collect is it tries to express the result as a polynomial in the term you indicate. So here it found all terms that had x^1 in them, collecting them together.
However, it seems like collect is not as smart as I might want it to be. It looks like collect is able to best identify a single variable. For example, in V, we see that it did not recognize that there were three terms with x*y in them, and we might want to see it collect a factored term like x*y*(a - c*d +c).
V = x*y*a + b - x*y*c*d + x*y*c + d
V = 
collect(V,x*y)
ans = 
Now, I suppose you might do this:
syms w
Vw = subs(V,x*y,w)
Vw = 
And now you could try a collect on w, and then recover the expression we want to see.
subs(collect(Vw,w),w,x*y)
ans = 
That did work. (There may easily be other ways to accomplish what I did here too.) Regardless, sometimes directing a symbolic tool to do what you think is obvious can take a little effort.
Anyway, can collect handle your task?
syms Vin L dx1d rL x1d kR1 x1
u_Version2 = Vin - L*dx1d - rL*x1d - L^2*kR1*x1d + L^2*kR1*x1
u_Version2 = 
collect(u_Version2,L^2*kR1)
ans = 
As you can see, collect fails, if we try the direct approach. But the sneaky, indirect approach does work, as you see here:
subs(collect(subs(u_Version2,L^2*kR1,w),w),w,L^2*kR1)
ans = 
subs in this case is apparently a synonym for subterfuge.

Más respuestas (0)

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by