Slope Fields

Manipulate[
xincr=(xmax-xmin)/(xpart-1);
yincr=(ymax-ymin)/(ypart-1);
dirvec [ComplexInfinity]:={0,1};
dirvec[Indeterminate]:={0,0};
(* The next line is based on Euler's linear element formula h = (H/(2(Sqrt[1+(f^2)((Subscript[x, 0]),(Subscript[y, 0]))]))) for a segment of length H with midpoint ((Subscript[x, 0]), (Subscript[y, 0])) and endpoints ((Subscript[x, 0])-h, (Subscript[y, 0])-hM) and ((Subscript[x, 0])+h, (Subscript[y, 0])+hM)for M = f((Subscript[x, 0]), (Subscript[y, 0])) being the slope of the segment.*)
dirvec[x_]:={1,x}/Sqrt[1+(x/((ymax-ymin)/(xmax-xmin)))^2];
pts=Flatten[Table[N[{r,s}],{r,xmin,xmax,xincr},{s,ymin,ymax,yincr}],1];
dir=f/.{x->pts[[All,1]],y->pts[[All,2]]}//Quiet;
newdir=Map[dirvec,dir];
endpts=pts+xincr/4*newdir;
frontpts=pts-xincr/4*newdir;
(*Now find an exact solution*)
ff=f/.y->y[x];
soln=Quiet@DSolve[{y'[x]==ff,y[xinit]==yinit},y[x],x];
If[soln≠{},
table=Table[soln[[1,1,2]]/.C[1]->xinit];
curve=Plot[Evaluate[table],{x,xmin,xmax}];
,curve={}];
Show[ Graphics[Map[Line,Transpose[{frontpts,endpts}]]],If[ptflg,Graphics[Point[pts]],{}],If[solflg,curve,{}] ,If[solflg,Graphics[{PointSize[Medium],Point[{xinit,yinit}]},{}],{}],Axes->Automatic,PlotRange->{{xmin,xmax},{ymin,ymax}},AspectRatio->1.5/GoldenRatio,PlotLabel->Grid[{{Style[Framed[Row[{"slope field for ",y'," = ",StringForm["``",f]}]]]},{}}],ImageSize->{350,350},ImagePadding->{{25,10},{25,55}}
],

{{f,x-y,"f(x,y) = "},
{x-y->TraditionalForm[x-y],
y-x->TraditionalForm[y-x],
x+y->TraditionalForm[x+y],
1/y->TraditionalForm[1/y],
1/x->TraditionalForm[1/x],
x/y->TraditionalForm[x/y],
-x/y->TraditionalForm[-x/y],
-y/x->TraditionalForm[-y/x],
x^2y->TraditionalForm[x^2y],
y-1->TraditionalForm[y-1],
y^2-1->TraditionalForm[y^2-1],
(y+x)/(y-x)->TraditionalForm[(y+x)/(y-x)],
Sin[y]->TraditionalForm[Sin[y]],
Cos[x]->TraditionalForm[Cos[x]]},
ControlType->PopupMenu},

Delimiter,
"x axis values:",
{{xmin,-2},-8,-1,1,Appearance->"Labeled",ImageSize->Tiny},
{{xmax,2 },1,8,1,Appearance->"Labeled",ImageSize->Tiny},
Delimiter,
"y axis values:",
{{ymin,-3 },-10,-.5,.01,Appearance->"Labeled",ImageSize->Tiny},
{{ymax,3 },.5,10,.01,Appearance->"Labeled",ImageSize->Tiny},
Delimiter,
"number of sample points:",
{{xpart,15,"x axis"},4,20,1,Appearance->"Labeled",ImageSize->Tiny},
{{ypart,15,"y axis"},4,20,1,Appearance->"Labeled",ImageSize->Tiny},


Delimiter,
{{ptflg,False,Tooltip["display points","Show the midpoint of each segment."]},{True,False}},
{{solflg,False,Tooltip["show exact solution","Graph a function solving the differential equation and the initial condiions."]},{True, False}},
(* the next two lines can be used instead of the popup menues for initial values;
input will be limited to numeric values
{{xinit,1,"initial x value "}, InputField[#1,Number,FieldSize->5,Enabled->solflg]&},
{{yinit,1,"initial y value "}, InputField[#1,Number,FieldSize->5,Enabled->solflg]&},
*)
{{xinit,1,"initial x value" }, xmin,xmax,.1,Appearance->"Labeled",ImageSize->Tiny,Enabled->solflg},
{{yinit,1 ,"initial y value"},ymin,ymax,.1,Appearance->"Labeled",ImageSize->Tiny,Enabled->solflg},
TrackedSymbols:>Manipulate,AutorunSequencing->{1,3,5,7}
]

CAPTION

This Demonstration generates a slope field for a number of differential equations. You have the option to plot a particular solution passing through one point. You can control the x and y axes as well as the number of segments plotted. You can display the midpoints of the segments.

THIS NOTEBOOK IS THE SOURCE CODE FROM

"Slope Fields" from the Wolfram Demonstrations Project
 http://demonstrations.wolfram.com/SlopeFields/

Contributed by: Charles E. Oelsner

A full-function Wolfram Mathematica system (Version 6 or higher) is required to edit this notebook.
GET WOLFRAM MATHEMATICA »

© Wolfram Demonstrations Project & Contributors   |   Terms of Use   |   Make a new version of this Demonstration »