Plotting Direction Fields for Differential Equations

## Description

We will plot the direction field for \$dy/dt = 2y(1- y)\$ for \$-2 < t < 2\$ and \$-2 < y < 2\$.

## Sage Cell

#### Code

``````y, t = var('y, t')
f = 2*y*(1 - y)
x_min = -2  #set the window size
x_max = 2
y_min = -2
y_max = 2

#plot the slope field and display
p = plot_slope_field(f, (t, x_min, x_max), (y, y_min, y_max), headaxislength=3, headlength=3, axes_labels = ['\$t\$','\$y\$'], xmin = x_min, xmax = x_max, ymin = y_min, ymax = y_max)
p```
```

## Options

#### Option

We add three solution curves to the plot (\$y(0) = 0.5\$, \$y(0) = 1.01\$, and \$y(0) = -0.01\$).

#### Code

``````y, t = var('y, t')
f = 2*y*(1 - y)
x_min = -2  #set the window size
x_max = 2
y_min = -2
y_max = 2

t_min = -2 #set the min and max for t
t_max = 4
t_start = 0 #set the initial conditions
y_start = 0.5

#plot first solution
P = desolve_rk4(f, y, ics=[t_start, y_start], ivar = t, end_points=[t_min, t_max], thickness=3)
p = line(P, title="\$y' = 2y(1-y)\$")

#plot second solution
y_start = 1.01
P = desolve_rk4(f, y, ics=[t_start, y_start], ivar = t, end_points=[t_min, t_max], thickness=3)
p += line(P)

#plot third solution
y_start = -0.01
P = desolve_rk4(f, y, ics=[t_start, y_start], ivar = t, end_points=[t_min, t_max], thickness=3)
p += line(P)

#plot the slope field and display
p += plot_slope_field(f, (t, x_min, x_max), (y, y_min, y_max), headaxislength=3, headlength=3, axes_labels = ['\$t\$','\$y\$'], xmin = x_min, xmax = x_max, ymin = y_min, ymax = y_max)
p```
```

#### Option

We now add the two equilibrium solutions (\$y = 0\$ and \$y = 2\$) to the plot.

#### Code

``````y, t = var('y, t')
f = 2*y*(1 - y)
x_min = -2  #set the window size
x_max = 2
y_min = -2
y_max = 2

t_min = -2 #set the min and max for t
t_max = 4
t_start = 0 #set the initial conditions
y_start = 0.5

#plot first solution
P = desolve_rk4(f, y, ics=[t_start, y_start], ivar = t, end_points=[t_min, t_max], thickness=3)
p = line(P, title="\$y' = 2y(1-y)\$")

#plot second solution
y_start = 1.01
P = desolve_rk4(f, y, ics=[t_start, y_start], ivar = t, end_points=[t_min, t_max], thickness=3)
p += line(P)

#plot third solution
y_start = -0.01
P = desolve_rk4(f, y, ics=[t_start, y_start], ivar = t, end_points=[t_min, t_max], thickness=3)
p += line(P)

#plot equilibrium solutions
p += plot(0, (y, x_min, x_max), color = "red", thickness=2, linestyle="--")
p += plot(1, (y, x_min, x_max), color = "red", thickness=2, linestyle="--")

#plot the slope field and display
p += plot_slope_field(f, (t, x_min, x_max), (y, y_min, y_max), headaxislength=3, headlength=3, axes_labels = ['\$t\$','\$y\$'], xmin = x_min, xmax = x_max, ymin = y_min, ymax = y_max)
p```
```

## Tags

Primary Tags: Differential Equations: First order differential equations

Secondary Tags: First order differential equations: Direction fields