Solving System of Equations with Symbolic Toolbox

1 次查看(过去 30 天)
I'm trying to solve a series of equations using the symbolic math toolbox, and I'm not sure why two methods aren't equivalent. Basically, I'm using the solution from the 1st equation in the 2nd, the solutions for the 1st and 2nd in the 3rd, and so forth. When I write it out by hand, it looks something like:
Fist Step:
Solution(1) = solve(Equation(1),X(1));
Second Step:
X(1)=Cut and paste of Solution(1);
Solution(2)=solve(Equation(2),X(2));
Third Step:
X(2)=Cut and paste of Solution(2);
X(1)=Cut and paste of Solution(1);
Solution(3)=solve(Equation(3),X(3));
Fourth Step:
X(3)=Cut and paste of Solution(3);
X(2)=Cut and paste of Solution(2);
X(1)=Cut and paste of solution(1);
Solution(4)=solve(Equation(4),X(4));
When I do this, the updated values of X(3) or X(2) get carried down into X(2) and X(1). I would like to continue this out for much longer than would be feasible to cut and paste data, so I tried putting it into a loop like this:
for n=1:4
for m=(n-1):(-1):1
X(m)=Solution(m);
end
Solution(n)=solve(Equation(n),X(n));
end
It seems like the inner loop should be updating the X values in decending order as was done with the cutting and pasting of the solutions, but that is not happening. Is there a reason why using X(m)=Solution(m) is not equivalent to X(m)=Cut and paste of Solution(m). I'm new to using the symbolic toolbox, so I'm not all the familiar with it.
Thanks,
Jon

采纳的回答

Torsten
Torsten 2022-3-10
Say you want to solve
eqn1 = expr1 == 0;
...
eqnn = exprn == 0;
Then according to what you are trying to do you can proceed as follows:
sol1 = solve(expr1 == 0,x1);
expr2 = subs(expr2,x1,sol1);
sol2 = solve(expr2 == 0,x2);
expr3 = subs(expr3,[x1,x2],[sol1,sol2])
sol3 = solve(expr3 == 0,x3)
...
Is it this what you were asking for ?
  1 个评论
Jon LeBlanc
Jon LeBlanc 2022-3-11
Thanks! I just had to tweak it slightly, since subbing multiple variables at a time still left some instances of the substituted variable. I ended up using:
sol1 = solve(expr1 == 0,x1);
expr2 = subs(expr2,x1,sol1);
sol2 = solve(expr2 == 0,x2);
expr3 = subs(expr3,x1,sol1);
expr3 = subs(expr3,x2,sol2);
sol3 = solve(expr3 == 0,x3);
Thanks again for your help!

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-3-10
E = Equation;
partial.(X(1)) = solve(E(1), X(1));
for n = 2 : min(length(Equation), length(X));
E = subs(E(2:end), partial);
sol = solve(E(1), X(n));
if isempty(sol); break; end
partial.(X(n)) = sol;
end
Afterwards you are left with a struct of partial solutions, each one depending on fewer and fewer variables. Afterwards, you need to back-substitute.
The above code is not robust for the possibility that you might get multiple solutions for a variable. In such a case, you need to iterate over each of the values, creating a "branch" that assumes each of the values in turn -- and the branches might lead to other branches, not necessarily the same number of solutions in each branch.

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

标签

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by