Interact to Plot Phase Planes of Systems of Differential Equations

## Description

A Sage interact Interact that plots phase planes of systems of Differential Equations with nullclines. The default plot is

(1)
\begin{align} x' & = x(1 - x) - 2xy \\ y' & = y(1-y) - 2xy. \end{align}

There is an option to include $x$ and $y$-nullclines.

## Sage Cell

#### Code

x,y,t = var('x,y,t')
@interact
def _(f = input_box(default = "x*(1 - x) - 2*x*y"),
g = input_box(default = "y*(1-y) - 2*x*y"),
x_min = input_box(default = 0), x_max = input_box(default = 1.5),
y_min = input_box(default = 0), y_max = input_box(default = 1.5),
t_start = input_box(default = 0.0), x_start = input_box(default = 1.4), y_start = input_box(default = 1.3),
step_size = (0.01, (0.001, 1.0)), t_end = (10, (5, 50)),
x_null = checkbox(False, "Show $x$-nullcline (green)"),
y_null = checkbox(False, "Show $y$-nullcline (red)")):
n = sqrt(f^2 + g^2)
p = plot_vector_field( (f/n, g/n), (x, x_min, x_max), (y, y_min, y_max))
p += point((x_start, y_start), pointsize=50)
P = desolve_system_rk4([f, g], [x, y],ics = [t_start, x_start, y_start], ivar = t, end_points = t_end, step = step_size)
Q = [ [j,k] for i,j,k in P]
p += line(Q, axes_labels = ['$x(t)$','$y(t)$'])
p += arrow(Q[int(len(Q)) - 2], Q[int(len(Q)) - 1], width = 1, arrowsize = 4)
x_nullcline = implicit_plot(f, (x, x_min, x_max), (y, y_min, y_max), color = 'green', legend_label = '$x$-nullcline')
y_nullcline = implicit_plot(g, (x, x_min, x_max), (y, y_min, y_max), color = 'red', legend_label = '$y$-nullcline')
if x_null:
p += x_nullcline
if y_null:
p += y_nullcline
p.show(xmin = x_min, xmax = x_max, ymin = y_min, ymax = y_max, aspect_ratio = 1)
pretty_print(html("<br />"))
pretty_print(html(r"$\displaystyle\frac{dx}{dt} = %s$"  %latex(f)))
pretty_print(html("<br />"))
pretty_print(html(r"$\displaystyle\frac{dy}{dt} = %s$"  %latex(g)))


None.

## Tags

Primary Tags—Differential equations: Systems of differential equations; Programming: Python

Secondary Tags—Systems of differential equations: Phase planes; Python: Interacts