Problem

Consider the problem of projectile motion in purely vertical direction that we solved in class. Here is the class we defined for the projectile,

class ProjLocY():  
    def __init__(self, velInitY, g = 9.8):  
        self.velInitY = velInitY # initial velocity along the y direction.  
        self.gravityConstant = g # gravityConstant, 9.81 on earth.  
    def getLocY(self, time):  
        """  
        Return the location of the projectile at the specific given `time` and initial velocity.  
        Input  
            time    :   An input real (float) representing the time  
                        past since the start of the projectile motion.  
        """  
        return self.velInitY * time - 0.5 * self.gravityConstant * time**2  
    def eval(self, time): return self.getLocY(time)  
  1. Now, define a similar class for projectile motion in the x direction.
    (Note: Recall the projectile motion along the x direction does not involve acceleration.)

  2. Create example objects from both classes and pass them to the following deafferentation function to compute the velocity in the x and y directions after one second (with initial velocity of 10 meters / sec in both X and Y directions.
    def getDiff(func, t, deltaT = 1.e-5):  
        """  
        Return the derivative of the function contained with the input `func` object.  
        The `func` object must contain a method that returns the value of the function  
        """  
        return (func.eval(t + deltaT) - func.eval(t)) / deltaT # finite differencing  
    

    Here is the final result for cross-validating your answer,

    px = ProjLocX(10)  
    py = ProjLocY(10)  
    print(getDiff(px, t = 1), getDiff(py, t = 1))  
    
    9.999999999976694 0.19995099993508345  
    
  3. Define a subclass that inherits the properties of the projectiles in x and y directions from the corresponding super (parent) classes (ProjLocX and ProjLocY).
    To do so, you need to define the new subclass via multiple inheritance.

  4. In addition to the inherited methods, this new subclass must contain a eval(time) method which computes and returns the location (x,y) of the projectile by calling and aggregating information from the corresponding methods of the parent classes getLocX(time) and getLocY(time).
  5. Finally, add a new method getSpeed(time) which computes the velocity of the projectile at any given moment of time. (Hint: Within this method you need to compute the velocities along each direction using the differentiation function above and then combine them to return the projectile speed at the given time.)
    Compute and return the speed of the projectile in X-Y plane for initial velocities (10, 10) meters / sec at time 1 sec since the start of the projectile.
    Here is the final result for cross-validating your answers,
    p = ProjLoc(10, 10)  
    print("Speed at time = {} is {}".format(1, np.linalg.norm(getDiff(p, t = 1))))  
    
    Speed at time = 1 is 10.00199882033131
    

Comments