2012-02-06 84 views
2

这是关系到how to find out the scaling factors to match two curves in matlab? 我用下面的代码找出缩放因子来匹配两条曲线找出缩放因子在Matlab配合fmincon两条曲线

function err = sqrError(coeffs, x1, y1, x2, y2) 
    y2sampledInx1 = interp1(coeffs(1)*x2,y2,x1); 
    err = sum((coeffs(2)*y2sampledInx1-y1).^2); 
end 

,我的后续问题使用fmincon来优化结果。

options = optimset('Algorithm','active-set','MaxFunEvals',10000,'TolCon',1e-7) 
A0(1)=1; A0(2)=1; LBA1=0.1; UBA1=5; LBA2=0.1; UBA2=5; 
LB=[LBA1 LBA2]; UB=[UBA1 UBA2]; 
coeffs = fmincon(@(c) sqrError(c,x1, y1, x2, y2),A0,[],[],[],[],LB,UB,[],options); 

当我和与该功能数据测试,

X1 = [ - 0.3 -0.24 -0.18 -0.12 -0.06 0.06 0.12 0.18 0.24 0.3 0.36 0.42 0.48 0.54 0.6 0.66 0.72 0.78 0.84 0.9 0.96 1.02 1.08 1.14 1.2 1.26 1.32 1.38 1.44 1.5 1.56 1.62 1.68 1.74 1.8 1.86 1.92 1.98 2.04] Y1 = [0.00 0.00 0.00 0.01 0.03 0.09 0.13 0.14 0.14 0.16 0.20 0.22 0.26 0.34 0.41 0.52 0.62 0.72 0.81 0.91 0.95 0.99 0.98 0.96 0.90 0.82 0.74 0.66 0.58 0.52 0.47 0.40 0.36 0.32 0.27 0.22 0.19 0.15 0.12 0.10];

X2 = [ - 0.3 -0.24 -0.18 -0.12 -0.06 0.06 0.12 0.18 0.24 0.3 0.36 0.42 0.48 0.54 0.6 0.66 0.72 0.78 0.84 0.9 0.96 1.02 1.08 1.14 1.2 1。26 1.32 1.38 1.44 1.5 1.56 1.62 1.68 1.74 1.8 1.86 1.92 1.98 2.04]; Y2 = [0.00 0.00 0.00 0.00 0.05 0.15 0.15 0.13 0.11 0.11 0.13 0.18 0.24 0.33 0.43 0.54 0.66 0.76 0.84 0.90 0.93 0.94 0.94 0.91 0.87 0.81 0.75 0.69 0.63 0.55 0.49 0.43 0.37 0.32 0.27 0.23 0.19 0.16 0.13 0.10 ]。

错误消息显示为如下:

???使用错误==> interp1在172的NaN不在2 y2sampledInx1 = interp1(coeffs(1)* X2,Y2,X1)为 X.

误差在==> sqrError一个适当的值;

错误==> @(c)中sqrError(C,X1,Y1,X2,Y2)

错误==> nlconst在805 F = feval(funfcn {3},X, varargin {:});

错误==> fmincon在758 [X,FVAL,LAMBDA,EXITFLAG,OUTPUT GRAD,粗麻布] = ...

错误==> coeffs = fmincon(@(三)sqrError(C,X1,Y1,X2,Y2 ),A0,[],[],[],[],LB,UB,[],选项);

有什么不对的代码,我应该如何避开它。 感谢您的帮助。

回答

2

你的缩放可能推动内插轴出的数据的x轴的范围。即

X1 <分钟(X2 * coeffs(1))或X1> MAX(X2 * coeffs(1)),用于通过拟合算法中的至少一种x1和coeffs的值(1)

您可以通过给外推值范围之外的数据解决这个问题。或者,你可以使用外插在这些数值猜测。因此,尝试这些

y2sampledInx1 = interp1(coeffs(1)*x2,y2,x1,'Linear', 'Extrap'); 
y2sampledInx1 = interp1(coeffs(1)*x2,y2,x1,'Linear', Inf); 
y2sampledInx1 = interp1(coeffs(1)*x2,y2,x1,'Linear', 1E18); %if Inf messes with the algorithm 
+0

@马克一个,感谢您的快速答复。我尝试了你提供的外插方法,它显示了下面的错误消息:“???错误使用==> interp1在344无效方法。”你有其他建议吗? – tytamu 2012-02-06 21:07:43

+0

@泰,马克的解决方案是正确的,对我来说工作得很好。您看到的问题是由于Marc最后一行('Lienear')中的简单拼写错误造成的。无论如何,你不需要指定插值的方法。在你的代码中,我用'y2sampledInx1 = interp1(coeffs(1)* x2,y2,x1,[],'extrap');替换了'y2sampledInx1 = interp1(coeffs(1)* x2,y2,x1)并没有问题。 – foglerit 2012-02-07 01:42:06

+0

@ jonnat和Marc,感谢您的帮助。它现在很好用! – tytamu 2012-02-07 02:50:45