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

问题描述:

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

I have to implement shifting in x directions in Matlab to match two data plots. Let

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在两条曲线之间,如下图所示. 见图1 .

and so on... Xshift between the two curves shown in below figure. see figure 1.

我需要移动绿色曲线以匹配蓝色曲线.因此,我查看了这文章,并尝试如下进行类似的实现.但是,我将乘法修改为加法.

I need to shift the green curve to match with the blue curve. Hence, I looked at This article and tried to implement similarly as follows. However, I modified the multiplications as additions.

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)

但是,我遇到的错误如下:

However, I am facing the error as follows:

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.

感谢您的输入以进行更正.在此先感谢.

Appreciate your inputs to correction. Thanks in advance..

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

Well, as the error says, your objective (i.e. sqrError) function is undefined at initial point (i.e. coeffs = [1;1]).
This is because x1 (your interpolated grid) has values outside of the range of coeffs(1)+x2. So basically you are trying to extrapolate rather than interpolate. In this case interp1 returns NaNs in the points outside coeffs(1)+x2.
If you would like it to perform also extrapolation, you should use the argument extrap:

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

其中method是插值方法,例如线性(默认),样条线等.

where method is the interpolation method such as linear (default), spline, etc.