vpasolve 未知数多于方程 => 无解
vpasolve More Unknowns than Equations => No Solutions
我有 3 个方程式和 4 个未知数需要求解。当然有解决方案,但 vpasolve 没有 return 任何,如果我下降到 3 eq 和 3 unknowns,它工作得很好。我知道随着更多的未知数,我有几乎无限多的解决方案,那么在这种情况下我该如何让它发挥作用?
这是我的代码:
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 结构,但它们是空的,没有解决方案。
求解具有 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
在求解表达式中被省略,这意味着它将被视为参数
解决方案是
sol.x = 13/2 - 7*z
sol.y = 3/2 - 2*z
- 从解中可以看出
x, y and z
不是数值
值,所以这就是你不能使用 vpasolve
which 的原因
代表 Variable-precision arithmetic
。 vpasolve 给出了一个
数值解的精度
- 此外,由于
x, y and z
不是数值,您无法预定义
他们的范围,除非你先修复 z
你可以用solve
来查看解集,这里我考虑phi1
作为参数,所以在求解表达式
中省略了它
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
是整数,主要是
用于三角函数周期性
如果您在 [-pi, pi]
范围内设置 z
,您可以将 k, m and n
调整为
得到 [-pi, pi]
.
范围内的有效解
如您所见,这很耗时
使用fmincon
- 或者您可以使用 fmincon 来解决您的问题
- 我主要是最小化一个常数函数,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找到解决方案
The Entire solution
我有 3 个方程式和 4 个未知数需要求解。当然有解决方案,但 vpasolve 没有 return 任何,如果我下降到 3 eq 和 3 unknowns,它工作得很好。我知道随着更多的未知数,我有几乎无限多的解决方案,那么在这种情况下我该如何让它发挥作用?
这是我的代码:
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 结构,但它们是空的,没有解决方案。
求解具有 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
在求解表达式中被省略,这意味着它将被视为参数
解决方案是
sol.x = 13/2 - 7*z
sol.y = 3/2 - 2*z
- 从解中可以看出
x, y and z
不是数值 值,所以这就是你不能使用vpasolve
which 的原因 代表Variable-precision arithmetic
。 vpasolve 给出了一个 数值解的精度 - 此外,由于
x, y and z
不是数值,您无法预定义 他们的范围,除非你先修复z
你可以用solve
来查看解集,这里我考虑phi1
作为参数,所以在求解表达式
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
是整数,主要是 用于三角函数周期性如果您在
范围内的有效解[-pi, pi]
范围内设置z
,您可以将k, m and n
调整为 得到[-pi, pi]
.
如您所见,这很耗时
使用fmincon
- 或者您可以使用 fmincon 来解决您的问题
- 我主要是最小化一个常数函数,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找到解决方案
The Entire solution