【问题标题】:finding out the scaling factors to match two curves with fmincon in matlab在matlab中找出与fmincon匹配的两条曲线的比例因子
【发布时间】:2012-02-28 06:25:50
【问题描述】:

这是与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 时出错,这不是一个合适的值 十。

==> sqrError 在 2 y2sampledInx1 = interp1(coeffs(1)*x2,y2,x1);

==> @(c)sqrError(c,x1,y1,x2,y2) 中的错误

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

==> fmincon 中的错误在 758 [X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...

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

代码有什么问题,我应该如何解决它。 感谢您的帮助。

【问题讨论】:

    标签: matlab data-processing


    【解决方案1】:

    您的缩放可能会将插值轴推到数据 x 轴的范围之外。即

    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
    

    【讨论】:

    • @Marc,感谢您的快速回复。我尝试了您提供的外推方法,它显示以下错误消息:“??? Error using ==> interp1 at 344 Invalid method。”您还有其他建议吗?
    • @Tai,Marc 的解决方案是正确的,对我来说效果很好。您看到的问题是由于 Marc 的最后一行 (Lienear) 中有一个简单的错字。在任何情况下,您都不需要指定插值方法。在您的代码中,我用y2sampledInx1 = interp1(coeffs(1)*x2,y2,x1); 替换了y2sampledInx1 = interp1(coeffs(1)*x2,y2,x1,[],'extrap');,没有遇到任何问题。
    猜你喜欢
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多