【问题标题】:Horizontal and vertical shift factors between data plots Matlab数据图之间的水平和垂直偏移因子 Matlab
【发布时间】:2014-09-26 06:27:15
【问题描述】:

我必须在 Matlab 中实现 x 方向的移动以匹配两个数据图。 让

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.

感谢您对纠正的投入。提前谢谢..

【问题讨论】:

    标签: matlab


    【解决方案1】:

    好吧,正如错误所说,您的目标(即sqrError)函数在初始点(即coeffs = [1;1])是未定义的。
    这是因为x1(您的插值网格)的值超出了coeffs(1)+x2 的范围。所以基本上你是在尝试推断而不是插值。在这种情况下,interp1coeffs(1)+x2 之外的点中返回NaNs。
    如果您希望它也执行外推,则应使用参数extrap

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

    其中method为线性(默认)、样条等插值方法

    【讨论】:

    • 感谢您的意见。
    • 但这里是输出:未定义的函数或变量“方法”。 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 无法继续。
    • 用“样条”或“线性”替换方法。另外,阅读文档也没有什么坏处
    • 这项研究的目的是重复以下参考文献中给出的过程:eng.uc.edu/~beaucag/Classes/Characterization/DMA%20Lab/… 中的图 4 感谢您的意见。
    • 我可以使用以下命令运行代码:y2sampledInx1 = @(c) interp1(c(1)+x2,y2,x1,'cubic', 'extrap');错误 = @(c) sum((c(2)+y2sampledInx1(c)-y1).^2); opts = optimoptions('fminunc', 'TolFun',1e-16, 'TolX',1e-16,'算法','准牛顿'); x0=[1.5; 1.5];系数 = fminunc(err,x0,opts);但是,解决方案很大程度上取决于初始猜测 x0。如果我给出 close x0 它能够给出很好的解决方案。因此,problelm 来这里是为了从大型数据集中自动执行此转换过程。欢迎您的意见。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    相关资源
    最近更新 更多