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!

Answers


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);

Need Your Help

linux process constantly drops connections

linux tcp boost-asio

I have written a server application that is currently running on a Ubuntu 12.04 x64 VM. The application connects to another server, sucks some data from this server, transforms it and then sends it...

ParseQuery not taking into consideration Where clause WindowsPhone

parse-platform windows-phone-8.1

I made a new query to select from Article Class with where clause for each item selected. However, it keeps getting the whole list every time although there are selected fields!