Full name: Farhang Forghanparast

R#: 321654987

HEX: 0x132c10cb

Title of the notebook

Date: 9/3/2020

Example

Create the AVERAGE function for three values and test it for these values:

  • 3,4,5
  • 10,100,1000
  • -5,15,5
In [1]:
def AVERAGE3(x,y,z) : #define the function "AVERAGE3"
    Ave = (x+y+z)/3  #computes the average
    return Ave
In [2]:
print(AVERAGE3(3,4,5))
print(AVERAGE3(10,100,1000))
print(AVERAGE3(-5,15,5))
4.0
370.0
5.0

Example

Create the FC function to convert Fahrenhiet to Celsius and test it for these values:

  • 32
  • 15
  • 100

*hint: Formula-(°F − 32) × 5/9 = °C

In [3]:
def FC(x) : #define the function "AVERAGE3"
    C = (x - 32)*5/9
    return C
In [4]:
print(FC(32))
print(FC(15))
print(FC(100))
0.0
-9.444444444444445
37.77777777777778

Exercise 1

Create the function $$f(x) = e^x - 10 cos(x) - 100$$ as a function (i.e. use the def keyword)

def name(parameters) :
    operations on parameters
    ...
    ...
    return (value, or null)

Then apply your function to the value.

Use your function to complete the table below:

x f(x)
0.0
1.50
2.00
2.25
3.0
4.25
In [ ]:
 
In [ ]:
 

Example

Use the plotting script and create a function that draws a straight line between two points.

In [5]:
def Line():
    from matplotlib import pyplot as plt # import the plotting library from matplotlibplt.show()
    x1 = input('Please enter x value for point 1')
    y1 = input('Please enter y value for point 1')
    x2 = input('Please enter x value for point 2')
    y2 = input('Please enter y value for point 2')
    xlist = [x1,x2]
    ylist = [y1,y2]
    plt.plot( xlist, ylist, color ='orange', marker ='*', linestyle ='solid') 
    #plt.title(strtitle)# add a title
    plt.ylabel("Y-axis")# add a label to the x and y-axes
    plt.xlabel("X-axis")
    plt.show() # display the plot
    return #null return
In [6]:
Line()

Example- Lets have some fun!

Copy the wrapper script for the plotAline() function, and modify the copy to create a plot of $$ x = 16sin^3(t) $$ $$ y = 13cos(t) - 5cos(2t) - 2cos(3t) - cos(4t) $$ for t raging from [0,2$\Pi$] (inclusive).

Label the plot and the plot axes.

In [7]:
from matplotlib import pyplot as plt # import the plotting library from matplotlibplt.show()
import numpy as np # import NumPy: for large, multi-dimensional arrays and matrices, along with  high-level mathematical functions to operate on these arrays.
pi = np.pi #pi value from the np package
t= np.linspace(0,2*pi,360)# the NumPy function np.linspace is similar to the range()

x = 16*np.sin(t)**3
y = 13*np.cos(t) - 5*np.cos(2*t) - 2*np.cos(3*t) - np.cos(4*t)

plt.plot( x, y, color ='purple', marker ='.', linestyle ='solid') 
plt.ylabel("Y-axis")# add a label to the x and y-axes
plt.xlabel("X-axis")
plt.axis('equal') #sets equal axis ratios
plt.title("A Hopeless Romantic's Curve")# add a title
plt.show() # display the plot

Exercise 2

Copy the wrapper script for the plotAline() function, and modify the copy to create a plot of $$ y = x^2 $$ for x raging from 0 to 9 (inclusive) in steps of 1.

Label the plot and the plot axes.

In [ ]:
 

Exercise 3

Use your function from Exercise 1.

$$f(x) = e^x - 10 cos(x) - 100$$

And make a plot where $x$ ranges from 0 to 15 in increments of 0.25. Label the plot and the plot axes.

In [ ]:
 
In [ ]: