package ejemplos;

/**
 * Title: Integracion numerica, metodos de Runge-Kutta
 * Description: Solucion de un circuito RL utilizando el metodo de EULER
 * Copyright: Copyright (c) 2003
 * Company: UMSNH
 * author Dr. Felix Calderon Solorio
 * version 1.0
 */


public class ej014 {
  public static void main(String[] args)
  {
    Runge_Kutta2();
    Runge_Kutta3();
    Runge_Kutta4();
  }

  // SE DECLARA LA FUNCION
 public static double di(double i, double t)
 {
   double y;
   double R = 2, L=1, V=5;
   y = (V - R*i)/L;
   return y;
 }

 public static double Exacta(double i)
 {
   double y;
   double R = 2, L=1, V=5;
   y = V*(1-Math.exp(-R*i/L))/R;
   return y;
 }

  public static void Runge_Kutta2()
  {
    double t[] = new double[200]; // tiempo
    double i[] = new double[200]; // corriente
    double ix[] = new double[200]; // corriente

    double k1, k2;

    t[0] = 0; i[0] = 0;

    double Delta =(double) 0.1;  // incremento

    // desde 0 hasta 2
    for(int k=0;k<199;k++)
    {
      k1 = di(i[k], t[k]) * Delta;
      k2 = di(i[k]+k1, t[k]+Delta) * Delta;
      i[k+1] = i[k] + (k1 + k2)/2.0;
      t[k+1] = t[k] + Delta;
    }

    for(int k=0;k<200;k++)
      ix[k] = Exacta(t[k]);

    // LLAMA AL METODO DE GRAFICACION
    grafica g=new grafica("ciruito RL con Runge-Kutta2");
    g.SerieX(t);
    g.SerieY(i);
    g.SerieY(ix);
    g.show();
  }

  public static void Runge_Kutta3()
  {
    double t[] = new double[200]; // tiempo
    double i[] = new double[200]; // corriente
    double ix[] = new double[200]; // corriente

    double k1, k2, k3;

    t[0] = 0; i[0] = 0;

    double Delta =(double) 0.1;  // incremento

    // desde 0 hasta 2
    for(int k=0;k<199;k++)
    {
      k1 = di(i[k], t[k]) * Delta;
      k2 = di(i[k] + k1/2, t[k]+Delta/2) * Delta;
      k3 = di(i[k] - k1 + 2*k2, t[k]+Delta) * Delta;
      i[k+1] = i[k] + (k1 + 4*k2 + k3)/6.0;
      t[k+1] = t[k] + Delta;
    }

    for(int k=0;k<200;k++)
      ix[k] = Exacta(t[k]);

    // LLAMA AL METODO DE GRAFICACION
    grafica g=new grafica("ciruito RL con Runge-Kutta3");
    g.SerieX(t);
    g.SerieY(i);
    g.SerieY(ix);
    g.show();
  }
  public static void Runge_Kutta4()
  {
    double t[] = new double[200]; // tiempo
    double i[] = new double[200]; // corriente
    double ix[] = new double[200]; // corriente

    double k1, k2, k3, k4;

    t[0] = 0; i[0] = 0;

    double Delta =(double) 0.1;  // incremento

    // desde 0 hasta 2
    for(int k=0;k<199;k++)
    {
      k1 = di(i[k]        , t[k]        ) * Delta;
      k2 = di(i[k] + k1/2 , t[k]+Delta/2) * Delta;
      k3 = di(i[k] + k2/2 , t[k]+Delta/2) * Delta;
      k4 = di(i[k] + k3   , t[k]+Delta  ) * Delta;
      i[k+1] = i[k] + (k1 + 2*k2 + 2*k3 + k4)/6.0;
      t[k+1] = t[k] + Delta;
    }

    for(int k=0;k<200;k++)
      ix[k] = Exacta(t[k]);

    // LLAMA AL METODO DE GRAFICACION
    grafica g=new grafica("ciruito RL con Runge-Kutta4");
    g.SerieX(t);
    g.SerieY(i);
    g.SerieY(ix);
    g.show();
  }
}