Fplot command only displaying a limited range. It displays [0,1] even though the range requested is [0,30].
1 次查看(过去 30 天)
显示 更早的评论
The function I am using looks like this:
function va = genMotorVoltage(t)
if t < 10
va = 0.5*t;
elseif t >= 10 && t < 15
va = 5;
elseif t >= 15 && t < 25
va = -0.5*t + 12.5;
elseif t >= 25 && t < 25.5
va = 0;
else
va = NaN;
end
end
The script I use to test is just fplot(@genMotorVoltage, [0,30]);
It generates a plot that is [0,1]. It has the correct values for [0,10], which can only be found by scrolling. Then after [0,10], there is not graph, it simply ends.
It displays this error after scrolling:
> In defaulterrorcallback (line 12)
In matlab.graphics.interaction.validateAndSetLimits (line 28)
In matlab.graphics.interaction.uiaxes/InteractionStrategy/set2DLimits
In matlab.graphics.interaction.uiaxes/InteractionStrategy/setPanLimits
In matlab.graphics.interaction.uiaxes/DefaultAxesInteractionStrategy/setPanLimits
In matlab.graphics.interaction.uiaxes/InteractionStrategy/setUntransformedPanLimits
In matlab.graphics.interaction.uiaxes/DefaultAxesInteractionStrategy/setUntransformedPanLimits
In matlab.graphics.interaction.uiaxes/InteractionStrategy/setUntransformedPanLimitsInternal
In matlab.graphics.interaction.uiaxes/PanBase/move
In matlab.graphics.interaction.uiaxes/Drag/move_drag
In matlab.graphics.interaction.uiaxes.Drag>@(o,evd)hObj.move_drag(o,evd,customevd)
0 个评论
采纳的回答
Voss
2023-3-13
fplot is sending a vector t to your function, not a scalar t. This causes an error if/when it gets to the first &&, which only happens if not all elements of t are less than 10 (i.e., the first if condition evaluates to false). So it runs ok if the xlimits are within [0 10] but beyond that you get an error.
Change your function to handle a vector t, for instance using logical indexing.
fplot(@genMotorVoltage, [0,30]);
function va = genMotorVoltage(t)
idx1 = t < 10;
idx2 = t >= 10 & t < 15;
idx3 = t >= 15 & t < 25;
idx4 = t >= 25 & t < 25.5;
va = NaN(size(t));
va(idx1) = 0.5*t(idx1);
va(idx2) = 5;
va(idx3) = -0.5*t(idx3) + 12.5;
va(idx4) = 0;
end
1 个评论
Steven Lord
2023-3-13
I would suggest a slight modification to the index vectors. If you have a small number of possible intervals, indenting and using <= instead of >= would make the edges of the intervals clearer from the code.
t = 0:30;
idx1 = t < 10;
idx2 = 10 <= t & t < 15; % t is in the interval [10, 15)
idx3 = 15 <= t & t < 25;
idx4 = 25 <= t & t < 25.5;
For a larger number of possible values I'd consider using the discretize function to identify into which interval each value of t falls.
bin = discretize(t, [-Inf 10 15 25 25.5])
results = table(t.', bin.', VariableNames=["t", "bin"])
tail(results) % Show the last couple values of t and bin
You could then use bin == 1 to identify those values of t that fall into bin 1, bin == 2 to identify bin 2, etc.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!