This is one in a series of posts containing short snippets of Sage code for my students to use: see my page on Sage.
Vector fields can be plotted using the vector_field
command:
var('y1,y2') vfield = vector([y1+y2,y1-y2]) plot_vector_field(vfield,(y1,-5,5),(y2,-5,5))
Sometimes it is convenient to plot the normalized version of the vector field:
var('y1,y2') vfield = vector([y1+y2,y1-y2]) nfield = vfield/vfield.norm() plot_vector_field(vfield,(y1,-5,5),(y2,-5,5))
Parametric curves can be plotted using the parametric_plot
function:
var('t') y1(t) = 2*exp(-.1*t)*cos(t) y2(t) = 2*exp(-.1*t)*sin(t) parametric_plot([y1(t),y2(t)], (t,0,5*pi))
Here’s a fun example that has a (normalized) vector field as well as a parametric curve that follows the vector field.
var('t,y1,y2') vfield = vector([y1+y2,y1-y2]) nfield = vfield/vfield.norm() nplot = plot_vector_field(nfield,(y1,-2,5),(y2,-2,5)) y1(t) = (1+sqrt(2))*exp(sqrt(2)*t) + (1-sqrt(2))*exp(-sqrt(2)*t) y2(t) = exp(sqrt(2)*t) + exp(-sqrt(2)*t) pplot = parametric_plot([y1(t),y2(t)], (t,-1,.5)) nplot + pplot
Related to vector fields are the slope field plots used in the differential equations course. Suppose we have the differential equation
.
The corresponding slope field is given by:
var('t','y') plot_slope_field(y*(1-y), (t,0,2), (y,-1,2))
Here is a fun example:
var('t','y') plot_slope_field(y*(1-y)*sin(t), (t,0,10), (y,-1,2))
yo Paul, did you mean to have the normalized vector field code print out the non normalized vector field?
block 2 line 6
plot_vector_field(vfield,(y1,-5,5),(y2,-5,5))
maybe instead:
plot_vector_field(nfield (y1, -5,5)(y2,-5,5))