当它应该趋向于 0 时,相对差趋向于负无穷大
The relative difference goes towards negative infinity when it's supposed to go towards 0
我编写了以下代码来绘制两个不同模型(蓝色和绿色线)的速度过程以及它们之间的相对差异(橙色线):
%% ----- Absolulte Carvelocity v[km/h] in carfixed Coordinate system ----
% Define Plotproperties
figure('Position', [100, 100, 1024, 768]);
hold on; grid on;
title('Absolute Velocity');
xlabel('time t[s]');
ylabel('absolute velocity v[km/h]');
xlim([t_start t_end]);
% Calculate the difference between the models
t = erg_sl.Vehicle.v_ms.Time;
v_ms = reshape(erg_sl.Vehicle.v_ms.Data,1,[],1);
diff=zeros(1,size_cmtime(2));
for ctr=1:size_cmtime(2)
diff(1,ctr)=-(erg_cm.Car_v.data(ctr)*3.6-v_ms(1,round(ctr*fac))*3.6)/abs(erg_cm.Car_v.data(ctr)*3.6)*100;
end
% From Carmaker
line(erg_cm.Time.data,erg_cm.Car_v.data .* 3.6,'Color','b','LineWidth',2)
% From Simulink
line(erg_sl.Vehicle.v_ms.Time,erg_sl.Vehicle.v_ms.Data .* 3.6,'Color',[0,0.6,0.5],'LineWidth',2)
% Legend
legend('Carmaker','Simulink');
% Change axes
ax1 = gca;
%ax1.XLim = [0 20];
%ax1.YLim = [-15 15];
ax2 = axes('Position',ax1.Position,'YAxisLocation','right', 'Color','none','YColor',[255,127,80]/256);
ax2.YLim = [-30 30];
ax2.YLabel.String = 'Relative difference [%]';
linkaxes([ax1,ax2],'x');
% Plot the difference
line(erg_cm.Time.data,diff,'Parent',ax2,'LineWidth',2,'Color',[255,127,80]/256)
% legend
legend('Relative Abweichung','Location','South')
% ------------------------------------------------------------------------
实际上它工作得很好,我得到了这个情节:
现在的问题是,开始时差值实际上几乎为零,但橙色线(即相对差值)走向负无穷大。您甚至可以看到从 t=0
到 t~75
绿线甚至在蓝线上方,因此橙色线应该在 0 以上。那么代码有什么问题,所以会发生这种情况?
编辑:这是同一脚本的另一个情节,看起来更像我的预期。橙色线也朝向 -inf
,但仅适用于小于我认为合乎逻辑的除数:
相对差异(x-y)./x
可以趋向于inf
或-inf
,即使绝对 差异 x-y
趋于 0
.
考虑例如:
t = logspace(0,-5,8); %// tends to zero
x = t; %// tends to zero
y = sqrt(t); %// tends to zero, but more slowly than x
absDiff = x-y; %// tends to zero
relDiff = (x-y)./x %// tends to -inf
这给出了
t =
1.0000 0.1931 0.0373 0.0072 0.0014 0.0003 0.0001 0.0000
absDiff =
0 -0.2463 -0.1558 -0.0776 -0.0359 -0.0161 -0.0071 -0.0032
relDiff =
0 -1.2758 -4.1795 -10.7877 -25.8270 -60.0540 -137.9495 -315.2278
我编写了以下代码来绘制两个不同模型(蓝色和绿色线)的速度过程以及它们之间的相对差异(橙色线):
%% ----- Absolulte Carvelocity v[km/h] in carfixed Coordinate system ----
% Define Plotproperties
figure('Position', [100, 100, 1024, 768]);
hold on; grid on;
title('Absolute Velocity');
xlabel('time t[s]');
ylabel('absolute velocity v[km/h]');
xlim([t_start t_end]);
% Calculate the difference between the models
t = erg_sl.Vehicle.v_ms.Time;
v_ms = reshape(erg_sl.Vehicle.v_ms.Data,1,[],1);
diff=zeros(1,size_cmtime(2));
for ctr=1:size_cmtime(2)
diff(1,ctr)=-(erg_cm.Car_v.data(ctr)*3.6-v_ms(1,round(ctr*fac))*3.6)/abs(erg_cm.Car_v.data(ctr)*3.6)*100;
end
% From Carmaker
line(erg_cm.Time.data,erg_cm.Car_v.data .* 3.6,'Color','b','LineWidth',2)
% From Simulink
line(erg_sl.Vehicle.v_ms.Time,erg_sl.Vehicle.v_ms.Data .* 3.6,'Color',[0,0.6,0.5],'LineWidth',2)
% Legend
legend('Carmaker','Simulink');
% Change axes
ax1 = gca;
%ax1.XLim = [0 20];
%ax1.YLim = [-15 15];
ax2 = axes('Position',ax1.Position,'YAxisLocation','right', 'Color','none','YColor',[255,127,80]/256);
ax2.YLim = [-30 30];
ax2.YLabel.String = 'Relative difference [%]';
linkaxes([ax1,ax2],'x');
% Plot the difference
line(erg_cm.Time.data,diff,'Parent',ax2,'LineWidth',2,'Color',[255,127,80]/256)
% legend
legend('Relative Abweichung','Location','South')
% ------------------------------------------------------------------------
实际上它工作得很好,我得到了这个情节:
现在的问题是,开始时差值实际上几乎为零,但橙色线(即相对差值)走向负无穷大。您甚至可以看到从 t=0
到 t~75
绿线甚至在蓝线上方,因此橙色线应该在 0 以上。那么代码有什么问题,所以会发生这种情况?
编辑:这是同一脚本的另一个情节,看起来更像我的预期。橙色线也朝向 -inf
,但仅适用于小于我认为合乎逻辑的除数:
相对差异(x-y)./x
可以趋向于inf
或-inf
,即使绝对 差异 x-y
趋于 0
.
考虑例如:
t = logspace(0,-5,8); %// tends to zero
x = t; %// tends to zero
y = sqrt(t); %// tends to zero, but more slowly than x
absDiff = x-y; %// tends to zero
relDiff = (x-y)./x %// tends to -inf
这给出了
t =
1.0000 0.1931 0.0373 0.0072 0.0014 0.0003 0.0001 0.0000
absDiff =
0 -0.2463 -0.1558 -0.0776 -0.0359 -0.0161 -0.0071 -0.0032
relDiff =
0 -1.2758 -4.1795 -10.7877 -25.8270 -60.0540 -137.9495 -315.2278