In: Computer Science
Write a function sin_x_crossings(begin, end, skip) that uses sin math function which takes begin, end and skip as input parameters, and returns the x-points as a Python list that crosses the x-axis (i.e., x-value that just before it crosses).
Hint: see axis-crossing lecture notes for hints.
Note: you cannot use for or while loops.
Note2: numpy has been imported as np
For example:
Test | Result |
---|---|
import math ans = sin_x_crossings(0, 4 * math.pi, 0.01) for val in ans: print(round(val, 2)) |
3.14 6.28 9.42 |
import math ans = sin_x_crossings(0, 2 * math.pi, 0.1) for val in ans: print(round(val, 2)) |
3.1 |
Python code:
# importing the library as np
import numpy as np
# Defining the function sin_x_crossing
def sin_x_crossing(begin , end , skip):
# We can see value of x which crosses just before the x axis are
multiple of pi
# hence we can get the ans directly
start = int(begin / np.pi)
end = int(end / np.pi)
x = np.linspace(start + 1 , end - 1 , end - start - 1) *
np.pi
return x
Sample output:
import math
ans = sin_x_crossing(0 , 4 * math.pi , 0.01)
for val in ans:
print(round(val , 2))
import math
ans = sin_x_crossing(0 , 2 * math.pi , 0.1)
for val in ans:
print(round(val , 1))