""" Ex 7.3 from "A primer on..." Extend the parabola class with a term involving a sine function and two additional parameters. """ import numpy as np class Line: def __init__(self, c0, c1): self.c0, self.c1 = c0, 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 += f'{x:12g} {y:12g}\n' return s class Parabola(Line): def __init__(self, c0, c1, c2): super().__init__(c0, c1) # Line stores c0, c1 self.c2 = c2 def __call__(self, x): return super().__call__(x) + self.c2*x**2 class SinPlusQuadratic(Parabola): def __init__(self,A,w,a,b,c): super().__init__(c,b,a) #order is opposite of Parabola class, as specified in the question text self.A = A self.w = w def __call__(self, x): return super().__call__(x) + self.A*np.sin(self.w*x) s1 = SinPlusQuadratic(1,1,1,1,1) x = np.pi print(s1(x)) print(s1.table(-np.pi,np.pi,11)) """ Terminal> python sin_plus_quadratic.py 14.011197054679151 -3.14159 7.72801 -2.51327 4.21549 -1.88496 1.71705 -1.25664 0.371443 -0.628319 0.17868 0 1 0.628319 2.61089 1.25664 4.78683 1.88496 7.38907 2.51327 10.4176 3.14159 14.0112 """