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][1]] for i in (0..len(P)-1))[0]
    x_max = max([P[i][1]] for i in (0..len(P)-1))[0]
    p += plot_slope_field(f, (t, t_min, t_max), (x, x_min, x_max), headaxislength=3, headlength=3)
    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)))

Options

None

Tags

Primary Tags: Differential Equations, Interacts

Secondary Tags: First-Order Differential Equations: Direction Fields

Related Cells

  • desolve. Solving ordinary differential equations with desolve.
  • 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.
  • desolve_laplace. Solving ordinary differential equations using Laplace transforms.

Attribute

Author: T. Judson

Permalink:

Date: 17 Jun 2017 16:24

Submitted by: Tom Judson

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License