Create the AVERAGE function for three values and test it for these values:
def AVERAGE3(x,y,z) : #define the function "AVERAGE3"
Ave = (x+y+z)/3 #computes the average
return Ave
print(AVERAGE3(3,4,5))
print(AVERAGE3(10,100,1000))
print(AVERAGE3(-5,15,5))
Create the FC function to convert Fahrenhiet to Celsius and test it for these values:
*hint: Formula-(°F − 32) × 5/9 = °C
def FC(x) : #define the function "AVERAGE3"
C = (x - 32)*5/9
return C
print(FC(32))
print(FC(15))
print(FC(100))
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 |
Use the plotting script and create a function that draws a straight line between two points.
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
Line()
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.
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
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.
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.