Mathematical Principles for Scientific Computing and Visualization
http : // www.farinhansford.com/books/scv
by Gerald Farin and Dianne Hansford

This notebook demonstrates slope fields and ODEs

Figures 9.1 was created with this notebook.

The slope field is a special case of a vectorfield, and as such it is created using the vectorfield function in Mathematica. The vector field function takes (f_x, f_y), so for our plot to show f_y over the domain t, we set f_x to a constant.

The latter part of the notebook demonstrates that some slope fields can generate very different solutions when the start point is change very slightly.

See Chapter 9 : Computing Dynamic Processes for details.

Updated July 2008

(* Load the vector filed package *)
Needs["VectorFieldPlots`"];

(* Set the output folder *)
SetDirectory[ToFileName[Extract["FileName"/.NotebookInformation[EvaluationNotebook[]],{1},FrontEnd`FileName]]];

(* Create a slope field *)
vecfield=VectorFieldPlot[{1,y},{x,-2,2},{y,-2,2},PlotPoints->20,Frame->True,BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11}];

(* Solve an ODE for given start value y(0) = 1 *)
ssol1=NDSolve[{y'[x]==y[x],y[0]==1.0},y[x],{x,-2,2}];
ssol1=ssol1[[1,1,2]];

(* Solve the same ODE with a different start value, y(0) = 0.2 *)
ssol2=NDSolve[{y'[x]==y[x],y[0]==0.2},y[x],{x,-2,2}];
ssol2=ssol2[[1,1,2]];

(* Create plots of the first and second ODE solution *)
ssplot1=Plot[ssol1,{x,-2,0.8`},PlotStyle->{RGBColor[0.3, 0.3, 0.8],Thickness[0.01`]},BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11}];

ssplot2=Plot[ssol2,{x,-2,2},PlotStyle->{RGBColor[0.4, 0.4, 0.9],Thickness[0.01`]},BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11}];

(* Create a plot of the start points for the two ODES above *)
ppoints=ListPlot[{{0,1},{0,0.2`}},Axes->False,PlotStyle->PointSize[0.03]];
ppoint=ListPlot[{{0,1.`}},Axes->False,PlotStyle->PointSize[0.03]];

(* Create the plots *)
(* Top left of FIgure 9.1 *)
pic=Show[{vecfield}]
Export["vecfield.eps",pic];

pic2=Show[{vecfield,ssplot1,ppoint}]
Export["vecfield2.eps",pic2];

pic3=Show[{vecfield,ssplot1,ssplot2,ppoints}]

(* Output this to a postscript file *)
Export["vecfield3.eps",pic3];

(* Create a different slope field *)
vecfield1=VectorFieldPlot[{1,y-0.4` (x^2)},{x,-2,2},{y,-2,2},PlotPoints->20,Frame->True,BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11}];

(* Solve this ODE with a given start value *)
newsol1=NDSolve[{y'[x]==y[x]-0.4` (x^2),y[-2]==0.7`},y[x],{x,-2,2}];
newsol1=newsol1[[1,1,2]];

(* Now solve it with a different start value *)
newsol2=NDSolve[{y'[x]==y[x]-0.4` (x^2),y[-2]==0.76`},y[x],{x,-2,2}];
newsol2=newsol2[[1,1,2]];

(* Create plots of the elements *)
newsplot1=Plot[newsol1,{x,-2,2},PlotStyle->{RGBColor[0.3, 0.3, 0.8],Thickness[0.01`]}];

newsplot2=Plot[newsol2,{x,-2,2},PlotStyle->{RGBColor[0.4, 0.4, 0.9],Thickness[0.01`]}];

points=ListPlot[{{-2,0.7`},{-2,0.76`}},Axes->False,PlotStyle->PointSize[0.03`]];

pic1=Show[{vecfield1,newsplot1,newsplot2,points}]

(* Output this to a postscript file *)
Export["vecfield1.eps",pic1];