classdef dampedOscillator < mySystem.simpleOscillator
    %DAMPEDOSCILLATOR A class that models an underdamped harmonic oscillator
    %   This class inherits from the SIMPLEOSCILLATOR class and models a
    %   one-dimensional underdamped harmonic oscillator. It has one
    %   additional property for the damping constant. Damping is assumed to
    %   be small and only first-order effects are included. The PHASE and
    %   the DYNAMICS methods are overloaded to include the effects of
    %   damping. Other methods are inherited from the base class. There is
    %   an additional helper method DAMPINGPARAMETER that calculates the
    %   normalized damping parameter used in the dynamical equation.
    
    % Copyright 2020 The MathWorks, Inc. 
        
    properties (SetAccess = private, GetAccess = protected)
        dampingConstant
    end
    
    methods
        function obj = dampedOscillator(m,b,k)
            obj@mySystem.simpleOscillator(m,k);
            obj.dampingConstant = b;
        end
        
        function gamma = dampingParameter(obj)
            gamma = obj.dampingConstant/(2*obj.mass);
        end
               
        function phi = phase(obj,initialPosition,initialVelocity)
            phi = atan2(obj.angularFrequency*initialPosition,initialVelocity +...
                obj.dampingParameter*initialPosition);
        end
        
        function finalPosition = dynamics(obj,initialPosition,initialVelocity,timeInterval)
            finalPosition = obj.amplitude(initialPosition, initialVelocity)...
                *exp(- obj.dampingParameter*timeInterval)*sin(obj.angularFrequency*timeInterval...
                + obj.phase(initialPosition,initialVelocity));
        end
    end
end

