2014-09-26 90 views
0

我必須在Matlab中實現x方向上的偏移以匹配兩個數據圖。 讓數據圖之間的水平和垂直位移因子Matlab

data1: 

x1=[-0.3:0.06:2.1]'; 

y1=[ 0.001 0.001 0.004 0.014 0.052 0.166 0.330 0.416 0.340 0.247 0.194 0.197 0.237 0.330 0.428 0.542 0.669 0.767 0.855 0.900 0.913 0.904 0.873 0.811 0.765 0.694 0.631 0.585 0.514 0.449 0.398 0.351 0.309 0.273 0.233 0.211 0.182 0.154 0.137 0.117 0.101 ]'; 

data2: 

x2=[-0.3:0.06:2.1]'; 

y2=[0.000 0.000 0.000 0.000 0.025 0.230 0.447 0.425 0.269 0.194 0.225 0.326 0.477 0.636 0.791 0.931 1.036 1.104 1.117 1.123 1.062 0.980 0.897 0.780 0.675 0.571 0.471 0.390 0.309 0.258 0.209 0.161 0.129 0.099 0.079 0.063 0.047 0.038 0.027 0.023 0.015 ]'; 

等等... XSHIFT如下圖所示的兩條曲線之間。 see figure 1

我需要移動綠色曲線以匹配藍色曲線。因此,我看了This article並試圖按如下方式執行。但是,我將乘法修改爲加法。

function err = sqrError(coeffs, x1, y1, x2, y2) 
% Interpolation of 'y2' with scaled 'x2' into the domain 'x1' 
y2sampledInx1 = interp1(coeffs(1)+x2,y2,x1); 
% Squred error calculation 
err = sum((coeffs(2)+y2sampledInx1-y1).^2); 
end 


coeffs = fminunc(@(c) sqrError(c,x1, y1, x2, y2),[1;1]); 
A = coeffs(1); 
B = coeffs(2); 
plot(x1, y1, A*x2, B*y2) 

不過,我對着錯誤如下:

Warning: Gradient must be provided for trust-region algorithm; 
using line-search algorithm instead. 
> In fminunc at 383 

Error using fminusub 
Objective function is undefined at initial point. Fminunc cannot continue. 

感謝您的輸入校正。在此先感謝..

回答

1

那麼,如錯誤所述,您的目標(即sqrError)功能在初始點(即coeffs = [1;1])未定義。
這是因爲x1(插值網格)的值超出了coeffs(1)+x2的範圍。所以基本上你試圖推斷而不是內插。在這種情況下,interp1coeffs(1)+x2之外的點返回NaN s。
如果你想它也進行外推,你應該使用參數extrap

y2sampledInx1 = interp1(coeffs(1)+x2,y2,x1,method,'extrap'); 

其中method是插值方法,例如線性(默認),花鍵等

+0

感謝您的投入。 – learner123 2014-09-26 11:09:44

+0

但這裏是輸出: 未定義的函數或變量'方法'。 sqrError錯誤(第9行) y2sampledInx1 = interp1(coeffs(1)+ x2,y2,x1,method,'extrap'); 錯誤@(c)中sqrError(C,X1,Y1,X2,Y2) 錯誤fminunc(線254) F = feval(funfcn {3}中,x,{varargin:}); ruf1錯誤(第36行) coeffs = fminunc(@(c)sqrError(c,x1,y1,x2,y2),[+ 0.5; 15]); 引起: 初次用戶提供的目標函數評估失敗。 FMINUNC無法繼續。 – learner123 2014-09-29 01:50:00

+0

用'spline'或'linear'代替方法。另外,閱讀文檔並不會造成損害 – ThP 2014-09-29 04:29:33