package comp599.a1.numerical;



/**
 * TODO: finish this class!
 * @author kry
 */
public class Midpoint implements Integrator {
    
    @Override
    public String getName() {
        return "Midpoint";
    }
    
    //constructor method for ForwardEuler
    public Midpoint(boolean subs){
    	subSteps=subs;
    	
    }
    private boolean subSteps;
    
    @Override
    /** 
     * Advances the system at t by h 
     * @param y The state at time h
     * @param n The dimension of the state (i.e., y.length)
     * @param t The current time (in case the derivs function is time dependent)
     * @param h The step size
     * @param yout  The state of the system at time t+h
     * @param derivs The object which computes the derivative of the system state
     */
    public void step(double[] y, int n, double t, double h, double[] yout, Function derivs) {
        // TODO Auto-generated method stub
    	double[] dydt=new double[y.length];  
    	double[] temp= new double[y.length];
    	derivs.derivs(t, y, dydt);
    	//compute the midpoint
    	for(int i=0; i<dydt.length; i++){
    		dydt[i]=dydt[i]*h/2;
    		yout[i]=y[i]+dydt[i];
    	}

    	//now we want to compute dydt given yout.
    	dydt=new double[y.length];
    	derivs.derivs(t, yout, dydt);
    	for(int i=0 ; i<dydt.length;i++){
    		dydt[i]=dydt[i]*h;
    		yout[i]=y[i]+dydt[i];
    	}
    	if(this.subSteps==true){
        	derivs.derivs(t, yout, dydt);
        	//compute the midpoint
        	for(int i=0; i<dydt.length; i++){
        		dydt[i]=dydt[i]*h/2;
        		temp[i]=yout[i]+dydt[i];
        	}

        	//now we want to compute dydt given yout.
        	dydt=new double[y.length];
        	derivs.derivs(t, temp, dydt);
        	for(int i=0 ; i<dydt.length;i++){
        		dydt[i]=dydt[i]*h;
        		yout[i]=yout[i]+dydt[i];
        	}
    	}
    	
    	
    }






}