[Go to the Notes on Diffy Qs home page]
Press the Evaluate button below to launch the Sage demonstration. You may have to wait a little before the graph appears. Be patient. To change the function, you should edit the code and press the Evaluate button again. Note that Sage ignores any line starting with a pound sign #, so those lines are simply comments for a human.
Here we plot the slope field of
xxxxxxxxxx
var("x y")
# We will plot the slope field of y' = f(x,y)
f(x,y) = 0.5*cos(x-y)-sin(x^2+y)
# Some more examples
#f(x,y) = sin(y^2)-0.5*x
#f(x,y) = cos(x*y^2)
#f(x,y) = 0.5*x
#f(x,y) = 0.5*y
#f(x,y) = 0.5*y^2
# The plot window
xmin = -2
xmax = 2
ymin = -2
ymax = 2
# Plot the slope field
sf = plot_slope_field(f(x,y), (x, xmin, xmax), (y, ymin, ymax), aspect_ratio=1, plot_points=20)
sf.show()
If you do not need a fixed aspect ratio (x and y units are the same), remove the "aspect_ratio=1," from the above. If you want to plot more little lines, you can change the "plot_points=20" parameter.
In this demonstration we plot the slope field and have the compute draw
a solution with a certain initial condition
xxxxxxxxxx
var("x y")
# We will plot the slope field of y' = f(x,y)
f(x,y) = 0.5*cos(x-y)-sin(x^2+y)
# Some more examples
#f(x,y) = sin(y^2)-0.5*x
#f(x,y) = cos(x*y^2)
#f(x,y) = 0.5*x
#f(x,y) = 0.5*y
#f(x,y) = 0.5*y^2
# The initial condition y(x0) = y0
x0 = 0.0
y0 = 0.5
# The plot window
xmin = -2
xmax = 2
ymin = -2
ymax = 2
# Plot the slope field
sf = plot_slope_field(f(x,y), (x, xmin, xmax), (y, ymin, ymax), aspect_ratio=1, plot_points=20)
# Plot the solution
sol = desolve_rk4(f(x, y), y, ivar=x, ics=[x0, y0],
end_points=[xmin,xmax], step=0.1, output='plot')
# The point of the initial condition
pt = point([x0,y0], size=20, color='red')
# Show both plots
(sf+sol+pt).show()