classdef HW4_Solution_ODESolver < handle 
    %HW_Solution_ODESolver Numerically integrates 1st order ODE
    %   Uses Euler or RK4 method
    
     properties
        dt  %sub-interval step
        T   %Stop Time
        f   %function handle
        X   %output
        X0  %intial conditions
        Tvec %time points
    end
    
    methods
        
        function RK4(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 Euler(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);
               obj.X(:,ii+1)=X_cur+obj.dt*k1;
            end
        end
        
        function fh=plotX1(obj,xlabstr,ylabstr)
            fh=figure;
            plot(obj.Tvec,obj.X(1,:))
            xlabel(xlabstr)
            ylabel(ylabstr)
        end

    end
    
end

