Facebook
From Melodic Bat, 3 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 55
  1. ##Create a function that will perform the following calculation on a given input value:  ?=5sin(?−6?12) . (The input value here is  ? , and  ?=3.14159...  is a constant.)
  2. import math  
  3. from sympy.abc import x
  4.  
  5. def sin_f(x):
  6.     y =  5 * math.sin((x-6*math.pi)/12)
  7.     return y
  8.  
  9. print(sin_f(math.radians(95)))
  10.  
  11. ##Complete the loop below to create a list named "x_list" that contains values ranging from 0 to 100, incremented by 0.25 (i.e.; 0, 0.25, 0.5, ... 99.5, 99.75, 100). Print out the list after creating it.
  12. x_list = []
  13.  
  14. maxCount = 100
  15. count = 0
  16. while(count < maxCount):
  17.     x_list.append(count)
  18.     count = count + 0.25
  19.    
  20. print(x_list)
  21.  
  22. ##Now create "y_list", which contains the value  ?  as defined in your function, given the  ?  values from "x_list". Then, print the first value of "y_list".
  23. y_list = []
  24. i = 0
  25. for i in range(len(x_list)):
  26.    
  27.     y = 5 * math.sin((x-6*math.pi)/12)
  28.     y_list.append(y)
  29.     i = i + 1
  30.    
  31. print(y_list[1])
  32.  
  33. ##Run the cell below to plot "x_list" against "y_list". Don't worry about understanding what that code is doing just yet. We will get to plotting later.
  34. %pylab inline
  35. import matplotlib.pyplot as pl
  36.  
  37. pl.plot(x_list,y_list)
  38. pl.xlabel('x')
  39. pl.ylabel('y')
  40. pl.show()
  41.  
  42. ##Create a new function called "y_from_list" that takes in an entire list of  ?  values as input, and then calculates all of the corresponding  ?  values and returns a list of those  ?  values.
  43. def y_from_list(input_list):
  44.     output_list = []
  45.     for j in range(len(input_list)):
  46.         output_list.append(y_from_list)
  47.     return output_list
  48.  
  49. ##If the function you just wrote is working properly, it should run the following cell:
  50.   y_list2 = y_from_list(x_list)
  51.  
  52. pl.plot(x_list,y_list,ls=':',linewidth=3,label='y_list')
  53. pl.plot(x_list,y_list2,linewidth=1,label='y_list2')
  54. pl.xlabel('x')
  55. pl.ylabel('y')
  56. pl.legend()
  57. pl.show()