[英]Matlab: Nonlinear equation solver
How do I solve these sets of equations and can matlab find a solution? 如何解决这些方程组,matlab可以找到解决方案吗? I'm solving for x1,x2,x3,x4,c1,c2,c3,c4.
我正在解决x1,x2,x3,x4,c1,c2,c3,c4。
syms c1 c2 c3 c4 x1 x2 x3 x4;
eqn1 = c1 + c2 + c3 + c4 == 2;
eqn2 = c1*x1 + c2*x2 + c3*x3 + c4*x4 == 0;
eqn3 = c1*x1^2 + c2*x2^2 + c3*x3^2 + c4*x4^2 == 2/3;
eqn4 = c1*x1^3 + c2*x2^3 + c3*x3^3 + c4*x4^3 == 0;
eqn5 = c1*x1^4 + c2*x2^4 + c3*x3^4 + c4*x4^4 == 2/5;
eqn6 = c1*x1^5 + c2*x2^5 + c3*x3^5 + c4*x4^5 == 0;
eqn7 = c1*x1^6 + c2*x2^6 + c3*x3^6 + c4*x4^6 == 2/7;
eqn8 = c1*x1^7 + c2*x2^7 + c3*x3^7 + c4*x4^7 == 0;
From what I understand, matlab has fsolve, solve, and linsolve, but I'm uncertain how to use them. 据我了解,matlab具有fsolve,resolve和linsolve,但我不确定如何使用它们。
You have a system of non-linear equations, so you can use fsolve to find a solution. 您有一个非线性方程组,因此可以使用fsolve找到一个解决方案。
First of all you need to create a function, say fcn
, of a variable x
, where x
is a vector with your initial point. 首先,您需要创建一个变量
x
的函数fcn
,其中x
是具有初始点的向量。 The function defines an output vector, depending on the current vector x
. 该函数根据当前向量
x
定义输出向量。
You have eight variables, so your vector x
will consist of eight elements. 您有八个变量,因此向量
x
将包含八个元素。 Let's rename your variables in this way: 让我们以这种方式重命名变量:
%x1 x(1) %c1 x(5)
%x2 x(2) %c2 x(6)
%x3 x(3) %c3 x(7)
%x4 x(4) %c4 x(8)
Your function will look like this: 您的函数将如下所示:
function F = fcn(x)
F=[x(5) + x(6) + x(7) + x(8) - 2 ;
x(5)*x(1) + x(6)*x(2) + x(7)*x(3) + x(8)*x(4) ;
x(5)*x(1)^2 + x(6)*x(2)^2 + x(7)*x(3)^2 + x(8)*x(4)^2 - 2/3 ;
x(5)*x(1)^3 + x(6)*x(2)^3 + x(7)*x(3)^3 + x(8)*x(4)^3 ;
x(5)*x(1)^4 + x(6)*x(2)^4 + x(7)*x(3)^4 + x(8)*x(4)^4 - 2/5 ;
x(5)*x(1)^5 + x(6)*x(2)^5 + x(7)*x(3)^5 + x(8)*x(4)^5 ;
x(5)*x(1)^6 + x(6)*x(2)^6 + x(7)*x(3)^6 + x(8)*x(4)^6 - 2/7 ;
x(5)*x(1)^7 + x(6)*x(2)^7 + x(7)*x(3)^7 + x(8)*x(4)^7
];
end
You can evaluate your function with some initial value of x
: 您可以使用一些
x
初始值来评估函数:
x0 = [1; 1; 1; 1; 1; 1; 1; 1];
F0 = fcn(x0);
Using x0
as initial point your function returns: 使用
x0
作为初始点,您的函数将返回:
F0 =
2.0000
4.0000
3.3333
4.0000
3.6000
4.0000
3.7143
4.0000
Now you can start fsolve
which will try to find some vector x
, such as your function returns all zeros: 现在您可以启动
fsolve
,它将尝试找到一些向量x
,例如您的函数返回全零:
[x,fval]=fsolve(@fcn, x0);
You will get something like this: 您将获得如下内容:
x =
0.7224
0.7224
-0.1100
-0.7589
0.3599
0.3599
0.6794
0.5768
fval =
-0.0240
0.0075
0.0493
0.0183
-0.0126
-0.0036
-0.0733
-0.0097
As you can see, the function values are really close to zeros, but you probably noticed that the optimization algorithm was stopped because of the limited count of the function evaluation steps stored in options.MaxFunEvals
(by default 800
). 如您所见,函数值实际上接近于零,但是您可能注意到优化算法已停止,因为在
options.MaxFunEvals
存储的函数评估步骤数量有限(默认为800
)。 Another possible reason is the limited number of iterations stored in MaxIter
(by default 400
). 另一个可能的原因是
MaxIter
存储的迭代次数有限(默认为400
)。
Redefine these value using the parameter options
: 使用参数
options
重新定义这些值:
options = optimset('MaxFunEvals',2000, 'MaxIter', 1000);
[x,fval]=fsolve(@fcn, x0, options);
Now your output is much better: 现在您的输出要好得多:
x =
0.7963
0.7963
-0.0049
-0.7987
0.2619
0.2619
0.9592
0.5165
fval =
-0.0005
-0.0000
-0.0050
0.0014
0.0208
-0.0001
-0.0181
-0.0007
Just play with different parameter values, in order to achieve a tolerable precision level for your problem. 只需使用不同的参数值,以达到您的问题可以忍受的精度水平。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.