Sage Notebook "varying parameters of a trigonometric functions"

lecture Theoretical Mechanics IPSP

Universität Leipzig, winter term 2020

Author: Jürgen Vollmer (2020)

lizence: Creative Commons Attribution-ShareAlike 4.0 International CC BY-SA 4.0

Sage is an OpenSource Project that implements a broach range of computer-based mathematics and computer algebra in a Jupyter notebook.

Documentation and information on the installation of Sage can be found at https://sagemath.org

A very nice introduction to working with Sage is given in the book Paul Zimmermann, u.a.: "Computational Mathematics with SageMath"

In [1]:
# declare plot variable
t=var("t")
In [2]:
## default plot parameters
#  -----------------------
#  here they are stored in a dictionary

plot_param = dict(  axes_labels = (r"$t$", r"$y_0 + A \: \cos( 2\pi \, (t-t_0)/T)$"),
                    fontsize    = 14,          # size of text font
                    figsize     = (6,4),       # figure size
                    frame       = True
                 )
In [3]:
## interactive plot where parameters are varied by slides
#  ------------------------------------------------------

#  announce that this will become an interactive plot
@interact

#  define what should be plotted, based on the provided parameters
def _( xRange = input_grid(1, 2, default = [[-1,2]], label = 'x interval'), 
       yRange = input_grid(1, 2, default = [[-1,2]], label = 'y interval'), 
       y_0=slider(-3  , 3, step_size = 0.1, default=0.5 ),
       A  =slider(-2  , 2, step_size = 0.1, default=1 ),
       t_0=slider( 0  , 2, step_size = 0.1, default=0 ),
       T  =slider( 0.5, 5, step_size = 0.5, default=1 ),
       auto_update=False
     ) :
    """ 
    provide the parameters as arguments to the function
    text input fields.
      xRange, yRange:  ranges for the plot
    sliders.
      offset:       y_0  takes values in  [-3  , 3]  that can be incremented in steps of size  0.1
      amplitude:    A    takes values in  [-2  , 2]  that can be incremented in steps of size  0.1
      time offset:  t_0  takes values in  [ 0  , 2]  that can be incremented in steps of size  0.2
      period        T    takes values in  [ 0.5, 5]  that can be incremented in steps of size  0.5
    """

    # update plot parameter values in dictionary
    plot_param["xmin"] = xRange[0][0]
    plot_param["xmax"] = xRange[0][1]
    plot_param["ymin"] = yRange[0][0]
    plot_param["ymax"] = yRange[0][1]

    #  make plot and store it in the variable  "plt"
    #  the dictionary of plot parameters are provided as argument "**plot_param"
    plt = plot( A*cos(2*pi*(t-t_0)/T)+y_0, **plot_param )

    # show the plot
    show( plt )
Widget Javascript not detected.  It may not be installed or enabled properly.
In [ ]: