Interact for Direction Fields for Differential Equations

## Description

This is a Sage interact for plotting direction fields and solutions for first-order differential equations. The default equation is

(1)
\begin{align} \frac{dx}{dt} = \frac{1}{2} x^2 - t. \end{align}

## Sage Cell

#### Code

x, t = var('x,t')
@interact
def _(f = input_box(default = x^2/2 - t),
t_min = input_box(default = -1.0),
t_max = input_box(default = 6),
t_start = input_box(default = 1),
x_start = input_box(default = -0.1),
step_size = (0.1, (0.001, 1.0))):
P = desolve_rk4(f,x,ics=[t_start, x_start],ivar=t,end_points=[t_min, t_max], step = step_size, thickness=3)
p = line(P, thickness = 2, axes_labels = ['$t$','$x$'],)
x_min = min([P[i]] for i in (0..len(P)-1))
x_max = max([P[i]] for i in (0..len(P)-1))
p += arrow(P[int(len(P)/2)], P[int(len(P)/2) + 1], width = 1, arrowsize = 8)
p += point((t_start, x_start), size=60, color="red")
p.show()
pretty_print(html(r"$\displaystyle\frac{dx}{dt} = %s$"  %latex(f)))


None

## Tags

Primary Tags—Differential Equations: First-order differential equations; Programming: Python

Secondary Tags—First-order differential equations; Direction fields; Python: Interacts

## Related Cells

• desolve. Solving ordinary differential equations with desolve.
• desolve_system. Solving a system of ordinary differential equations with desolve_system.
• desolve_odeint. Solving ordinary differential equations numerically with desolve_odeint.
• Euler's Method. eulers_method implements Euler’s method for finding a numerical solution of the first-order ODE $y′=f(x,y)$.
• Euler's Method for Systems. eulers_method_2x2 implements Euler’s method for finding a numerical solution of a $2 \times 2$ system of first-order ODEs.

]]].

## Attribute

Author: T. Judson