Clear Filters
Clear Filters

symbolic equation system solving with a lot of simple equations

1 view (last 30 days)
I have got a lot of very simple symbolic equations and i want to solve the equation system for the vars but for some reason i cannot succed in it. Here is an example:
syms u1 u2 I1 I2 I3 I4 C1 C2 R1 R2 a b e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11 e12 e13 e14 e15 e16 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 dx1 dx2 dx3 dx4 dx5 dx6 X1 X2 X3 X4 X5 X6
eqns = [e1==u1, e1==e2+e3+e4, f1==f2, f2==f3, f3==f4, e2==R1*f2, e3==dx1, f3==X1/I1, e4==a*f5, f4==e5/a, e5==e6+e7+e8, f5==f6, f6==f7, f7==f8, e6==R2*f6, e7==dx2, f7==X2/I2, e8==b*e9, f8==f9/b, e9==e10, e10==e11, f9==f10+f11, e10==X3/C1, f10==dx3, e11==e12+e13+e14, f11==f12, f12==f13, f13==f14, e12==u2, e13==dx4, f13==X4/I3, e14==e15, e15==e16, f14==f15+f16, e15==X5/C2, f15==dx5, e16==dx6, f16==X6/I4];
var = [dx1 dx2 dx3 dx4 dx5 dx6];
sol=solve(eqns,var)
As you can see i've got a lot of variables too and i want to eliminate all of the e. and f. -s (e1,e2,...e16,f1,f2,...,f16) and get is solved for dx.-es. I can solve it by hand so it shouldn't be an issue with matlab (this is the result: "Empty sym: 0-by-1"), possibly i just missed some important step.
Do you think it is even possible to solve the symbolic equations this way or should i try some different method?

Accepted Answer

John D'Errico
John D'Errico on 20 Apr 2018
Edited: John D'Errico on 20 Apr 2018

People seem to think that shorthand that makes sense to them is always going ot make sense to a computer.

syms f1 f2 f3 f4

When use the following form, what does MATLAB see?

f1==f2==f3==f4
ans =
((f1 == f2) == f3) == f4

Can we "solve" that problem? What if we added one more equation, that f4==1? That should make the problem well posed.

solve(f1==f2==f3==f4,f4 == 1)
ans = 
  struct with fields:
    f1: [0×1 sym]
    f2: [0×1 sym]

No solution was found.

The problem is YOU understand a transitive shorthand like this: a=b=c=d, to imply that all of them are equal. MATLAB does not take it that way.

Instead, you need to set the problem up with separate equations.

S = solve(f1==f2, f2==f3, f3==f4, f4 == 1)
S = 
struct with fields:
    f1: [1×1 sym]
    f2: [1×1 sym]
    f3: [1×1 sym]
    f4: [1×1 sym]

And we see that S.f1 is what we expect.

S.f1
ans =
1
  2 Comments
bobby27
bobby27 on 20 Apr 2018
You are right, that was a problem. I just edited the code in the original question the way you advised, however still cannot get it working in matlab. Could it be because i never given an exact value to any of the variables like u did in your example (f4==1) or it should work without it anyway?
John D'Errico
John D'Errico on 30 Apr 2018
It is entirely because you have not specified sufficient information.
Suppose I gave you this problem, written as you did:
x == y == z == w
But nothing more. That is, if we untangle the transitive equalities:
x == y
y == z
z == w
But I told you NOTHING else. Could you "solve" the problem? Surely not. There are three equations with 4 unknowns. The result will always be ambiguous.
Of course, yes, you could solve it in terms of one of the variables. Pick w to be held fixed. The result would then be
x = w
y = w
z = w
Where here w is a parameter that you have not specified. We can think of the solution as an infinite family of solutions. Pick any value for w.
But if you want an automatically explicit solution where all variables are found to have some value? There is insufficient information, unless you choose some variable to be left out.
That is what I did when I added that extra piece of information, there, f4==1. Lets try it.
syms x y z w
sol = solve(x==y,y==z,z==w)
sol =
struct with fields:
w: [1×1 sym]
x: [1×1 sym]
y: [1×1 sym]
sol.x
ans =
z
sol.y
ans =
z
sol.w
ans =
z
So, as predicted, we can solve for any 3 of the 4 variables, in terms of the other(s). That is because we have 3 equations in 4 unknowns. Above, solve was actually smart enough to see that it could do so, thus set one parameter to the side. Solve need not be always that smart. If you have a sufficiently large system, it could be confused.
What you can do is to specify which of the variables will be solved for, in terms of the others. In the case of a linear system like this, just make sure that you will solve for exactly as many variables as you have equations. So write it like this:
sol = solve(x==y,y==z,z==w,x,y,z)
Three equations, three variables to be solved for. Now solve understands that it is asked to solve for x,y,z, BUT to do so in terms of w as an unknown parameter.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!