2016-12-07 58 views
0

我試圖解決使用MATLAB fmincon功能的問題。我有一個下面顯示的公式,爲此我使用一些時間點生成了測試數據。我想根據生成的測試數據通過使用優化來估計參數x(1),x(2)和x(3)。目前使用fmincon估算的參數並不接近用於生成數據的初始參數。任何幫助將不勝感激。fmincon - 結果不接近原始參數

測試數據; 時間點= [10:10:3​​00,500,700,1000]; x = [0.1,0.5,0.3];感興趣的%參數 Data = x(1)* sin(x(2)。* Timepoints)+ log(x(3)。* Timepoints); %使用測試方程式生成數據

% Parameters used to run the fmincon 
x0 = [0, 0.1, 0.1]; % initial guess 
lb = zeros(1, length(x0)); % lower bound of parameters 
ub = ones(1, length(x0)); % upper bound of parameters 

[x, fval, exitflag, output] = fmincon(@modelA1, x0, [], [], [], [], lb, ub, [], options, Timepoints, Data); 


function fvalues = modelA(x, Timepoints, fvals) 
Fvalues = zeros(1, length(Timepoints)); 
PreFvalues = zeros(1, length(Timepoints)); 

for Temp = 1:length(Timepoints) 
tempY = x0(1)*sin(x0(2).*Timepoints(Temp))+log(x0(3).*Timepoints(Temp)); 
PreFvalues(Temp) = (fvals(Temp)-tempY)^2; 
end 
fvalues=sqrt(sum(PreFvalues)); 

回答

0

當提供代碼示例時,至少要確保它的工作原理,幷包含定義測試數據的代碼。

適當修改,使其工作後,下面的代碼表明,你的代碼生成的解決方案是相當不錯的。您具有非常非線性的函數,因此解決方案是局部最小值而不是全局最小值(即原始解決方案)就不足爲奇了。取決於初始條件(即x0),您會得到稍微不同的結果。

對於這個問題我也建議使用lsqcurvefit而非fmincon。代碼中也有一個例子。

function test 

Timepoints = [10:10:300, 500, 700, 1000]; x = [0.1, 0.5, 0.3]; 
Data = x(1)*sin(x(2).*Timepoints)+log(x(3).*Timepoints); 

options = optimoptions('fmincon'); 

% Parameters used to run the fmincon 
x0 = [0, 0.1, 0.1]; % initial guess 
lb = zeros(1, length(x0)); % lower bound of parameters 
ub = ones(1, length(x0)); % upper bound of parameters 

[x, fval, exitflag, output] = fmincon(@modelA, x0, [], [], [], [], lb, ub, [], options, Timepoints, Data); 

% Using lsqcurvefit 
F = @(x,xData)x(1)*sin(x(2).*xData)+log(x(3).*xData); 
x = lsqcurvefit(F,x0,Timepoints,Data,lb,ub); 

plot(... 
    Timepoints,Data,... 
    Timepoints,x(1)*sin(x(2).*Timepoints)+log(x(3).*Timepoints),... 
    Timepoints,F(x,Timepoints)); 
legend({'Original','fmincon','lsqcurvefit'}); 

function fvalues = modelA(x, Timepoints, fvals) 

tempY = x(1)*sin(x(2)*Timepoints)+log(x(3)*Timepoints); 
PreFvalues = (fvals-tempY).^2; 

fvalues=sum(PreFvalues); 
0

當提供代碼示例時,至少要確保它的工作原理,幷包含定義測試數據的代碼。

適當修改,使其工作後,下面的代碼表明,你的代碼生成的解決方案是相當不錯的。您具有非常非線性的函數,因此解決方案是局部最小值而不是全局最小值(即原始解決方案)就不足爲奇了。取決於初始條件(即x0),您會得到稍微不同的結果。

對於這個問題我也建議使用lsqcurvefit而非fmincon。代碼中也有一個例子。

function test 

Timepoints = [10:10:300, 500, 700, 1000]; x = [0.1, 0.5, 0.3]; 
Data = x(1)*sin(x(2).*Timepoints)+log(x(3).*Timepoints); 

options = optimoptions('fmincon'); 

% Parameters used to run the fmincon 
x0 = [0, 0.1, 0.1]; % initial guess 
lb = zeros(1, length(x0)); % lower bound of parameters 
ub = ones(1, length(x0)); % upper bound of parameters 

[x, fval, exitflag, output] = fmincon(@modelA, x0, [], [], [], [], lb, ub, [], options, Timepoints, Data); 

% Using lsqcurvefit 
F = @(x,xData)x(1)*sin(x(2)*xData)+log(x(3)*xData); 
x = lsqcurvefit(F,x0,Timepoints,Data,lb,ub); 

plot(... 
    Timepoints,Data,... 
    Timepoints,x(1)*sin(x(2).*Timepoints)+log(x(3).*Timepoints),... 
    Timepoints,F(x,Timepoints)); 
legend({'Original','fmincon','lsqcurvefit'}); 

function fvalues = modelA(x, Timepoints, fvals) 

tempY = x(1)*sin(x(2)*Timepoints)+log(x(3)*Timepoints); 
PreFvalues = (fvals-tempY).^2; 

fvalues=sum(PreFvalues); 
+0

感謝您尋找到這一點。當我運行腳本時,對於fmincon我得到了x = [0.0131,0.9772,0.2976],對於lsqcurvefit我得到了x = [0.0000,0.0000,0.2973]。我想知道這是不是你所得到的。 – TTZ