solve nonlinear equations with unknown variables in matrix format
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jingming Yao
el 9 de Feb. de 2022
Comentada: Walter Roberson
el 10 de Feb. de 2022
Hello, Sir/Madam
I have a 3 equations system in the format below.
eq1 = (x+y)*cos(z)-a;
eq2 = (x+y)*cos(z+1)-b;
eq3 = (x+y)*cos(z+2)-c;
where a, b and c are known constant (matrix) with size 100x100. In return, I expect unknown x ,y and z are matrix in the same size.
What's the best or quickest way to solve x,y adn z?
I am using
syms x y z
[x, y, z] = solve(eq1, eq2, eq3);
but it says error, Second argument must be a vector of symbolic variables.
Youer help is highly appreciated!
best,
JM
0 comentarios
Respuesta aceptada
John D'Errico
el 10 de Feb. de 2022
As an alternative way to understand why there is no solution to your problem is to look at your equations.
eq1 = (x+y)*cos(z)-a;
eq2 = (x+y)*cos(z+1)-b;
eq3 = (x+y)*cos(z+2)-c;
Three equations, three unknowns. So there should be a solution, right? WRONG.
x and y appear in exactly the same way in all equations. So suppose a solution (x0, y0) exists, that is the optimal solution? Then it must be true that (x0+k,y0-k) is also a completely valid solution, for ANY value of k. If any solution exists, then there must be infinitely many identically good solutions.
That should be the clue you need to know there is no solution at all. You effectively have three equations, with only TWO effective unkowns, thus (x+y) and z.
Más respuestas (1)
Walter Roberson
el 10 de Feb. de 2022
Editada: Walter Roberson
el 10 de Feb. de 2022
syms x y z
syms A B C
eq1 = (x+y)*cos(z)-A;
eq2 = (x+y)*cos(z+1)-B;
eq3 = (x+y)*cos(z+2)-C;
sol = solve([eq1, eq2, eq3], [x, y, z])
%demo data
a = randi(9, 5, 5), b = randi(9, 5, 5), c = randi(9, 5, 5)
%end demo data
X = subs(sol.x, {A, B, C}, {a, b, c})
Y = subs(sol.y, {A, B, C}, {a, b, c})
Z = subs(sol.z, {A, B, C}, {a, b, c})
Notice the lack of solution. This is because your equations are not independent.
eqn = [eq1, eq2, eq3]
partialx = solve(eqn(1),x)
eqn2 = subs(eqn(2:end), x, partialx)
partialz = solve(eqn2(1), z)
eqn3 = subs(eqn2(2:end), z, partialz)
simplify(eqn3)
Now notice that the simplified eqn3 is independent of the third variable, y, so your system has an infinite number of solutions (y can be anything except perhaps a limited number of special values)
2 comentarios
Walter Roberson
el 10 de Feb. de 2022
You can get closed form solutions in a straight forward manner, at least with current versions
syms x y z
syms a b c
eq1 = (x+y)+(x-y).*cos(z)-a;
eq2 = (x+y)+(x-y).*cos(z+1)-b;
eq3 = (x+y)+(x-y).*cos(z+2)-c;
eqn = [eq1, eq2, eq3]
sol = solve(eqn, [x, y, z])
simplify([sol.x, sol.y, sol.z])
Or instead you can proceed step by step
partialx = solve(eqn(1),x)
eqn2 = subs(eqn(2:end), x, partialx)
partialy = simplify(solve(eqn2(1), y))
eqn3 = simplify(subs(eqn2(2:end), y, partialy))
partialz = solve(eqn3(1), z)
fullz = partialz
fully = subs(partialy, z, partialz)
fullx = subs(partialx, {z, y}, {fullz, fully})
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!










