如何让MATLAB在数值上解决一个特别讨厌的方程组?

时间:2015-07-08 18:33:37

标签: matlab numerical-methods

我有四个参数,t1,t2,theta1和theta2。我想找到一个解决方案(可能有无限多个)到下面的方程组:

t1*cos(theta1) + t2*cos(theta2) + 2*t1*t2*sin(theta1 + theta2) = t1*sin(theta1) + t2*sin(theta2) + 2*t1*t2*cos(theta1 + theta2) + 1

t1*cos(theta1) + t2*cos(theta2) + t1*sin(theta1) + t2*sin(theta2) = 2*t1*t2*sin(theta1 + theta2) + 2*t1*t2*cos(theta1 + theta2) + 1

2*t1^2*t2^2 + sin(theta1)*t1*t2^2 + sin(theta1 + theta2)*t1*t2 + 1 = sin(theta2)*t1^2*t2 + t1^2 + sin(theta1)*t1 + t2^2

由于参数多于方程,我们必须施加进一步的限制来确定该系统的特定解决方案。现在我并不关心选择哪种解决方案,只要它不是微不足道的(就像所有变量等于零)。

我目前的方法是将第三个等式设置为0.5,用theta1和theta2求解t1和t2,并将这些表达式代入前两个等式以求解theta1和theta2。但是,MATLAB试图象征性地进行此操作,这非常耗时。有没有办法可以为这个系统找到一些近似的解决方案?我不能精确地绘制这些方程表示的表面并查看它们的交点,因为方程的每一边都涉及两个以上的参数,这意味着它不能在三维中可视化。

1 个答案:

答案 0 :(得分:1)

您可以使用Attempt 0: [1, 3, 2, 4, 5, 6] Attempt 1: [2, 3, 1, 4, 5, 6] Attempt 2: [2, 1, 3, 4, 5, 6] Attempt 3: [3, 1, 2, 4, 5, 6] Attempt 4: [3, 2, 1, 4, 5, 6] All attempts [[1, 3, 2, 4, 5, 6], [2, 3, 1, 4, 5, 6], [2, 1, 3, 4, 5, 6], [3, 1, 2, 4, 5, 6], [3, 2, 1, 4, 5, 6]] 执行此操作。

首先,您需要为三个方程的残差创建匿名函数。我将创建一个向量fsolve,其中x。这就产生了残差:

x = [t1; t2; theta1; theta2]

然后,我们需要创建一个单个函数,它是这三个残差的向量,因为r1 = @(x) x(1)*cos(x(3)) + x(2)*cos(x(4)) + 2*x(1)*x(2)*sin(x(3) + x(4)) - (x(1)*sin(x(3)) + x(2)*sin(x(4)) + 2*x(1)*x(2)*cos(x(3) + x(4)) + 1); r2 = @(x) x(1)*cos(x(3)) + x(2)*cos(x(4)) + x(1)*sin(x(3)) + x(2)*sin(x(4)) - (2*x(1)*x(2)*sin(x(3) + x(4)) + 2*x(1)*x(2)*cos(x(3) + x(4)) + 1); r3 = @(x) 2*x(1)^2*x(2)^2 + sin(x(3))*x(1)*x(2)^2 + sin(x(3) + x(4))*x(1)*x(2) + 1 - (sin(x(4))*x(1)^2*x(2) + x(1)^2 + sin(x(3))*x(1) + x(2)^2); 试图让这个向量为零:

fsolve

现在,我们致电r = @(x) [r1(x); r2(x); r3(x)] 。我选择了一个任意的起点:

fsolve

您可以忽略该警告。 >> [x, fval, exitflag] = fsolve(r, [0.5,0.5,pi/4,pi/4]) Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead. > In fsolve at 287 Equation solved. fsolve completed because the vector of function values is near zero as measured by the default value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> x = 0.9654 0.5182 0.7363 0.7344 fval = 1.0e-10 * -0.0090 0.2743 -0.0181 exitflag = 1 是您要查找的四个值。 x是残差的值。 fval表示我们找到了根。