classdef RK4
    %This class makes use of Runge-Kutta method to numericlly solve an
    %arbitraray set of first order linear differential equation.
    
    properties
        
       Dt  %sub-interval step
        T   %Stop Time
        f   %function handle for derivative calcualtions
        X   %output
        X0  %intial conditions
        Tvec %time points
        
    end
       
    methods
        %This function integrates the differential equation
        function [obj] = calc(obj)
            
            obj.Tvec=(0:obj.Dt:obj.T);
            obj.X(:,1)=obj.X0;
            for ii=1:length(obj.Tvec)-1
               X_cur=obj.X(:,ii);
               k1=obj.f(X_cur);
               k2=obj.f(X_cur+obj.Dt*k1/2);
               k3=obj.f(X_cur+obj.Dt*k2/2);
               k4=obj.f(X_cur+obj.Dt*k3);
               obj.X(:,ii+1)=X_cur+obj.Dt/6*(k1+2*k2+2*k3+k4);
            end
                
            end
            
            function []=plotFig1(obj)
                
                %plotting function
                figure;
                plot(obj.X(1,:),obj.X(2,:))
                xlabel('X')
                ylabel('Y')
                
            end
            function []=plotFig2(obj)
                
                %plotting function
                figure;
                plot(obj.Tvec,obj.X(1,:))
                xlabel('Time')
                ylabel('Y')
                
            end
            
        end
        
end
