【发布时间】: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