Fplot command only displaying a limited range. It displays [0,1] even though the range requested is [0,30].

4 vues (au cours des 30 derniers jours)
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)

Réponse acceptée

Voss
Voss le 13 Mar 2023
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 commentaire
Steven Lord
Steven Lord le 13 Mar 2023
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])
bin = 1×31
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 NaN NaN NaN NaN
results = table(t.', bin.', VariableNames=["t", "bin"])
results = 31×2 table
t bin __ ___ 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 2 11 2 12 2 13 2 14 2 15 3
tail(results) % Show the last couple values of t and bin
t bin __ ___ 23 3 24 3 25 4 26 NaN 27 NaN 28 NaN 29 NaN 30 NaN
You could then use bin == 1 to identify those values of t that fall into bin 1, bin == 2 to identify bin 2, etc.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Produits


Version

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by