Sage Notebook "plotting cycloid"
draw several functions and control color and line thickness

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"

import NumPy functions

We use np.arange to generate points where the position is the reflector is calculated and plotted

In [1]:
import numpy as np

position of the reflectors

is provided here as a sum of two vectors $\mathbf M$ and $\mathbf D$ that depend on $\theta$, $\phi$ and $d$

We fist declare these variables...

In [2]:
theta, phi, d = var("theta,phi,d")

...and specify how $\mathbf M$ and $\mathbf D$ depend on $\theta$, $\phi$ and $d$

In [3]:
M =      vector([-theta, 1])
D =  d * vector([-sin(theta+phi), cos(theta+phi)])

you can change this definition to plot other trajectories!

Function definions

calculate the cycloid for the given parameters, and plot the path

In [4]:
def Cycloid( _d, phi_pi, thetaMax, *args, **kwargs ):
    """"
    this function makes a plot of a cycloid
    determined by the parameters  _d, phi_pi, thetaMax,  that are provided by the function arguments
    The optional arguments in  ars  and  kwargs  will be passed to the 'plot' function.
    This allows us to specify colors, line thickness, and other plot parameters
    """

    # size of the plot window
    if (_d<1) :
        _yMin = 0.
        _yMax = 2.
    else :
        _yMin = 1-_d
        _yMax = 1+_d
   

    ## parameter dictionary with paramters that specify the properties of the plot
    plot_params = dict(xmin = -1,    xmax = 10,         # interval on x-Achse
                       ymin = _yMin,    ymax = _yMax,   #             y-Achse
                       ticks=(2,1),                     # tick intervals along x and y
                       axes_labels=(r"$x$",""),         # axis labels

                       thickness=4,                     # line width of functions
                       fontsize=16,                     # text size

                       aspect_ratio=1,                  # same scale for x- and y-axis
                       figsize=6,                       # plot size
                      )

    
    resolution = 50.
    thetaValues = -np.arange(thetaMax*resolution)/resolution

    #  lines for trail and the height of the axis
    plt  = plot( 0, color="black", alpha=0.1, **plot_params )
    plt += plot( 1, color="gray" , alpha=0.1, **plot_params )

    # add cycloide -- taking into account command line options for plot parameters
    plot_params.update(**kwargs     # parameter provided in function call
                      )

    path = [(M+D).subs([d==_d, phi==phi_pi*pi, theta==_theta]) \
                     for _theta in thetaValues]
    plt += line( path, **plot_params )

    # this function returns a plot
    return plt
In [5]:
Cycloid( 0.8, 0., 4*pi, color="blue",  thickness=3 )
Out[5]:

make and display the plot

In [6]:
# initial condition for the spoke
phi_pi   = 0.0

# range of  theta-values
thetaMax = 3*pi+1

# Cycloid  provides plot for given parameters
cyclo  = Cycloid( 1.0, phi_pi, thetaMax, color="red",   thickness=4 )

# ... and we can also add more lines with other parameters and line properties
cyclo += Cycloid( 0.8, phi_pi, thetaMax, color="blue",  thickness=3 )
cyclo += Cycloid( 0.6, phi_pi, thetaMax, color="green", thickness=2 )
cyclo += Cycloid( 0.4, phi_pi, thetaMax, color="gray",  thickness=1.5 )
cyclo += Cycloid( 0.2, phi_pi, thetaMax, color="yellow"  )

# show figure
show(cyclo, frame=True)

save the plot

In [9]:
# as  svg-file
cyclo.save_image('p02_plot-cycliods.svg')

# as  PDF-file
# cyclo.save_image('p02_plot-cycliods.pdf')

# as  png-file with transparent background
cyclo.save_image('p02_plot-cycliods.png', transparent=True, frame=True)

watch out: the saved plot looks different from the one shown above when you do not provide the option "frame=True".
After all this option was given to "show". It is not stored in the image.
The difference can be checked by comparing the 'svg' and the 'png' image generated by this notebook.

In [ ]: