2015-10-07 177 views
1

我有一些不連續的數據,我想分段擬合所有,因爲有一個函數的所有部分共同的擬合參數。 不連續點的數量,以及它們的位置有所不同。簡單的方法來分段擬合在Matlab中具有不同數量的奇點的不連續函數

我想過使用一個類,並將不連續性索引作爲數據成員,然後使用具有不同輸入數量的成員函數作爲我發送到Matlab的擬合函數的函數。

例如

function f = approximate(obj,varargin) 
     f = zeros(size(varargin{1})); 
     for i = 1:nargin-3 
      x = varargin{1}(obj.segmentStartIdx(i):obj.segmentEndIdx(i)); 
      f(obj.segmentStartIdx(i):obj.segmentEndIdx(i)) = varargin{2} + (0.25*(1-x/varargin{i+2}).^-2+x/varargin{i+2}-0.25); 
     end 
    end 

顯然,這不工作... 使用配合

fittype = ('fp.approximate(x,A,B,C)'); 

Matlab軟件拋出了以下錯誤:

Error using fittype/testCustomModelEvaluation (line 12) 
Expression fp.approximate(x,A,B,C) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated: 
Error in fittype expression ==> fp.approximate(x,A,B,C) 
??? Attempt to reference field of non-structure array. 
Error in fittype>iCreateFittype (line 371) 
    testCustomModelEvaluation(obj); 

Error in fittype (line 328) 
       obj = iCreateFittype(obj, varargin{:}); 

Error in fit>iFit (line 157) 
    model = fittype(fittypeobj, 'numindep', size(xdatain, 2)); 

Error in fit (line 108) 
[fitobj, goodness, output, convmsg] = iFit(xdatain, ydatain, fittypeobj, ... 

Caused by: 
    Error using fittype/evaluate (line 102) 
    Error in fittype expression ==> fp.approximate(x,A,B,C) 
    ??? Attempt to reference field of non-structure array. 

嘗試一個非成員函數沒修好問題,不知道我在做什麼錯,雖然... 我已經簡化了以下功能:

function [ f ] = moreArgTestFunc(p, xData) 
f = zeros(size(xData)); 
global segmentStartIdx; 
global segmentEndIdx; 
for i = 1:length(p)-1 
    x = xData(segmentStartIdx(i): segmentEndIdx(i)); 
    f(segmentStartIdx(i):segmentEndIdx(i)) = p(1) + (0.25*(1-x/p(i+1)).^-2+x/p(i+1)-0.25); 
end 
end 

試圖在下面的錯誤使用它在NonLinearModel.fit,或nlfit結果:

Error using moreArgTestFunc (line 2) 
Not enough input arguments. 

所以我可能會丟失的東西在這裏...

有沒有更好的辦法去做吧?

回答

0

當調用NonLinearModel.fit,fitnlm或lsqcurvefit函數時,必須將函數的指針作爲參數傳遞。我認爲傳入函數名是足夠的 - 但顯然不是。 使用匿名函數

funcToFit = @(p,x) myfunctionName(p,x) 

,然後通過funcToFit而不是myfunctionName作爲參數解決了這個問題。

通過使用p的不同大小的向量來解決擬合的參數的不同數量。

相關問題