Solving symbolic equations with extra equations

9 views (last 30 days)
Jack
Jack on 12 Nov 2018
Edited: John D'Errico on 13 Nov 2018
I am trying to solve some simultaneous equations that are becoming too time consuming and erroneous to solve by hand. My problem boils down to trying to solve for variables which do not require all of the equations to solve it. For example:
eqn= [ c1*g1 + a20/g1 - b20/g1 == 0
a11 - b11 + (a20*g2)/g1 - (b20*g2)/g1 == 0
a01 - b01 + a11/g1 - b11/g1 == 0
a02 - b02 + (a11*g2)/g1 - (b11*g2)/g1 == 0];
Where all of the variables are symbolic. I want to solve for b20, b11, and b02 in terms of everything else but I don't know which equations are required (1,2,4). If I solve in just [b20, b11, b02] I get:
>> solve(eqn,[b20, b11, b02])
ans =
struct with fields:
b20: [0×1 sym]
b11: [0×1 sym]
b02: [0×1 sym]
As there is more equations than variables, I guess. Whereas if I do the following:
>> solve(eqn([1,2,4]),[b20, b11, b02])
ans =
struct with fields:
b20: [1×1 sym]
b11: [1×1 sym]
b02: [1×1 sym]
b20 =
a20 + c*g1^2
b11 =
a11 + c*g1*g2
b02 =
a02 + c*g2^2
Which match the solutions I got by hand. Any ideas on how to solve without knowing which equations are needed?
  1 Comment
Torsten
Torsten on 12 Nov 2018
Not possible since you get different solutions depending on whether you choose [1 2 4] or [1 3 4] as underlying equations.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 12 Nov 2018
You show A set of solutions that you got by hand. But do they satisfy all the equations? No. Of the unknown variables you solved for, only one of them appears in eqn(3).
Sol = solve(eqn([1 2 4]),[b20, b11, b02]);
simplify(subs(eqn(3),b11,Sol.b11))
ans =
a01 + c1*g2 == b01
So ONLY if that relationship holds true for ALL values of {a01, c1,g2, b01}, then you found a solution. But there is no reason to know why you would have chosen that equation to ignore, or why you should. And there is certainly no magical way for MATLAB to know what you wanted to see for a solution.
Worse, you could have picked a completely different subset of equations to solve.
Sol124 = solve(eqn([1 2 4]),[b20, b11, b02])
Sol124 =
struct with fields:
b20: [1×1 sym]
b11: [1×1 sym]
b02: [1×1 sym]
Sol124.b11
ans =
a11 - c1*g1*g2
Sol134 = solve(eqn([1 3 4]),[b20, b11, b02])
Sol134 =
struct with fields:
b20: [1×1 sym]
b11: [1×1 sym]
b02: [1×1 sym]
Sol134.b11
ans =
a11 + a01*g1 - b01*g1
Sol234 = solve(eqn([2 3 4]),[b20, b11, b02]) Sol234 = struct with fields:
b20: [1×1 sym]
b11: [1×1 sym]
b02: [1×1 sym]
Sol234.b11
ans =
a11 + a01*g1 - b01*g1
Why is one solution better than the other? Don't get all excited either, in that equations 2, 3, & 4 seem to give the same solution as 1, 3, & 4 did. They don't give consistently the same solutions for each variable.
[Sol124.b20;Sol134.b20;Sol234.b20]
ans =
c1*g1^2 + a20
c1*g1^2 + a20
(a20*g2 - a01*g1^2 + b01*g1^2)/g2
[Sol124.b11;Sol134.b11;Sol234.b11]
ans =
a11 - c1*g1*g2
a11 + a01*g1 - b01*g1
a11 + a01*g1 - b01*g1
[Sol124.b02;Sol134.b02;Sol234.b02]
ans =
c1*g2^2 + a02
a02 - a01*g2 + b01*g2
a02 - a01*g2 + b01*g2
Sorry, but you cannot ask MATLAB to solve this for you in any rational way. 4 independent equations in 3 unknowns is asking for the impossible. Sometimes you will get lucky, but that will not happen here.
  3 Comments
Torsten
Torsten on 13 Nov 2018
Edited: Torsten on 13 Nov 2018
Use the four equations together with four of the b variables. The solution will then be written in terms of the other variables with the fifth b variable as free parameter.
Best wishes
Torsten.
John D'Errico
John D'Errico on 13 Nov 2018
Edited: John D'Errico on 13 Nov 2018
So you are now treating this as a system of 4 equations in 5 unknowns. But now you want to introduce one additional parameter, c1 that you want to vary. In fact, all of those parameters are treated as parameters that you could vary, once a solution is found.
So lets start over, given the rest of the information you have now told us.
You have a linear system in the b variables, thus [b20, b11, b02, b10, b01]. But you have only 4 equations in 5 unknowns, so the system is under-determined. I'll first write the problem as a linear system.
syms b20 b11 b02 b10 b01 a10 a20 g1 a11 g2 a01 b01
eqn = [ a10 - b10 + a20/g1 - b20/g1 == 0
a11 - b11 + (a20*g2)/g1 - (b20*g2)/g1 == 0
a01 - b01 + a11/g1 - b11/g1 == 0
a02 - b02 + (a11*g2)/g1 - (b11*g2)/g1 == 0];
I'll let MATLAB do the conversion.
[Aeq,beq] = equationsToMatrix(eqn,[b20, b11, b02, b10, b01])
Aeq =
[ -1/g1, 0, 0, -1, 0]
[ -g2/g1, -1, 0, 0, 0]
[ 0, -1/g1, 0, 0, -1]
[ 0, -g2/g1, -1, 0, 0]
beq =
- a10 - a20/g1
- a11 - (a20*g2)/g1
- a01 - a11/g1
- a02 - (a11*g2)/g1
You cannot solve the problem until you add one additional piece of information, in some form.
In fact, the following linear combination of your unknowns is unspecified:
[b20, b11, b02, b10, b01]*null(Aeq)
ans =
b01 + b02*g2 - b11*g1 + (b20*g1^2)/g2 - (b10*g1)/g2
One way or another, in order to find a solution, you need to provide one more piece of information. Your decision was to create the new parameter c1. (By the way, you should learn to use useful variable names in the future. If not, then this obsession with numbered variables will lead you into programming hell before long.) But if you do create the variable c1 as you wanted, then just do this:
syms c1
[Aeq;0 0 0 0 1]\[beq;a01 + c1*g2]
Bsol = [Aeq;0 0 0 0 1]\[beq;a01 + c1*g2]
Bsol =
c1*g1^2 + a20
a11 - c1*g1*g2
c1*g2^2 + a02
a10 - c1*g1
a01 + c1*g2
Or you could as easily have appended that new equation involving c1 to the set in eqn, then used solve. Personally, I prefer to use a linear solve when I know I have a linear system, rather than just throwing it at solve. But solve would have worked just as well here.

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!