我有3个方程和4个未知数要解决。当然有解决方案,但是vpasolve不返回任何解决方案,如果我下降到3 eq和3未知数,它会很好。我知道随着未知数的增加,我有无数种解决方案,那么在那种情况下我该如何解决呢?
这是我的代码:
syms x y z theta1 theta2 theta3 phi1
xEquation = x == cos(theta1)*cos(phi1) + cos(theta1 + theta2)*cos(phi1) + cos(theta1 + theta2 + theta3)*cos(phi1)
yEquation = y == cos(theta1)*sin(phi1) + cos(theta1 + theta2)*sin(phi1) + cos(theta1 + theta2 + theta3)*sin(phi1)
zEquation = z == sin(theta1) + sin(theta1 + theta2) + sin(theta1 + theta2 + theta3)
x = 2;
y = 1.5;
z = 0;
sol = vpasolve([eval(xEquation), eval(yEquation), eval(zEquation)], [theta1, theta2, theta3, phi1], [-pi pi; -pi pi; -pi pi; -pi pi;]);
这会产生具有4个字段的sol struct,但是它们是空的,没有解。
答案 0 :(得分:1)
求解具有n个未知数的m个方程,例如m<n
,这意味着某些变量将是取决于其他变量的参数。
例如
x-3y+z = 2
x-y+5z = 5
假设z
为参数
在Matlab中解决此问题的代码是
syms x y z
eq1 = x-3*y+z ==2;
eq2 = x-y+5*z ==5;
sol = solve(eq1, eq2, x, y);
sol.x
sol.y
您会看到z
省略了solve表达式,这意味着它将被视为参数
解决方案是
sol.x = 13/2 - 7*z
sol.y = 3/2 - 2*z
x, y and z
不是数字
值,所以这就是您不能使用vpasolve
代表Variable-precision arithmetic
。 vpasolve给出一个
数值解的精度 x, y and z
不是数字值,因此您无法预先定义
除非您先修复z
您可以使用solve
来查看解决方案集,这里我将phi1
视为参数,因此在solve expression中将其省略
syms x y z theta1 theta2 theta3 phi1
xEquation = 2 == cos(theta1)*cos(phi1) + cos(theta1 + theta2)*cos(phi1) + cos(theta1 + theta2 + theta3)*cos(phi1);
yEquation = 1.5 == cos(theta1)*sin(phi1) + cos(theta1 + theta2)*sin(phi1) + cos(theta1 + theta2 + theta3)*sin(phi1);
zEquation = 0 == sin(theta1) + sin(theta1 + theta2) + sin(theta1 + theta2 + theta3);
sol = solve(xEquation, yEquation, zEquation, theta1, theta2, theta3);
sol.theta1 = [-pi*(2*n - 1); pi*(2*m + 1); pi*k; pi*k]
sol.theta2 = [pi*(2*m + 1); -pi*(2*n - 1); -pi*(2*n - 1); z]
sol.theta3 = [pi*k; pi*k; pi*(2*m + 1); pi*(2*m + 1)]
phi1 is the parameter
X = [sol.theta1(1); sol.theta2(1); sol.theta3(1); phi1]
X = [-pi*(2*n - 1); pi*(2*m + 1); pi*k; phi1]
根据上述推论,共写出了4套
z
是参数,k, m, n
是整数,主要是
用于三角函数的周期性
如果您在z
范围内设置[-pi, pi]
,则可以将k, m and n
调整为
获取[-pi, pi]
范围内的有效解决方案。
fmincon
ceq = 0
[-pi pi]
转换为lb = -pi
并
ub = pi
0
t = 0:0.1:1;
x = 1.5 + 0.5 .* cos(8 .* pi .* t);
y = 1.5 + 0.5 .* sin(8 .* pi .* t);
z = 1 .* t .* ones(size(x));
lb = -pi*ones(1, 4);
ub = -lb;
p0 = zeros(1,4);
sol = cell(1,length(t));
for i = 1:length(t)
sol{i} = fmincon(@(p)0,p0,[],[],[],[],lb,ub,@(p)nonlincon(x(i),y(i), z(i), p(1), p(2), p(3), p(4)));
end
function [c, ceq] = nonlincon(x,y, z, theta1, theta2, theta3, phi1)
c = [];
ceq(1) = cos(theta1)*cos(phi1) + cos(theta1 + theta2)*cos(phi1) + cos(theta1 + theta2 + theta3)*cos(phi1)-x;
ceq(2) = cos(theta1)*sin(phi1) + cos(theta1 + theta2)*sin(phi1) + cos(theta1 + theta2 + theta3)*sin(phi1)-y;
ceq(3) = sin(theta1) + sin(theta1 + theta2) + sin(theta1 + theta2 + theta3)-z;
end
t = 0.1
为sol{2}
时的第二套解决方案
sol{2}.(1) = pheta1
sol{2}.(2) = pheta2
sol{2}.(3) = pheta3
sol{2}.(4) = phi1
您可以遵循相同的逻辑在不同的时间t找到解决方案
整个解决方案