我编写了以下代码来绘制两种不同模型(蓝线和绿线)的velicity-course及它们之间的相对差异(橙线):
%% ----- 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
方向前进,但仅适用于小于1的除数,这对我来说是合乎逻辑的:
答案 0 :(得分:2)
相对差异(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