Solve system of equations depending on other syms variables
14 次查看(过去 30 天)
显示 更早的评论
I have two equations that I like to combine and solve for z. Since I have more unknowns than equations, the answer will be dependent on variables. How can I specify which variables the solution will be dependent on?
I created a code example:
syms x y z
e1 = x == 5*z;
e2 = y == 5*x + z;
eqns = [e1 e2];
z_ans1 = solve(eqns, [x,z])
%Works. Retruns struct with a solution for x and z.
z_ans2 = solve(e2, z)
%Works. Returns z_ans2 = y- 5*x
z_ans3 = solve(eqns, z)
%Doesn't work. Returns empty sym instead of z_ans3 = y/26. Probably because
%it dones't know that I like to eliminate x and keep y.
3 个评论
Torsten
2022-10-10
syms x y z
e1 = x == 5*z;
e2 = y == 5*x + z;
eqns = [e1 e2];
[~, z_ans] = solve(eqns, [x,z])
采纳的回答
Matt J
2022-10-10
编辑:Matt J
2022-10-10
This version solve(eqns, [x,z]) is correct because it communicates to solve() that x is not a constant.
With solve(eqns, z), the solver must assume that x is a constant, and therefore it concludes that a solution will normally not exist. For example, if we were to assign the values x=0, y=1 there can be no solution for z.
5 个评论
Torsten
2022-10-10
编辑:Torsten
2022-10-10
I feel like I created them precicly because I don't know them.
No, usually equations contain parameters and unknowns. In most cases you don't want to specify the parameters explicitly right at the beginning by giving them numerical values, but you want the equations to be solved more generally with the "parameter names" and substitute values for the parameters after the equations have been solved for the unknowns.
更多回答(1 个)
Matt J
2022-10-10
编辑:Matt J
2022-10-10
Plus I need to specify the variables to eliminate and not the ones that the solution should depend on.
Just because you are able to reach an expression z=y/26 by elimination of x doesn't mean the solution has no dependence on x. To be valid, the solution z also has to satisfy z=x/5, otherwise the first equation will not be satisfied. This is only possible when x and y are related according to x=5*y/26. If you tell the solver that relationship in advance, it can solve the equations quite handily:
syms x y z
x=5*y/26;
e1 = x == 5*z;
e2 = y == 5*x + z;
solve([e1,e2],z)
3 个评论
Matt J
2022-10-10
编辑:Matt J
2022-10-10
I'm just puzzled why this owrks but the solution up there with 2 equations not.
Well, your two equations were,
e1 = x == 5*z;
e2 = y == 5*x + z;
Let's take the candidate solution z=y/26 and plug back into the two equations and see what we get.
Equation e1 becomes : x=5*y/26
Equation e2 becomes : y=5*x +y/26
Are both of the equations satisfied? Yes, sometimes, but not always. It depends on the choice of x,y. If x=0 and y=1,, they are not satisfied. If y=0 and x=1, they are not satisfied.
If it so happens that x=5*y/26, then the equations will both be satisfied, but since you haven't told solve() that that is true in any way, solve() cannot conclude that the equations are satisfied.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!