首页 文章

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

提问于
浏览
2

这是与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在758 [X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN] = ...

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

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

1 回答

  • 2

    您的缩放可能会将插值轴推出数据的x轴范围 . 即

    x1 <min(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
    

相关问题