#exer 9.3 (and partly 9.1) import numpy as np import matplotlib.pyplot as plt class Line: def __init__(self, c0, c1): self.c0 = c0 self.c1 = c1 def __call__(self, x): return self.c0 + self.c1*x def table(self, L, R, n): """Return a table with n points for L <= x <= R.""" s = '' for x in np.linspace(L, R, n): y = self(x) s += '%12g %12g\n' % (x, y) return s class Parabola(Line): def __init__(self,c0,c1,c2): Line.__init__(self,c0,c1) self.c2 = c2 def __call__(self,x): return self.c0+self.c1*x+self.c2*x**2 class SinPlusQuad(Parabola): def __init__(self,A,w,a,b,c): Parabola.__init__(self,c,b,a) self.A = A self.w = w def __call__(self,x): return Parabola.__call__(self,x)+self.A*np.sin(self.w*x) #Example usage: #Create an instance of the class spq = SinPlusQuad(1,10,1,0.5,0.0) #compute and plot the function x = np.linspace(-5,5,1000) y = spq(x) plt.plot(x,y) plt.show()