Solving System of Equations with Symbolic Toolbox
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Jon LeBlanc
el 10 de Mzo. de 2022
Comentada: Jon LeBlanc
el 11 de Mzo. de 2022
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
0 comentarios
Respuesta aceptada
Torsten
el 10 de Mzo. de 2022
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 ?
Más respuestas (1)
Walter Roberson
el 10 de Mzo. de 2022
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.
0 comentarios
Ver también
Categorías
Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!