Second-order differential equation

Second-order differential equation#

Introduction#

Wait a few seconds until Python interaction ready! is shown in the top.

After that follow the instructions in the Grasple Exercise.

On this webpage the initial-value problem

\[\begin{split} \left\{\begin{array}{rcl}\dfrac{d^2y}{dt^2}+a\dfrac{dy}{dt}+by &=&0, \\\\y(0)&=&y_0, \\\\\dfrac{dy}{dt}(0)&=&z_0.\end{array}\right. \end{split}\]

is considered.

This webpage solves this initial-value problem symbolically, using sympy, and then uses numpy and pyplot to display the obtained exact solution.

The general solution is in this case is given by

\[ y(t) = c_1e^{r_1t}+c_2e^{r_2t}, \]

where \(r_1,r_2\) come from the characteristic equation \(r^2+ar+b=0\) (assuming \(a^2-4b\neq0\)), and \(c_1,c_2\) are determined by the initial conditions.

import micropip
await micropip.install("ipympl")
import matplotlib
if not hasattr(matplotlib.rcParams, '_get'):
    matplotlib.rcParams._get = matplotlib.rcParams.get
%matplotlib widget
import sympy as sp
import numpy as np
import matplotlib.pyplot as plt
    
def Second_order_linear_DEs(a,b,y0,z0):

    def solve_ivp(a,b,y0,z0):
        t = sp.symbols('t')
        y = sp.Function('y')
        ode = sp.diff(sp.diff(y(t),t),t)+a*sp.diff(y(t),t)+b*y(t)
        sol = sp.dsolve(ode).rhs
        constants = sp.solve([sol.subs(t,0)-y0,sol.diff(t).subs(t,0)-z0],sp.symbols('C1 C2'))
        final_answer = sol.subs(constants)
        return final_answer

    y = []
    y_vals = []
    t_vals = np.linspace(0, 100, 1001)
    if type(a) is float:
        a = [a]
    for ai in a:
        y.append(solve_ivp(ai,b,y0,z0))
        lam_yA = sp.lambdify(sp.symbols('t'),y[-1],modules=['numpy'])
        y_vals.append(lam_yA(t_vals))

    plt.close('all')
    fig = plt.figure()
    plt.title('$b='+str(b)+',y_0='+str(y0)+',z_0='+str(z0)+'$')
    for i,y_valsi in enumerate(y_vals):
        plt.plot(t_vals,y_valsi,label='$a$='+str(a[i]))
    plt.xlim(0,100)
    plt.xlabel('$t$')
    plt.ylabel('$y(t)$')
    plt.legend()
    plt.show()
    fig.canvas.toolbar_visible = True
    
    pass

INPUT CELL#

In the cell below you should put the values of the parameters you see in Grasple.

Please note: Each attempt in Grasple contains different values for the parameters.

After you run the cell the results of the calculations are shown with the set parameter values.

If you point your mouse below the figure, controls will appear that you can use to pan and zoom.

''' Set the given values in the following lines '''
a = -0.04
b = 0.4
y_0 = 0.1
z_0 = -0.4

''' The next line performs the calculations and shows the results. '''
Second_order_linear_DEs(a,b,y_0,z_0)