Download (right-click, save target as ...) this page as a jupyterlab notebook from: Lab6-HW


Exercise Set 6: FUN with functions

LAST NAME, FIRST NAME

R00000000

ENGR 1330 ES6 - Homework

In [1]:
# Preamble script block to identify host, user, and kernel
import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)
atomickitty
sensei
/opt/jupyterhub/bin/python3
3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0]
sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)

Exercise 1

Make a function that cubes its input:

$$ f(x) = x^3 $$

and test it for the following values of x:

  • -1
  • 0.0
  • 1.0
  • 2.0
  • 3.0

Exercise 2

Generate two lists:

  1. x ranging from 0 to 9 in steps of 1
  2. f(x) from your function in Exercise 1

Use the plotAline() function (below) to create a plot of

$$ y = x^3 $$

for x raging from 0 to 9 (inclusive) in steps of 1.

Label the plot and the plot axes.

A wrapper script is included below that needs completion to work and draw the plot.

In [1]:
def plotAline(list1,list2,strx,stry,strtitle): # plot list1 on x, list2 on y, xlabel, ylabel, title
    import matplotlib.pyplot # import the plotting library from matplotlib.pyplot
    matplotlib.pyplot.plot( list1, list2, color ='green', marker ='o', linestyle ='solid') # create a line chart, years on x-axis, gdp on y-axis
    matplotlib.pyplot.title(strtitle)# add a title
    matplotlib.pyplot.ylabel(stry)# add a label to the x and y-axes
    matplotlib.pyplot.xlabel(strx)
    matplotlib.pyplot.show() # display the plot
    return #null return
In [3]:
# wrapper script
x = []  # define two lists 
y = []
# populate x and y for plotting
# ...
# ...
# then plot
plotAline(x,y,"x"," x^3","Plot of y=x^3")

Exercise 3

Modify the wrapper script above to create a plot of the parametric functions x(t) and y(t)

$$ x(t) = 16sin^3(t) $$$$ y(t) = 13cos(t) - 5cos(2t) - 2cos(3t) - cos(4t) $$

for t raging from [0,2$\Pi$] (inclusive).

Label the plot and the plot axes.

In [ ]: