在matlab中找出与fmincon匹配两条曲线的缩放因子

时间:2012-02-06 19:18:12

标签: matlab data-processing

这是与how to find out the scaling factors to match two curves in matlab?相关的跟进问题 我使用以下代码来计算匹配两条曲线的缩放因子

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   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   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];

错误消息显示如下:

  

???使用==>时出错在172 NaN处的interp1不是适当的值   X

     

==>中的错误sqrError at 2 y2sampledInx1 =   interp1(coeffs(1)* X2,Y2,X1);

     

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

     

==>中的错误nlconst at 805                   f =                   feval(funfcn {3}中,x,{varargin:});

     

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

错误==> coeffs = fmincon(@(c)sqrError(c,x1,y1,x2, Y2),A0,[],[],[],[],LB,UB,[],选项);

代码有什么问题,我应该如何处理它。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

您的缩放可能会使插补轴超出数据x轴的范围。即

  

x1< min(x2 * coeffs(1))或x1>至少一个x1的max(x2 * coeffs(1))和拟合算法选择的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