solve nonlinear equations with unknown variables in matrix format

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

John D'Errico
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.
  1 comentario
Jingming Yao
Jingming Yao el 10 de Feb. de 2022
Thank you! Walter and John. I neglected something in the origianl equations. My real equations are
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;
sol = solve([eq1, eq2, eq3], [x, y, z]);
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});
Here a, b, c are 100x100 matrix with real integer constant.
In this modified case, (x0+k,y0-k) won't be a solution but still "Unable to find explicit solution". X, Y, and Z are 0x100 as there is no solution found. Any thought? Thanks!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
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])
sol = struct with fields:
x: [0×1 sym] y: [0×1 sym] z: [0×1 sym]
%demo data
a = randi(9, 5, 5), b = randi(9, 5, 5), c = randi(9, 5, 5)
a = 5×5
1 2 4 8 3 3 9 7 1 8 8 1 9 4 9 4 8 3 5 5 6 3 8 6 6
b = 5×5
1 9 3 2 4 2 1 3 4 5 1 3 8 4 6 4 3 3 7 3 9 7 5 1 1
c = 5×5
5 2 3 5 6 8 4 4 4 6 5 4 1 2 7 5 8 1 4 7 8 3 8 5 7
%end demo data
X = subs(sol.x, {A, B, C}, {a, b, c})
X = Empty sym: 0-by-5
Y = subs(sol.y, {A, B, C}, {a, b, c})
Y = Empty sym: 0-by-5
Z = subs(sol.z, {A, B, C}, {a, b, c})
Z = Empty sym: 0-by-5
Notice the lack of solution. This is because your equations are not independent.
eqn = [eq1, eq2, eq3]
eqn = 
partialx = solve(eqn(1),x)
partialx = 
eqn2 = subs(eqn(2:end), x, partialx)
eqn2 = 
partialz = solve(eqn2(1), z)
partialz = 
eqn3 = subs(eqn2(2:end), z, partialz)
eqn3 = 
simplify(eqn3)
ans = 
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
Jingming Yao
Jingming Yao el 10 de Feb. de 2022
Thank you! Walter and John. I neglected something in the origianl equations. My real equations are
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;
sol = solve([eq1, eq2, eq3], [x, y, z]);
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});
Here a, b, c are 100x100 matrix with real integer constant.
In this modified case, (x0+k,y0-k) won't be a solution but still "Unable to find explicit solution". X, Y, and Z are 0x100 as there is no solution found. Any thought? Thanks!
Walter Roberson
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]
eqn = 
sol = solve(eqn, [x, y, z])
sol = struct with fields:
x: [2×1 sym] y: [2×1 sym] z: [2×1 sym]
simplify([sol.x, sol.y, sol.z])
ans = 
Or instead you can proceed step by step
partialx = solve(eqn(1),x)
partialx = 
eqn2 = subs(eqn(2:end), x, partialx)
eqn2 = 
partialy = simplify(solve(eqn2(1), y))
partialy = 
eqn3 = simplify(subs(eqn2(2:end), y, partialy))
eqn3 = 
partialz = solve(eqn3(1), z)
Warning: Solutions are only valid under certain conditions. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
partialz = 
fullz = partialz
fullz = 
fully = subs(partialy, z, partialz)
fully = 
fullx = subs(partialx, {z, y}, {fullz, fully})
fullx = 

Iniciar sesión para comentar.

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!

Translated by