First-order linear differential equation

First-order linear 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 differential equation

\[ y'(t)=ay(t)+b, \]

is considered,with the initial condition \(y(0)=y_0\).

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

The exact solution is in this case is given by

\[ y(t) = \left(y_0+\frac{b}{a}\right)e^{at}-\frac{b}{a}. \]
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 First_order_linear_DEs(a,b,y0):

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

    t = sp.symbols('t')

    yA = -b/a
    t_vals = np.linspace(0, 10, 101)
    yA_vals = yA*np.ones_like(t_vals)

    yB = solve_ivp(a,b,y0)
    lam_yB = sp.lambdify(t,yB,modules=['numpy'])
    yB_vals = lam_yB(t_vals)

    plt.close('all')
    fig = plt.figure()
    plt.plot(t_vals,yA_vals,label='Case A')
    plt.plot(t_vals,yB_vals,label='Case B')
    plt.xlim(0,10)
    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.1
b = 0.8
y_0 = -7.3

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