Laplace transformations

Laplace transformations#

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}y''+k^2y &=&F_0\left(u_a\left(t\right)-u_{a+d}\left(t\right)\right), \\\\y(0)&=&y_0, \\\\y'(0)&=&v_0.\end{array}\right. \end{split}\]

is considered for a pendulum.

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

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
from IPython.display import Markdown, clear_output

def Laplace_transformations(a,d,y_0,v_0,F_0,k):
    
    a = sp.nsimplify(a)
    d = sp.nsimplify(d)
    y_0 = sp.nsimplify(y_0)
    v_0 = sp.nsimplify(v_0)
    F_0 = sp.nsimplify(F_0)
    k = sp.nsimplify(k)
    
    display(Markdown('# The figure is being rendered, please be patient.'))
    
    t = sp.Symbol('t', positive=True)
    s = sp.symbols('s')
    y = sp.Function('y')(t)
    
    def apply_initial_conditions(L,ic):
        wf = sp.Wild('f')
        lw = sp.LaplaceTransform(wf,t,s)
        ll=L.find(lw)
        for l in ll: 
            lz=l.match(lw)[wf]
        newL = L
        icl = [lz.diff(t,i).subs(t,0) for i in range(0,len(ic))]
        for i in range(0,len(ic)):
            newL = newL.replace(icl[i],ic[i])
        return(newL)

    def laplace(e, t, s,y_0,v_0):
        res = sp.laplace_transform(e, t, s, noconds=True)
        wf = sp.Wild('f')
        lw = sp.LaplaceTransform(wf, t, s)
              
        res = apply_initial_conditions(res,[y_0,v_0])
        
        return res
    
    # define the ode
    ode = sp.Derivative(y,t,t)+k**2*y-F_0*(sp.Heaviside(t-a)-sp.Heaviside(t-(a+d)))

    # transform
    L_eq = laplace(ode,t,s,y_0,v_0)
        
    # simplify
    L_y = sp.Function('L_y')(s)
    L_eq = L_eq.replace(sp.LaplaceTransform(y,t,s),L_y)
    
    # solve
    L_y = sp.solve(L_eq,L_y)[0]

    # inverse transform
    y = sp.inverse_laplace_transform(L_y,s,t)
    
    y_str = str(y).replace(', _None','').replace('InverseLaplaceTransform','sp.inverse_laplace_transform').replace('pi','sp.pi').replace('exp','sp.exp')
    for fun in ['sqrt','sin','cos','tan','I','Heaviside']:
        y_str = y_str.replace(fun,'sp.'+fun)
    y = eval(y_str)
    
    # eval
    T = np.array(2*sp.pi/k).astype(np.float64)
    t_vals = np.linspace(0, 5*T, 1001)
    lam_y = sp.lambdify(t,y,modules=['numpy'])
    y_vals = lam_y(t_vals)
    
    clear_output()
    
    plt.close('all')
    fig = plt.figure()
    plt.plot(t_vals,np.real(y_vals),label='$y(t)$')
    ylims = np.copy(plt.ylim())
    ylims = [np.floor(ylims[0]*10)/10,np.ceil(ylims[1]*10)/10]
    plt.plot([a,a],ylims,label='$t=a$')
    plt.plot([a+d,a+d],ylims,label='$t=a+d$')
    plt.xlim(0,5*T)
    plt.ylim(ylims)
    plt.xlabel('$t$')
    plt.ylabel('$y(t)$')
    plt.grid()
    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 = 2.2
d = 0.05
y_0 = -0.09
v_0 = 0
F_0 = 40.00*(sp.pi)**2
k = 2.00*sp.pi

''' The next line performs the calculations and shows the results. '''
Laplace_transformations(a,d,y_0,v_0,F_0,k)

The figure is being rendered, please be patient.