在matlab

时间:2017-02-10 18:48:46

标签: matlab

我在Matlab中有两条曲线。

曲线A:

x1 = [128 192   256 384 512 704 1024 1472 2048 2880 4096 5824 8192 11584 16384 23168];
y1 = [0.62 0.51 0.43 0.35 0.3 0.26 0.22 0.18 0.15 0.13 0.11 0.09 0.08 0.06 0.05 0.05];

曲线B:

x2 = [16 24 32 48 64 88 128 184 256 360 512 728 1024 1448 2048 2896];
y2 = [1.94 1.54 1.33 1.15 0.97 0.86 0.71 0.59 0.5 0.42 0.36 0.3 0.25 0.21 0.18 0.15];

在同一图中绘制两条曲线(x轴指数)后:

semilogx(x1,y1,'-o')
hold on
semilogx(x2,y2,'-o')

我发现B曲线高于A曲线。但我想将B向左移动,使B曲线与A曲线重叠。所以问题是,我需要将B曲线移动到重叠A曲线的数量(从右到左)?

一些线索:可能需要计算从B到A(插值)的垂直距离(对于所有匹配点)并对距离求平方并将它们全部加起来并找到Alpha的值。我怎么能在Matlab中做到这一点?

1 个答案:

答案 0 :(得分:2)

我们可以通过首先找到x2的哪些值将曲线B精确地移动到曲线A的顶部来找到所需的移位。这可以通过在对应于y坐标的点处重新采样曲线A来实现。曲线B中的点。以下代码说明了这一点。

由于您在日志域中绘制x轴,我假设您要转移log10(x2)。因此,移位曲线上的x点将为log10(x2) + shift而不是log10(x2 + shift)

% find the subset of y2 which is within the range of y1
idxCommonB = find((y2 <= max(y1)) & (y2 >= min(y1)));
y2c = y2(idxCommonB);
x2c = x2(idxCommonB);

% for each point on curve B, find a new x2 that would move the point on
% curve A
% We use interp1 function for resample the curve. This function requires
% all the points in the domain to be unique. So find the unique elements in
% y1.
[y1_unique,iUnique] = unique(y1);
x2c_desired = interp1(y1_unique, x1(iUnique), y2c, 'linear');

% find the average distance between the desired and given curves
x2_logshift = mean(log10(x2c_desired) - log10(x2c));

% Display the result
fprintf('Required shift in log10(x2) is %f.\n', x2_logshift);
% Required shift in log10(x2) is -0.126954.    

% plot to verify the estimate
figure;
plot(log10(x1),y1,'-o')
hold on
plot(log10(x2),y2,'-o')
plot(log10(x2)+x2_logshift,y2,':*')
grid on;
legend({'A', 'B', 'Shifted B'});
set(gca, 'FontSize', 12);

results