Matlab 数据图之间的水平和垂直移动因子

Matlab 数据图之间的水平和垂直移动因子,matlab,Matlab,我必须在Matlab中实现x方向的移位,以匹配两个数据图。 让 等等。。。X在下图所示的两条曲线之间移动 我需要移动绿色曲线以匹配蓝色曲线。因此,我查看并尝试实现类似的功能,如下所示。但是,我将乘法修改为加法 function err = sqrError(coeffs, x1, y1, x2, y2) % Interpolation of 'y2' with scaled 'x2' into the domain 'x1' y2sampledInx1 = interp1(coeffs(1)+

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

等等。。。X在下图所示的两条曲线之间移动

我需要移动绿色曲线以匹配蓝色曲线。因此,我查看并尝试实现类似的功能,如下所示。但是,我将乘法修改为加法

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.

感谢您对更正的意见。提前谢谢

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

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

其中
method
是插值方法,如线性(默认)、样条曲线等。

感谢您的输入。但这里是输出:未定义函数或变量“method”。sqrError(第9行)中的错误y2sampledInx1=interp1(系数(1)+x2,y2,x1,方法“extrp”);@(c)sqrError中的错误(c,x1,y1,x2,y2)fminunc中的错误(第254行)f=feval(funfcn{3},x,varargin{:});ruf1(第36行)系数中的误差=fminunc(@(c)sqrError(c,x1,y1,x2,y2),[+0.5;15]);原因:初始用户提供的目标函数评估失败。FMINUNC无法继续。请将方法替换为“样条线”或“线性”。此外,阅读该文件也无妨。本研究的目的是重复以下参考文献中给出的程序:图4中的“感谢您的输入”。我能够使用以下命令运行代码:y2sampledInx1=@(c)interp1(c(1)+x2,y2,x1,'cubic',extrap');误差=@(c)和((c(2)+y2sampledInx1(c)-y1)。^2);opts=optimoptions('fminunc','TolFun','1e-16','TolX','1e-16','Algorithm','quasi-newton');x0=[1.5;1.5];系数=fminunc(err、x0、opts);然而,解决方案强烈依赖于初始猜测x0。如果我给出接近的答案,它就能给出好的解决方案。因此,probelm来到这里是为了从一个大数据集中自动化这个转换过程。欢迎你的意见。谢谢
y2sampledInx1 = interp1(coeffs(1)+x2,y2,x1,method,'extrap');