简体   繁体   中英

How to plot slope fields in Matlab?

I am new to Matlab (literally just downloaded it) and I would like to know how to create a slope field and integral curves.

My equation is dy/dx = x^2/(1-y^2).

My attempt at code is:

Ffun = @(X,Y)X.^2./(1-Y.^2);               % function f(x,y)
[X,Y]=meshgrid(-5:.5:5,-5:.5:5); % choose the plot sizes
DY=Ffun(X,Y); DX=ones(size(DY)); % generate the plot values 
quiver(X,Y,DX,DY);
hold on;
contour(X,Y,DY,10); 

I keep getting: "Warning: Matrix is singular to working precision. In @(X,Y)X^2/(1-Y^2) I also get blank graphs.

Also, it would be nice if I could get the positive, negative, and zero slopes in different colors.

Help would be appreciated, thank you!

You are producing inf values when evaluating this function, as it has poles around Y=+-1. The inf makes the plot to scale to absurdity.... To still plot this you can transform all inf values to NaN (not a number does not show in a plot) as follows:

DY(isinf(DY))=NaN;
quiver(X,Y,DX,DY);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM