solve nonlinear equations with unknown variables in matrix format
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 10 Fév 2022
Modifié(e) : Walter Roberson
le 10 Fév 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 commentaires
Jingming Yao
le 10 Fév 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})
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!










