Please code in sage or python
a. Execute this example of a function definition: def h(x): return sum ([sin (n*x)/n for n in srange (1, 20.0)]) b. Evaluate h(x), and plot h(x) for x between 0 and 4*pi. c. Define a function that is equal to 2* x for x > 0, and equal to -3*x for x
Expert Answer
from math import sin, pi
import matplotlib.pyplot as plt
def h(x):
return sum([sin(n*x)/n for n in srange(1, 20.0)])
def frange(start, stop, step):
res = []
x = start
while x < stop:
res.append(x)
x += step
return res
x = srange(0, 4*pi)
y = [h(i) for i in x]
plt.plot(x, y)
def myfunc(x):
if x > 0:
return 2*x
else:
return -3*x
x = frange(-3, 3, 0.1)
y = [myfunc(i) for i in x]
plt.plot(x, y)
# code link: https://paste.ee/p/cy6ay (please add your statement for importing srange or use my frange method)