<jupyter_start><jupyter_text>Trasformata di LaplaceIn matematica, la trasformata di Laplace, dal nome del suo inventore <NAME>, è una trasformata integrale che converte una funzione di una variabile reale $t$ (spesso il tempo) in una funzione di una variabile complessa $s$ (frequenza complessa). Questa trasformata ha molte applicazioni nella scienza e nell'ingegneria in quanto risulta uno strumento efficace per la risoluzione di equazioni differenziali. In particolare, trasforma le equazioni differenziali in equazioni algebriche e la convoluzione di due funzioni nella loro moltiplicazione.In questo esempio, viene presentata una "tabella interattiva" per la trasformata di Laplace: alla selezione della funzione $f(t)$ (tramite menù a tendina), ne viene generato il grafico e viene fornita la corrispondente trasformata di Laplace $F(s)$.<jupyter_code>%matplotlib inline
#%config InlineBackend.close_figures=False 
from ipywidgets import interactive
from ipywidgets import widgets
from IPython.display import Latex, display, Markdown # For displaying Markdown and LaTeX code
import matplotlib.pyplot as plt
import numpy as np
import math
import random
import matplotlib.patches as mpatches
from IPython.display import HTML, clear_output
from IPython.display import display
from ipywidgets.widgets.interaction import show_inline_matplotlib_plots

# Ignore division by zero warning
#np.seterr(divide='ignore')

# Patches
blue_patch = mpatches.Patch(color='blue', label='$f(t)$')

# Layouts
panel_layout = widgets.Layout(border='solid blue', width = '40%', height = '440px', padding='5px')
plot_layout = widgets.Layout(border='solid red', width = '60%', height = '440px', padding='5px')
output_panel = widgets.Output(layout = panel_layout)
output_plot = widgets.Output(layout = plot_layout)

# Widgets
n_slider = widgets.IntSlider(description='n:', min=0, max=5, step=1, continuous_update=False)
c_slider = widgets.FloatSlider(description='c:', min=0, max=4.5, step=0.25, continuous_update=False)
k_slider_exp = widgets.IntSlider(description='k:', min=0, max=4, step=1, continuous_update=False)
a_slider_sin = widgets.FloatSlider(description='a:', min=0, max=10, step=0.25, continuous_update=False)
a_slider_cos = widgets.FloatSlider(description='a:', min=0, max=10, step=0.25, continuous_update=False)
a_slider_expsin = widgets.FloatSlider(description='a:', min=0, max=10, step=0.25, continuous_update=False)
k_slider_expsin = widgets.IntSlider(description='k:', min=0, max=4, step=1, continuous_update=False)
a_slider_expcos = widgets.FloatSlider(description='a:', min=0, max=10, step=0.25, continuous_update=False)
k_slider_expcos = widgets.IntSlider(description='k:', min=0, max=4, step=1, continuous_update=False)


example_select = widgets.Dropdown(
    options=['funzione lineare', 'funzione costante', 'funzione potenza', 'funzione esponenziale', 'sinusoide', 'cosinusoide', 'sinusoide smorzata', 'cosinusoide smorzata'],
    value='funzione lineare',
    description='Funzione:',
    disabled=False,
    style = {'description_width': 'initial'},
)


#functions
def first():
    with output_plot:
        linear_fun()
        
    with output_panel:
        output_panel.clear_output()
        display(Markdown(r'Funzione in input: $f(t) = t$'))
        display(Markdown(r'Trasformata di Laplace:'))
        display(Markdown(r'$\mathcal{L}\{F(t)\} = F(s) = \frac{1}{s^2}$'))
        display(Markdown(r'con $Re(s) > 0$'))

        
def linear_fun():   
    output_plot.clear_output()
    
    plt.figure(figsize=(10,5))
    x_axis = np.linspace(0,5, num=1000)
    plt.plot(x_axis, x_axis, 'b-')
    plt.xlim(0, 5)
    plt.ylim(0, 5)
    plt.grid(True)
    plt.legend(handles=[blue_patch])
    plt.xlabel('t [s]')
    plt.ylabel('f(t)')
    plt.xticks(range(0,5,1))
    plt.title(r'Grafico della funzione $f(t) = t$')
    plt.show()
    
    with output_panel:
        output_panel.clear_output()
        display(Markdown(r'Funzione in input: $f(t) = t$'))
        display(Markdown(r'Trasformata di Laplace:'))
        display(Markdown(r'$\mathcal{L}\{F(t)\} = F(s) = \frac{1}{s^2}$'))
        display(Markdown(r'con $Re(s) > 0$'))

    
def constant_fun(c=1):
    output_plot.clear_output()
    display(c_slider)
    
    plt.figure(figsize=(10,5))
    x_axis = np.linspace(0,5, num=1000)
    plt.plot(x_axis, [c for t in x_axis], 'b-')
    plt.xlim(0, 5)
    plt.ylim(-1, 5)
    plt.grid(True)
    plt.legend(handles=[blue_patch])
    plt.xlabel('t [s]')
    plt.ylabel('f(t)')
    plt.xticks(range(0,5,1))
    plt.title(r'Grafico della funzione $f(t) = c$')
    plt.show()
        
    with output_panel:
        output_panel.clear_output(wait=True)
        display(Markdown(r'Funzione in input: $f(t) = c$'))
        display(Markdown(r'Trasformata di Laplace:'))
        display(Markdown(r'$\mathcal{L}\{F(t)\} = F(s) = \frac{c}{s}$'))
        display(Markdown(r'con $Re(s) > 0$'))
        display(Markdown(r'Per c = '+str(c_slider.value)+r': $F(s) = \frac{'+str(c_slider.value)+r'}{s}$'))
  
    
def potential_fun(n=1):
    output_plot.clear_output()
    display(n_slider)
    
    plt.figure(figsize=(10,5))
    x_axis = np.linspace(0,5, num=1000)
    plt.plot(x_axis, [x**n for x in x_axis], 'b-')
    plt.xlim(0, 5)
    plt.ylim(0, 20)
    plt.grid(True)
    plt.legend(handles=[blue_patch])
    plt.xlabel('t [s]')
    plt.ylabel('f(t)')
    plt.xticks(range(0,5,1))
    plt.title(r'Grafico della funzione $f(t) = t^n$')
    plt.show()
        
    with output_panel:
        output_panel.clear_output()
        display(Markdown(r'Funzione in input: $f(t) = t^n$'))
        display(Markdown(r'Trasformata di Laplace:'))
        display(Markdown(r'$\mathcal{L}\{F(t)\} = F(s) = \frac{n!}{s^{n+1}}$'))
        display(Markdown(r'con $Re(s) > 0$'))
        display(Markdown(r'Per n = '+str(n_slider.value)+r': $F(s)= \frac{'+str(math.factorial(n_slider.value))+r'}{s^'+str(n_slider.value+1)+r'}$'))

        
def exponential_fun(k):
    output_plot.clear_output()
    display(k_slider_exp)
    
    plt.figure(figsize=(10,5))
    x_axis = np.linspace(0,5, num=1000)
    plt.plot(x_axis, [np.e**(k*x) for x in x_axis], 'b-')
    plt.xlim(0, 4)
    plt.ylim(0, 20)
    plt.grid(True)
    plt.legend(handles=[blue_patch])
    plt.xlabel('t [s]')
    plt.ylabel('f(t)')
    plt.xticks(range(0,5,1))
    plt.title(r'Grafico della funzione $f(t) = e^{kt}$')
    plt.show()
        
    with output_panel:
        output_panel.clear_output()
        display(Markdown(r'Funzione in input: $f(t) = e^{kt}$'))
        display(Markdown(r'Trasformata di Laplace:'))
        display(Markdown(r'$\mathcal{L}\{F(t)\} = F(s) = \frac{1}{s-k}$'))
        display(Markdown(r'con $Re(s) > Re(k)$'))
        display(Markdown(r'Per k = '+str(k_slider_exp.value)+r': $F(s)= \frac{1}{s-'+str(k_slider_exp.value)+r'}$'))

        
def sin_fun(a):
    output_plot.clear_output()
    display(a_slider_sin)
    
    plt.figure(figsize=(10,5))
    x_axis = np.linspace(0,5, num=1000)
    plt.plot(x_axis, [np.sin(2*np.pi*a*x)for x in x_axis], 'b-')
    plt.xlim(0, 5)
    plt.ylim(-2, 2)
    plt.grid(True)
    plt.legend(handles=[blue_patch])
    plt.xlabel('t [s]')
    plt.ylabel('f(t)')
    plt.xticks(range(0,5,1))
    plt.title(r'Grafico della funzione $f(t) = sin(at)$')
    plt.show()

    with output_panel:
        output_panel.clear_output()
        display(Markdown(r'Funzione in input: $f(t) = sin(at)$'))
        display(Markdown(r'Trasformata di Laplace:'))
        display(Markdown(r'$\mathcal{L}\{F(t)\} = F(s) = \frac{a}{s^2 + a^2}$'))
        display(Markdown(r'con $Re(s) > 0$'))
        display(Markdown(r'Per a = '+str(a_slider_sin.value)+r': $F(s)= \frac{'+str(a_slider_sin.value)+r'}{s^2+'+str(a_slider_sin.value**2)+r'}$'))
    
    
def cos_fun(a):
    output_plot.clear_output()
    display(a_slider_cos)
    
    plt.figure(figsize=(10,5))
    x_axis = np.linspace(0,5, num=1000)
    plt.plot(x_axis, [np.cos(2*np.pi*a*x)for x in x_axis], 'b-')
    plt.xlim(0, 5)
    plt.ylim(-2, 2)
    plt.grid(True)
    plt.legend(handles=[blue_patch])
    plt.xlabel('t [s]')
    plt.ylabel('f(t)')
    plt.xticks(range(0,5,1))
    plt.title(r'Grafico della funzione $f(t) = cos(at)$')
    plt.show()

    with output_panel:
        output_panel.clear_output()
        display(Markdown(r'Funzione in input: $f(t) = cos(at)$'))
        display(Markdown(r'Trasformata di Laplace:'))
        display(Markdown(r'$\mathcal{L}\{F(t)\} = F(s) = \frac{s}{s^2 + a^2}$'))
        display(Markdown(r'con $Re(s) > 0$'))
        display(Markdown(r'Per a = '+str(a_slider_cos.value)+r': $F(s)= \frac{s}{s^2+'+str(a_slider_cos.value**2)+r'}$'))    

        
def exp_sin_fun(a, k):
    output_plot.clear_output()
    display(a_slider_expsin, k_slider_expsin)
    
    plt.figure(figsize=(10,5))
    x_axis = np.linspace(0,5, num=1000)
    plt.plot(x_axis, [np.e**(-k*x)* np.sin(2*np.pi*a*x) for x in x_axis], 'b-')
    plt.xlim(0, 5)
    plt.ylim(-2, 2)
    plt.grid(True)
    plt.legend(handles=[blue_patch])
    plt.xlabel('t [s]')
    plt.ylabel('f(t)')
    plt.xticks(range(0,5,1))
    plt.title(r'Grafico della funzione $f(t) = e^{-kt} sin(at)$')
    plt.show()
    
    with output_panel:
        output_panel.clear_output()
        display(Markdown(r'Funzione in input: $f(t) = e^{-kt} sin(at)$'))
        display(Markdown(r'Trasformata di Laplace:'))
        display(Markdown(r'$\mathcal{L}\{F(t)\} = F(s) = \frac{a}{(s+k)^2 + a^2}$'))
        display(Markdown(r'con $Re(s) > -k$'))
        display(Markdown(r'Per a, k = '+str(a_slider_expsin.value)+', '+str(k_slider_expsin.value)+r': $F(s)= \frac{'+str(a_slider_expsin.value)+r'}{(s+'+str(k_slider_expsin.value)+r')^2+'+str(a_slider_expsin.value**2)+r'}$'))
        
        
def exp_cos_fun(a, k):
    output_plot.clear_output()
    display(a_slider_expcos, k_slider_expcos)
    
    plt.figure(figsize=(10,5))
    x_axis = np.linspace(0,5, num=1000)
    plt.plot(x_axis, [np.e**(-k*x)* np.cos(2*np.pi*a*x) for x in x_axis], 'b-')
    plt.xlim(0, 5)
    plt.ylim(-2, 2)
    plt.grid(True)
    plt.legend(handles=[blue_patch])
    plt.xlabel('t [s]')
    plt.ylabel('f(t)')
    plt.xticks(range(0,5,1))
    plt.title(r'Grafico della funzione $f(t) = e^{-kt} cos(at)$')
    plt.show()

    with output_panel:
        output_panel.clear_output()
        display(Markdown(r'Funzione in input: $f(t) = e^{-kt} cos(at)$'))
        display(Markdown(r'Trasformata di Laplace:'))
        display(Markdown(r'$\mathcal{L}\{F(t)\} = F(s) = \frac{s+k}{(s+k)^2 + a^2}$'))
        display(Markdown(r'con $Re(s) > -k$'))
        display(Markdown(r'Per a, k = '+str(a_slider_expcos.value)+', '+str(k_slider_expcos.value)+r': $F(s)= \frac{s+'+str(k_slider_expcos.value)+r'}{(s+'+str(k_slider_expcos.value)+r')^2+'+str(a_slider_expcos.value**2)+r'}$'))    
        
        
# slider observers        
def slider_change(change, sender):
    if sender == 'constant':
        constant_fun(change['new'])
    if sender == 'potential':
        potential_fun(change['new'])
    if sender == 'exponential':
        exponential_fun(change['new'])
    if sender == 'sin':
        sin_fun(change['new'])
    if sender == 'cos':
        cos_fun(change['new'])
    if sender == 'expsin_a':
        exp_sin_fun(change['new'], k_slider_expsin.value)
    if sender == 'expsin_k':
        exp_sin_fun(a_slider_expsin.value, change['new'])
    if sender == 'expcos_a':
        exp_cos_fun(change['new'], k_slider_expcos.value)
    if sender == 'expcos_k':
        exp_cos_fun(a_slider_expcos.value, change['new'])

c_slider.observe(lambda change: slider_change(change, 'constant'), names='value')
n_slider.observe(lambda change: slider_change(change, 'potential'), names='value')
k_slider_exp.observe(lambda change: slider_change(change, 'exponential'), names='value')
a_slider_sin.observe(lambda change: slider_change(change, 'sin'), names='value')
a_slider_cos.observe(lambda change: slider_change(change, 'cos'), names='value')
a_slider_expsin.observe(lambda change: slider_change(change, 'expsin_a'), names='value')
k_slider_expsin.observe(lambda change: slider_change(change, 'expsin_k'), names='value')
a_slider_expcos.observe(lambda change: slider_change(change, 'expcos_a'), names='value')
k_slider_expcos.observe(lambda change: slider_change(change, 'expcos_k'), names='value')

    
# dropdown selection handler    
def example_select_handler(change):
    if example_select.value == 'funzione lineare':
        with output_plot:
            linear_fun()
    
    if example_select.value == 'funzione costante':
        with output_plot:
             constant_fun(c_slider.value)
    
    if example_select.value == 'funzione potenza':
        with output_plot:
            potential_fun(n_slider.value)        
                 
    if example_select.value == 'funzione esponenziale':
        with output_plot:
            exponential_fun(k_slider_exp.value)
     
    if example_select.value == 'sinusoide':
        with output_plot:
            sin_fun(a_slider_sin.value)
           
    if example_select.value =='cosinusoide':
        with output_plot:
            cos_fun(a_slider_cos.value)
    
    if example_select.value == 'sinusoide smorzata':
        with output_plot:
            exp_sin_fun(a_slider_expsin.value, k_slider_expsin.value)
        
    if example_select.value == 'cosinusoide smorzata':
        with output_plot:
            exp_cos_fun(a_slider_expcos.value, k_slider_expcos.value)
            
            
example_select.observe(example_select_handler, names='value')
display(example_select, widgets.HBox([output_plot, widgets.Label(" "), output_panel ]))
first()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17084249084249084, 'fraction_numerical': 0.01611721611721612, 'mean_word_length': 3.8219710349699754, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '589281', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 5345, 'n_tokens_neox': 5183, 'n_words': 888, 'path': 'ICCT_it/examples/01/M-09-Trasformata_di_Laplace.ipynb', 'repo_name': 'ICCTerasmus/ICCT', 'size': '13664'}