2016-02-15 77 views
0

这一问题与下面的帖子:Matlab的:非线性方程优化

Matlab: Nonlinear equation solver

随着8变量X0-X8,我得到了很大的成绩。但是,当我增加到解决10个变量时,结果不太好。即使我的“猜测”接近实际值并将最大迭代次数改为100000,结果仍然很差。还有什么我可以做的吗?

下面是代码:

function F = fcn(x) 
F=[x(6) +   x(7)   + x(8)   + x(9)  + x(10)-2 ; 
     x(6)*x(1) + x(7)*x(2) + x(8)*x(3) + x(9)*x(4) + x(10)*x(5)   ; 
     x(6)*x(1)^2 + x(7)*x(2)^2 + x(8)*x(3)^2 + x(9)*x(4)^2 + x(10)*x(5)-2/3 ; 
     x(6)*x(1)^3 + x(7)*x(2)^3 + x(8)*x(3)^3 + x(9)*x(4)^3 + x(10)*x(5)   ; 
     x(6)*x(1)^4 + x(7)*x(2)^4 + x(8)*x(3)^4 + x(9)*x(4)^4 + x(10)*x(5)-2/5 ; 
     x(6)*x(1)^5 + x(7)*x(2)^5 + x(8)*x(3)^5 + x(9)*x(4)^5 + x(10)*x(5)   ; 
     x(6)*x(1)^6 + x(7)*x(2)^6 + x(8)*x(3)^6 + x(9)*x(4)^6 + x(10)*x(5)-2/7 ; 
     x(6)*x(1)^7 + x(7)*x(2)^7 + x(8)*x(3)^7 + x(9)*x(4)^7 + x(10)*x(5)   ; 
     x(6)*x(1)^8 + x(7)*x(2)^8 + x(8)*x(3)^8 + x(9)*x(4)^8 + x(10)*x(5)-2/9 ; 
     x(6)*x(1)^9 + x(7)*x(2)^9 + x(8)*x(3)^9 + x(9)*x(4)^9 + x(10)*x(5)   

     ]; 

end 

clc 
clear all; 
format long 

x0 = [0.90; 0.53; 0; -0.53; -0.90; 0.23; 0.47; 0.56; 0.47; 0.23]; %Guess 

F0 = fcn(x0); 

[x,fval]=fsolve(@fcn, x0) %solve without optimization 

options = optimset('MaxFunEvals',100000, 'MaxIter', 100000); %optimization criteria 

[x,fval]=fsolve(@fcn, x0, options) %solve with optimization 

这里是我想要得到的实际值:

x1 = 0.906179 
x2 = 0.538469 
x3 = 0.000000 
x4 = -0.53846 
x5 = -0.906179 
x6 = 0.236926 
x7 = 0.478628 
x8 = 0.568888 
x9 = 0.478628 
x10 = 0.236926 
+0

但'fcn(your_expected_answer)'不是0 –

回答

1

fsolve这种优化功能的结果取决于初始点非常多。像你这样的非线性函数可以有很多局部最小值,你的选择是随机选择初始点,并希望它能使优化比以前更好。

你可以这样做:

clear; 

options = optimset('MaxFunEvals',2000, 'MaxIter', 1000, 'Display', 'off'); 

n = 200; %how many times calculate f with different initial points 
z_min = 10000; %the current minimum Euclidian distance between fval and zeros 

for i=1:n 
    x0 = rand(10, 1); 

    [x,fval]=fsolve(@fcn, x0, options); 

    z = norm(fval); 

    if (z < z_min) 
     z_min = z; 
     x_best = x; 
     f_best = fval; 

     display(['i = ', num2str(i), '; z_min = ', num2str(z_min)]); 
     display(['x = ', num2str(x_best')]); 
     display(['f = ', num2str(f_best')]); 

     fprintf('\n') 
    end 
end 

变化看z值优化的最大循环数和。它显示了你的函数与零向量的接近程度。

到目前为止,我已经得到了最好的解决方案:

x_best = 

    0.9062 
    -0.9062 
    -0.5385 
    0.5385 
    0.0000 
    0.2369 
    0.2369 
    0.4786 
    0.4786 
    0.5689 

f_best = 

    1.0e-08 * %these are very small numbers :) 

     0 
    0.9722 
    0.9170 
    0.8740 
    0.8416 
    0.8183 
    0.8025 
    0.7929 
    0.7883 
    0.7878 

对于这个解决方案z_min2.5382e-08

+0

再次感谢您!你的救星! – Sagistic