no arrows appear when plotting direction field with quiver()

I have an equation y' = x - (1/y) and I need to make a direction field for it. Here is the code I have tried:
if true
[x,y] = meshgrid(-10:1:10 -10:1:10);
dy = x - (1./y);
dx = ones(size(y));
quiver(x,y,dx,dy)
end
When I input this I only end up with a grid of points, no vectors. Although when I place a Datatip in the figure window it does display the componets of the vectors that are supposed to be there and correctly. Still i see no reason for the arrows not to appear.
What might I be doing wrong?

1 个评论

I just realized something that may or may not be important, at some points dy is equal to Inf. Would that mess it up or would it just draw a straight line up?

请先登录,再进行评论。

 采纳的回答

The infs are messing with you.
[x,y] = meshgrid(-10:1:10,-10:1:10);
F = x - (1./y);
[dx,dy] = gradient(F,1,1);
dy(isinf(dy)) = nan;
quiver(x,y,dx,dy);

3 个评论

I see I've worked it out by editing my domain and range.
BTW, does Matlab scale the vectors relative to each other?
Because if one of my slopes get too steep it seems that the others just get reduced to dots.
Thank you for the tip, but could you explain the
dy(isinf(dy)) = nan;
part?
isinf(dy) is true exactly at the infinities. dy(isinf(dy)) is logical indexing, selecting the dy locations that have the infinities. Those are then assigned NaN. MATLAB graphics know that NaN are not to be plotted.
That line just sets any inf values in dy to nan. When plotting, nans don't show up.
dy = [ 3 4 5 inf 2 3 inf 1 inf];
dy(isinf(dy)) = nan;
MATLAB scales the arrows to fit within the grid, then stretches them by the optional input scale factor. To see them without scaling, use 0 for the scale value.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Vector Fields 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by