Solving system of quadratic equations
30 次查看(过去 30 天)
显示 更早的评论
Hi guys!
I'm trying to solve a set of quadratic equations for a code I'm working on. I've tried to use vpasolve and solve but the code doesn't bring any solution. The equations are correct and I'm sure there are solutions to it as I can solve them with Mathematica but I'd like to be able to solve them in matlab so that I can write my code in there instead of Mathematica.
The code is something like this:
syms y1 y2 y3 y4 z1 z2
depd = [y1 y2 y3 y4 z1 z2];
% Assign the independent variables
x1 = sqrt(2/3);
x2 = sqrt(1/6);
x3 = sqrt(1/2);
x4 = sqrt(1/2);
z3 = sqrt(1/2);
z4 = sqrt(1/2);
prev = [-0.438450, -0.505030, -0.076748, -0.048791, 0.455646, 0.989215];
% write constraint equations
eq1 = (x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2 - 1/3 == 0;
eq2 = (x3 - x1)^2 + (y3 - y1)^2 + (z3 - z1)^2 - tan(pi/12) == 0;
eq3 = (x3 - x2)^2 + (y3 - y2)^2 + (z3 - z2)^2 - tan(pi/12) == 0;
eq4 = (y4 - x4)^2 + (z4 - y4)^2 + (x4 - z4)^2 - 1 == 0;
eq5 = (y4 - x1)^2 + (z4 - y1)^2 + (x4 - z1)^2 - 2 == 0;
eq6 = (z3 - x2)^2 + (x3 + y2)^2 + (y3 + z2)^2 - 0.84529946 == 0;
eqs = [eq1, eq2, eq3, eq4, eq5, eq6];
sol = vpasolve(eqs, depd, prev);
I don't need a precise solution but rather a numerical approximation. Is there something that can provide that in Matlab?
2 个评论
Matt J
2022-4-11
Are you sure the first equation shouldn't be,
eq1 = (x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2 - 1/3 == 0;
采纳的回答
Davide Masiello
2022-4-11
I am not really confident with Simulink, but it does work on Matlab using the fsolve function.
In the equation, I changed z1 and z2 to y(5) and y(6) respectively so to make possible the indexing of the dependent variable.
clear,clc
y0 = [-0.438450, -0.505030, -0.076748, -0.048791, 0.455646, 0.989215];
y = fsolve(@eqSystem,y0)
function out = eqSystem(y)
x1 = sqrt(2/3);
x2 = sqrt(1/6);
x3 = sqrt(1/2);
x4 = sqrt(1/2);
z3 = sqrt(1/2);
z4 = sqrt(1/2);
out = [ (x2 - x1)^2 + (y(2) - y(1))^2 + (y(6) - y(5)) - 1/3;...
(x3 - x1)^2 + (y(3) - y(1))^2 + (z3 - y(5))^2 - tan(pi/12);...
(x3 - x2)^2 + (y(3) - y(2))^2 + (z3 - y(6))^2 - tan(pi/12);...
(y(4) - x4)^2 + (z4 - y(4))^2 + (x4 - z4)^2 - 1;...
(y(4) - x1)^2 + (z4 - y(1))^2 + (x4 - y(5))^2 - 2;...
(z3 - x2)^2 + (x3 + y(2))^2 + (y(3) + y(6))^2 - 0.84529946;...
];
end
2 个评论
Alex Sha
2022-4-12
There are four solutions:
No. y1 y2 y3 y4 z1 z2
1 -0.408248290310322 -0.408248290883343 -2.65940075527358E-10 5.66489492618543E-16 0.408248289890841 0.816496580354705
2 -0.561883068548743 -0.453705322086167 -0.0890364837212022 1.4142135623731 0.527109082977269 0.9207640611459
3 -0.332314192176063 -0.0311689186229293 -0.387501968689092 5.89828382789581E-16 1.21003557681 0.934394159596848
4 -0.473380938970007 -0.109067418601998 -0.390919363116206 1.4142135623731 1.20628926550608 1.02205483456621
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!