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
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])
z_ans = 
Philipp Kutschmann
Philipp Kutschmann 2022-10-10
Thank you. I didn't know about the ~ opperator. I will definitely use it here.

请先登录,再进行评论。

采纳的回答

Matt J
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
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.
Philipp Kutschmann
Philipp Kutschmann 2022-10-10
Am I right to assume this is because we want to keep precision and plugging in the variables before solving might lead to (arguably small) numeric errors? Another reason I might think of is that later changing the variables only needs plugging in the stored and already solved equation therefore saving compute time. On the other hand if one doesn’t care that much about the precision and doesn’t need to store the solved equation for future use, the solving process might be faster if the values are plugged in right at the beginning. On the other hand one might argue that if precision is not as important as compute time, you should go for a numeric method instead of an analytical one in the fist place. Do I have any mistakes in my reasoning here?

请先登录,再进行评论。

更多回答(1 个)

Matt J
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)
ans = 
  3 个评论
Matt J
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.
Philipp Kutschmann
Philipp Kutschmann 2022-10-11
ok, to restate your explanation it in my own words: solve trys to find a solution for the given unknown that always satisfies the given equations for all values other syms might have. They are preceived as arbitrary and fixed numbers and not as other (unknown) variables. Therefore just plugging in the first equation in the seccond one might result in a solution that is not valid for all x-y value pairs (to stay with the above example). If x is directly defined as 5*z and not in an equation, MATLAB donesn't see it as a constant number, as it is already assigned.
The entire prossedure makes a bit more sense when choosing a example form physics for example:
syms m v g h E
E_pod = E == m*g*h;
E_kin = E == 0.5*m*v^2;
eqns =[E_kin E_pod];
[v_sol, ~] = solve(eqns,[v,E]);
double(subs(v_sol,[g,h],[9.81,10]))
ans = 2×1
14.0071 -14.0071
Clearly here m, g and h are given and known and will only be plugged in later.
Thanks for your explanations, time and patience.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Formula Manipulation and Simplification 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by