Skip to main content
added 333 characters in body
Source Link
Dori
  • 41
  • 4
        #include<stdio.h>
        #include<math.h>
        #include<stdbool.h>
        #include  <stdlib.h>

        #define N 3

        double ddopri5(void fcn(double, double *, double *), 
       double *y);
      double alpha;
      void fcn(double t, double *y, double *f);
      double eps;

      int main(void){
     double y[N];
     //eps = 1.e-9;
      printf("Enter epsilon:\n"); 
      scanf("%lg", &eps);
      y[0]=1.0;
      y[1]=12.00;
      y[2]=30.00000567845; 
     ddopri5(fcn, y);
alpha;
     }


    void fcn(double t, double *y, double *f){
     /*  double h = 0.25;*/
     double p,hub;
     p=0;
    hub=y[1]/y[0];
    f[0] = y[1];
    f[1] = -(y[0]*y[2]+3*p*y[0])/6.0;
    f[2] = -3*hub*y[2]-3*hub*p;

    }

      double ddopri5(void fcn(double, double *, double *), double 
    *y){
      double t, h, a, b, tw, chi;
      double w[N], k1[N], k2[N], k3[N], k4[N], k5[N], k6[N], 
     k7[N], err[N], dy[N];;
      int i;
    double errabs;
     int iteration;
     FILE *fpw;
     fpw=fopen("case1.dat", "w");eps;

        int main(void){
          double y[N];
          //eps = 1.e-9;
           printf("Enter epsilon:\n"); 
           scanf("%lg", &eps);
           y[0]=1.0;
           y[1]=12.00;
           y[2]=30.00000567845; 
           ddopri5(fcn, y);

      iteration = 0; }

     //eps = 1.e-9;
     h = 1.0e-6;
     a = 0.0;
     b = 30;
     t = a;
     while(t < b -eps){

     /* printf("%lg\n", eps);*/
     fcn(t, y, k1);
      tw = t+ (1.0/5.0)*h;
      for(i = 0; i < N; i++){
        /*printf("k1[%i] = %.15lf \n", i, k1[i]);*/
        w[i] = y[i] + h*(1.0/5.0)*k1[i];    
       }
      fcn(tw, w, k2);
      tw = t+ (3.0/10.0)*h;
      for(i = 0; i < N; i++){
        /*printf("k2[%i] = %.15lf \n", i, k2[i]);*/
        w[i] = y[i] + h*((3.0/40.0)*k1[i] + (9.0/40.0)*k2[i]);  
      }

  void fcn(tw, w, k3);
    tw = t+ (4.0/5.0)*h;
    for(i = 0; i < N; i++){
        /*printf("k3[%i] = %.15lf \n",double it, k3[i]);*/
        w[i] = y[i] + h*((44.0/45.0)*k1[i] - (56.0/15.0)*k2[i] + 
      (32.0/9.0)*k3[i]); 
    }
    fcn(tw,double w*y, k4);
    tw = t+ (8.0/9.0)*h;
    for(i = 0; i < N;double i++*f){
        /*printf("k4[%i] = %.15lf \n", i, k4[i]);*/
        w[i] = y[i] + h*((19372.0/6561.0)*k1[i] - 
      (25360.0/2187.0)*k2[i] + (64448.0/6561.0)*k3[i] - 
        (212.0/729.0)*k4[i]);   
      }
       fcn(tw,double wp, k5);
       tw = t + h;
      for(i = 0; i < N; i++){hub;
        /*printf("k5[%i] = %.15lf \n", i, k5[i]);*/
        w[i] = y[i] + h*((9017.0/3168.0)*k1[i] - p=0;
     (355.0/33.0)*k2[i] + (46732.0/5247.0)*k3[i] + 
      (49.0/176.0)*k4[i] - (5103.0hub=y[1]/18656.0)*k5[i]) ;   
    }
    fcn(tw, w, k6);y[0];

    tw = t + h;
    for(i = 0; i < N; i++){
        /*printf("k6[%i]f[0] = %.15lf \n", i, k6[i]);*/y[1];
        w[i] = y[i] + h*((35.0/384.0)*k1[i] + 
         (500.0/1113.0)*k3[i] +f[1] (125.0/192.0)*k4[i]= - 
          (2187.0/6784.0y[0]*y[2]+3*p*y[0])*k5[i] + (11.0/846.0)*k6[i]);  0;
       }
       fcn(tw, w,f[2] k7);= -3*hub*y[2]-3*hub*p;

            }

      errabs =double 0;ddopri5(void fcn(double, double *, double *), 
                      double *y){
              double t, h, a, b, tw, chi;
              double w[N], k1[N], k2[N], k3[N], k4[N], k5[N], 
                   k6[N],k7[N], err[N], dy[N];
               int i;
               double errabs;
               int iteration;
               FILE *fpw;
               fpw=fopen("case1.dat", "w");

    for(i           iteration = 0; 
 i < N; i++){
    /*  printf("k7[%i] = %.15lf \n", i, k7[i]);*//eps = 1.e-9;
   /*          dy[i]  h = h*((71.0/576001.0)*k1[i]0e-6;
  - 
          (71.0/16695.0)*k3[i] + (71. a = 0/1920.0)*k4[i]0;
 - 
             (17253.0/339200.0)*k5[i] +b (22.0/525.0)*k6[i]);*/= 30;
        dy[i] = h*((35.0/384.0)*k1[i] + (500.0/1113.0)*k3[i] +  t = a;
          (125.0/192.0)*k4[i] -    while(2187.0/6784.0)*k5[i]t +< b -eps){
                  fcn(11.0/84.0)*k6[i]t, y, k1);
        /*err[i]          tw = t+ h*((711.0/576005.0)*k1[i]*h;
  + 
                for(71.0/16695.0i = 0; i < N; i++)*k3[i]{
                     w[i] = y[i] + h*(711.0/19205.0)*k4[i]*k1[i]; -   
         (17253.0/339200.0)*k5[i] + (22.0/525.0)*k6[i] -       }
                  fcn(1.0/40.0)*k7[i]tw, w, k2)*/;
        err[i]          tw = t+ h*((713.0/5760010.0)*k1[i]*h;
  - (71.0/16695.0)*k3[i] 
          +     for(71.0/1920.0i = 0; i < N; i++)*k4[i]{
 -                     w[i] = y[i] + h*(17253(3.0/33920040.0)*k5[i]*k1[i] + 
            (22.0/525.0)*k6[i] -               (19.0/40.0)*k7[i]*k2[i]);
         
  /*printf("err[%i] = %.15lf \n", i, err[i]);*/
        errabs+=err[i]*err[i];
         }

                fcn(tw, w, k3);
                tw = t+ (4.0/5.0)*h;
                for(i = 0; i < N; i++){
                     w[i] = y[i] + h*((44.0/45.0)*k1[i] - 
                           (56.0/15.0)*k2[i] + 
                          (32.0/9.0)*k3[i]); 
                       }
                 fcn(tw, w, k4);
                 tw = t+ (8.0/9.0)*h;
                 for(i = 0; i < N; i++){
                 w[i] = y[i] + h*((19372.0/6561.0)*k1[i] - 
                         (25360.0/2187.0)*k2[i] + 
                          (64448.0/6561.0)*k3[i] 
                          -(212.0/729.0)*k4[i]);   
                     }
                  fcn(tw, w, k5);
                  tw = t + h;
                  for(i = 0; i < N; i++){
                    w[i] = y[i] + h*((9017.0/3168.0)*k1[i] - 
                           (355.0/33.0)*k2[i] + 
                           (46732.0/5247.0)*k3[i] + 
                           (49.0/176.0)*k4[i] - 
                           (5103.0/18656.0)*k5[i]) ;   
                      }
                 fcn(tw, w, k6);
                 tw = t + h;
                 for(i = 0; i < N; i++){
                   w[i] = y[i] + h*((35.0/384.0)*k1[i] + 
                          (500.0/1113.0)*k3[i] + 
                          (125.0/192.0)*k4[i] - 
                          (2187.0/6784.0)*k5[i] + 
                          (11.0/84.0)*k6[i]);  
                     }
                 fcn(tw, w, k7);
                 errabs = 0;
                 for(i = 0; i < N; i++){
                    dy[i] = h*((35.0/384.0)*k1[i] + 
                            (500.0/1113.0)*k3[i] + 
                            (125.0/192.0)*k4[i] - 
                            (2187.0/6784.0)*k5[i] + 
                             (11.0/84.0)*k6[i]);
     
                       err[i] =  h*((71.0/57600.0)*k1[i]  - 
                               (71.0/16695.0)*k3[i] 
                               + (71.0/1920.0)*k4[i] - 
                               (17253.0/339200.0)*k5[i] + 
                              (22.0/525.0)*k6[i] - 
                              (1.0/40.0)*k7[i]);
                       errabs+=err[i]*err[i];
                 }

         errabs = sqrt(errabs);
         /*  printf("errabs = %.15lf\n", errabs); */
         if( errabs < eps){
        t+= h;
        /*  printf(" FROM IF \t t = %.25lf, \n h  = %.25lf, \n 
         errabs = %.25lf, \n iteration = %i . \n", t, h, errabs, 
        iteration);*/
        for(i = 0; i < N; i++){
            y[i]+=dy[i];            
        }
      } 
      chi=errabs/eps;
      chi = pow(chi, (1.0/6.0));
       if(chi > 10)    chi = 10;
      if(chi < 0.1)   chi = 0.1;
       h*=  0.95/chi;
       if( t + h > b ) h = b - t;      
          /*  for(i = 0; i < 4; i++){
        printf("y[%i] = %.15lf \n", i, y[i]);
        }*/

                errabs = sqrt(errabs);
         
                if( errabs < eps){
                t+= h;
        
                for(i = 0; i < N; i++){
                         y[i]+=dy[i];            
                       }
              } 
             chi=errabs/eps;
             chi = pow(chi, (1.0/6.0));
             if(chi > 10)    chi = 10;
             if(chi < 0.1)   chi = 0.1;
             h*=  0.95/chi;
             if( t + h > b ) h = b - t;      
        
             iteration++;
             fprintf(fpw ,"%.25lf %.25lf %.25lf %.25lf \n", 
                            t,y[0],y[1],y[2]);

   
             if(iteration > 30000) break;
            }

          fclose(fpw);
          return 0;
         }
        #include<stdio.h>
        #include<math.h>
        #include<stdbool.h>
        #include  <stdlib.h>

        #define N 3

       double ddopri5(void fcn(double, double *, double *), 
       double *y);
      double alpha;
      void fcn(double t, double *y, double *f);
      double eps;

      int main(void){
     double y[N];
     //eps = 1.e-9;
      printf("Enter epsilon:\n"); 
      scanf("%lg", &eps);
      y[0]=1.0;
      y[1]=12.00;
      y[2]=30.00000567845; 
     ddopri5(fcn, y);

     }


    void fcn(double t, double *y, double *f){
     /*  double h = 0.25;*/
     double p,hub;
     p=0;
    hub=y[1]/y[0];
    f[0] = y[1];
    f[1] = -(y[0]*y[2]+3*p*y[0])/6.0;
    f[2] = -3*hub*y[2]-3*hub*p;

    }

      double ddopri5(void fcn(double, double *, double *), double 
    *y){
      double t, h, a, b, tw, chi;
      double w[N], k1[N], k2[N], k3[N], k4[N], k5[N], k6[N], 
     k7[N], err[N], dy[N];
      int i;
    double errabs;
     int iteration;
     FILE *fpw;
     fpw=fopen("case1.dat", "w");


      iteration = 0;

     //eps = 1.e-9;
     h = 1.0e-6;
     a = 0.0;
     b = 30;
     t = a;
     while(t < b -eps){

     /* printf("%lg\n", eps);*/
     fcn(t, y, k1);
      tw = t+ (1.0/5.0)*h;
      for(i = 0; i < N; i++){
        /*printf("k1[%i] = %.15lf \n", i, k1[i]);*/
        w[i] = y[i] + h*(1.0/5.0)*k1[i];    
       }
      fcn(tw, w, k2);
      tw = t+ (3.0/10.0)*h;
      for(i = 0; i < N; i++){
        /*printf("k2[%i] = %.15lf \n", i, k2[i]);*/
        w[i] = y[i] + h*((3.0/40.0)*k1[i] + (9.0/40.0)*k2[i]);  
      }

   fcn(tw, w, k3);
    tw = t+ (4.0/5.0)*h;
    for(i = 0; i < N; i++){
        /*printf("k3[%i] = %.15lf \n", i, k3[i]);*/
        w[i] = y[i] + h*((44.0/45.0)*k1[i] - (56.0/15.0)*k2[i] + 
      (32.0/9.0)*k3[i]); 
    }
    fcn(tw, w, k4);
    tw = t+ (8.0/9.0)*h;
    for(i = 0; i < N; i++){
        /*printf("k4[%i] = %.15lf \n", i, k4[i]);*/
        w[i] = y[i] + h*((19372.0/6561.0)*k1[i] - 
      (25360.0/2187.0)*k2[i] + (64448.0/6561.0)*k3[i] - 
        (212.0/729.0)*k4[i]);   
      }
       fcn(tw, w, k5);
       tw = t + h;
      for(i = 0; i < N; i++){
        /*printf("k5[%i] = %.15lf \n", i, k5[i]);*/
        w[i] = y[i] + h*((9017.0/3168.0)*k1[i] - 
     (355.0/33.0)*k2[i] + (46732.0/5247.0)*k3[i] + 
      (49.0/176.0)*k4[i] - (5103.0/18656.0)*k5[i]) ;   
    }
    fcn(tw, w, k6);

    tw = t + h;
    for(i = 0; i < N; i++){
        /*printf("k6[%i] = %.15lf \n", i, k6[i]);*/
        w[i] = y[i] + h*((35.0/384.0)*k1[i] + 
         (500.0/1113.0)*k3[i] + (125.0/192.0)*k4[i] - 
          (2187.0/6784.0)*k5[i] + (11.0/84.0)*k6[i]);  
       }
       fcn(tw, w, k7);


      errabs = 0;

    for(i = 0; i < N; i++){
    /*  printf("k7[%i] = %.15lf \n", i, k7[i]);*/
   /*          dy[i] = h*((71.0/57600.0)*k1[i]  - 
          (71.0/16695.0)*k3[i] + (71.0/1920.0)*k4[i] - 
             (17253.0/339200.0)*k5[i] + (22.0/525.0)*k6[i]);*/
        dy[i] = h*((35.0/384.0)*k1[i] + (500.0/1113.0)*k3[i] + 
          (125.0/192.0)*k4[i] - (2187.0/6784.0)*k5[i] + 
            (11.0/84.0)*k6[i]);
        /*err[i] =  h*((71.0/57600.0)*k1[i]  + 
           (71.0/16695.0)*k3[i] + (71.0/1920.0)*k4[i] - 
         (17253.0/339200.0)*k5[i] + (22.0/525.0)*k6[i] - 
         (1.0/40.0)*k7[i])*/;
        err[i] =  h*((71.0/57600.0)*k1[i]  - (71.0/16695.0)*k3[i] 
          + (71.0/1920.0)*k4[i] - (17253.0/339200.0)*k5[i] + 
            (22.0/525.0)*k6[i] - (1.0/40.0)*k7[i]);
          /*printf("err[%i] = %.15lf \n", i, err[i]);*/
        errabs+=err[i]*err[i];
         }


         errabs = sqrt(errabs);
         /*  printf("errabs = %.15lf\n", errabs); */
         if( errabs < eps){
        t+= h;
        /*  printf(" FROM IF \t t = %.25lf, \n h  = %.25lf, \n 
         errabs = %.25lf, \n iteration = %i . \n", t, h, errabs, 
        iteration);*/
        for(i = 0; i < N; i++){
            y[i]+=dy[i];            
        }
      } 
      chi=errabs/eps;
      chi = pow(chi, (1.0/6.0));
       if(chi > 10)    chi = 10;
      if(chi < 0.1)   chi = 0.1;
       h*=  0.95/chi;
       if( t + h > b ) h = b - t;      
          /*  for(i = 0; i < 4; i++){
        printf("y[%i] = %.15lf \n", i, y[i]);
        }*/

   iteration++;
        fprintf(fpw ,"%.25lf %.25lf %.25lf %.25lf \n", 
         t,y[0],y[1],y[2]);

   
        if(iteration > 30000) break;
      }

      fclose(fpw);
     return 0;
   }
        #include<stdio.h>
        #include<math.h>
        #include<stdbool.h>
        #include  <stdlib.h>

        #define N 3

        double ddopri5(void fcn(double, double *, double *), 
        double *y);
        double alpha;
        void fcn(double t, double *y, double *f);
        double eps;

        int main(void){
          double y[N];
          //eps = 1.e-9;
           printf("Enter epsilon:\n"); 
           scanf("%lg", &eps);
           y[0]=1.0;
           y[1]=12.00;
           y[2]=30.00000567845; 
           ddopri5(fcn, y);

         }


        void fcn(double t, double *y, double *f){
               double p,hub;
               p=0;
               hub=y[1]/y[0];

               f[0] = y[1];
               f[1] = -(y[0]*y[2]+3*p*y[0])/6.0;
               f[2] = -3*hub*y[2]-3*hub*p;

            }

       double ddopri5(void fcn(double, double *, double *), 
                      double *y){
              double t, h, a, b, tw, chi;
              double w[N], k1[N], k2[N], k3[N], k4[N], k5[N], 
                   k6[N],k7[N], err[N], dy[N];
               int i;
               double errabs;
               int iteration;
               FILE *fpw;
               fpw=fopen("case1.dat", "w");

               iteration = 0; 
               //eps = 1.e-9;
               h = 1.0e-6;
               a = 0.0;
               b = 30;
               t = a;
               while(t < b -eps){
                  fcn(t, y, k1);
                  tw = t+ (1.0/5.0)*h;
                  for(i = 0; i < N; i++){
                     w[i] = y[i] + h*(1.0/5.0)*k1[i];    
                   }
                  fcn(tw, w, k2);
                  tw = t+ (3.0/10.0)*h;
                  for(i = 0; i < N; i++){
                      w[i] = y[i] + h*((3.0/40.0)*k1[i] + 
                            (9.0/40.0)*k2[i]);   
                        }

                fcn(tw, w, k3);
                tw = t+ (4.0/5.0)*h;
                for(i = 0; i < N; i++){
                     w[i] = y[i] + h*((44.0/45.0)*k1[i] - 
                           (56.0/15.0)*k2[i] + 
                          (32.0/9.0)*k3[i]); 
                       }
                 fcn(tw, w, k4);
                 tw = t+ (8.0/9.0)*h;
                 for(i = 0; i < N; i++){
                 w[i] = y[i] + h*((19372.0/6561.0)*k1[i] - 
                         (25360.0/2187.0)*k2[i] + 
                          (64448.0/6561.0)*k3[i] 
                          -(212.0/729.0)*k4[i]);   
                     }
                  fcn(tw, w, k5);
                  tw = t + h;
                  for(i = 0; i < N; i++){
                    w[i] = y[i] + h*((9017.0/3168.0)*k1[i] - 
                           (355.0/33.0)*k2[i] + 
                           (46732.0/5247.0)*k3[i] + 
                           (49.0/176.0)*k4[i] - 
                           (5103.0/18656.0)*k5[i]) ;   
                      }
                 fcn(tw, w, k6);
                 tw = t + h;
                 for(i = 0; i < N; i++){
                   w[i] = y[i] + h*((35.0/384.0)*k1[i] + 
                          (500.0/1113.0)*k3[i] + 
                          (125.0/192.0)*k4[i] - 
                          (2187.0/6784.0)*k5[i] + 
                          (11.0/84.0)*k6[i]);  
                     }
                 fcn(tw, w, k7);
                 errabs = 0;
                 for(i = 0; i < N; i++){
                    dy[i] = h*((35.0/384.0)*k1[i] + 
                            (500.0/1113.0)*k3[i] + 
                            (125.0/192.0)*k4[i] - 
                            (2187.0/6784.0)*k5[i] + 
                             (11.0/84.0)*k6[i]);
     
                       err[i] =  h*((71.0/57600.0)*k1[i]  - 
                               (71.0/16695.0)*k3[i] 
                               + (71.0/1920.0)*k4[i] - 
                               (17253.0/339200.0)*k5[i] + 
                              (22.0/525.0)*k6[i] - 
                              (1.0/40.0)*k7[i]);
                       errabs+=err[i]*err[i];
                 }


                errabs = sqrt(errabs);
         
                if( errabs < eps){
                t+= h;
        
                for(i = 0; i < N; i++){
                         y[i]+=dy[i];            
                       }
              } 
             chi=errabs/eps;
             chi = pow(chi, (1.0/6.0));
             if(chi > 10)    chi = 10;
             if(chi < 0.1)   chi = 0.1;
             h*=  0.95/chi;
             if( t + h > b ) h = b - t;      
        
             iteration++;
             fprintf(fpw ,"%.25lf %.25lf %.25lf %.25lf \n", 
                            t,y[0],y[1],y[2]);

   
             if(iteration > 30000) break;
            }

          fclose(fpw);
          return 0;
         }
added 5228 characters in body
Source Link
Dori
  • 41
  • 4

The code I used to solve this is

        #include<stdio.h>
        #include<math.h>
        #include<stdbool.h>
        #include  <stdlib.h>

        #define N 3

       double ddopri5(void fcn(double, double *, double *), 
       double *y);
      double alpha;
      void fcn(double t, double *y, double *f);
      double eps;

      int main(void){
     double y[N];
     //eps = 1.e-9;
      printf("Enter epsilon:\n"); 
      scanf("%lg", &eps);
      y[0]=1.0;
      y[1]=12.00;
      y[2]=30.00000567845; 
     ddopri5(fcn, y);

     }


    void fcn(double t, double *y, double *f){
     /*  double h = 0.25;*/
     double p,hub;
     p=0;
    hub=y[1]/y[0];
    f[0] = y[1];
    f[1] = -(y[0]*y[2]+3*p*y[0])/6.0;
    f[2] = -3*hub*y[2]-3*hub*p;

    }

      double ddopri5(void fcn(double, double *, double *), double 
    *y){
      double t, h, a, b, tw, chi;
      double w[N], k1[N], k2[N], k3[N], k4[N], k5[N], k6[N], 
     k7[N], err[N], dy[N];
      int i;
    double errabs;
     int iteration;
     FILE *fpw;
     fpw=fopen("case1.dat", "w");


      iteration = 0;

     //eps = 1.e-9;
     h = 1.0e-6;
     a = 0.0;
     b = 30;
     t = a;
     while(t < b -eps){

     /* printf("%lg\n", eps);*/
     fcn(t, y, k1);
      tw = t+ (1.0/5.0)*h;
      for(i = 0; i < N; i++){
        /*printf("k1[%i] = %.15lf \n", i, k1[i]);*/
        w[i] = y[i] + h*(1.0/5.0)*k1[i];    
       }
      fcn(tw, w, k2);
      tw = t+ (3.0/10.0)*h;
      for(i = 0; i < N; i++){
        /*printf("k2[%i] = %.15lf \n", i, k2[i]);*/
        w[i] = y[i] + h*((3.0/40.0)*k1[i] + (9.0/40.0)*k2[i]);  
      }

   fcn(tw, w, k3);
    tw = t+ (4.0/5.0)*h;
    for(i = 0; i < N; i++){
        /*printf("k3[%i] = %.15lf \n", i, k3[i]);*/
        w[i] = y[i] + h*((44.0/45.0)*k1[i] - (56.0/15.0)*k2[i] + 
      (32.0/9.0)*k3[i]); 
    }
    fcn(tw, w, k4);
    tw = t+ (8.0/9.0)*h;
    for(i = 0; i < N; i++){
        /*printf("k4[%i] = %.15lf \n", i, k4[i]);*/
        w[i] = y[i] + h*((19372.0/6561.0)*k1[i] - 
      (25360.0/2187.0)*k2[i] + (64448.0/6561.0)*k3[i] - 
        (212.0/729.0)*k4[i]);   
      }
       fcn(tw, w, k5);
       tw = t + h;
      for(i = 0; i < N; i++){
        /*printf("k5[%i] = %.15lf \n", i, k5[i]);*/
        w[i] = y[i] + h*((9017.0/3168.0)*k1[i] - 
     (355.0/33.0)*k2[i] + (46732.0/5247.0)*k3[i] + 
      (49.0/176.0)*k4[i] - (5103.0/18656.0)*k5[i]) ;   
    }
    fcn(tw, w, k6);

    tw = t + h;
    for(i = 0; i < N; i++){
        /*printf("k6[%i] = %.15lf \n", i, k6[i]);*/
        w[i] = y[i] + h*((35.0/384.0)*k1[i] + 
        (500.0/1113.0)*k3[i] + (125.0/192.0)*k4[i] - 
          (2187.0/6784.0)*k5[i] + (11.0/84.0)*k6[i]);  
       }
       fcn(tw, w, k7);


      errabs = 0;

    for(i = 0; i < N; i++){
    /*  printf("k7[%i] = %.15lf \n", i, k7[i]);*/
   /*          dy[i] = h*((71.0/57600.0)*k1[i]  - 
         (71.0/16695.0)*k3[i] + (71.0/1920.0)*k4[i] - 
            (17253.0/339200.0)*k5[i] + (22.0/525.0)*k6[i]);*/
        dy[i] = h*((35.0/384.0)*k1[i] + (500.0/1113.0)*k3[i] + 
          (125.0/192.0)*k4[i] - (2187.0/6784.0)*k5[i] + 
            (11.0/84.0)*k6[i]);
        /*err[i] =  h*((71.0/57600.0)*k1[i]  + 
          (71.0/16695.0)*k3[i] + (71.0/1920.0)*k4[i] - 
         (17253.0/339200.0)*k5[i] + (22.0/525.0)*k6[i] - 
         (1.0/40.0)*k7[i])*/;
        err[i] =  h*((71.0/57600.0)*k1[i]  - (71.0/16695.0)*k3[i] 
         + (71.0/1920.0)*k4[i] - (17253.0/339200.0)*k5[i] + 
            (22.0/525.0)*k6[i] - (1.0/40.0)*k7[i]);
          /*printf("err[%i] = %.15lf \n", i, err[i]);*/
        errabs+=err[i]*err[i];
         }


         errabs = sqrt(errabs);
         /*  printf("errabs = %.15lf\n", errabs); */
         if( errabs < eps){
        t+= h;
        /*  printf(" FROM IF \t t = %.25lf, \n h  = %.25lf, \n 
         errabs = %.25lf, \n iteration = %i . \n", t, h, errabs, 
        iteration);*/
        for(i = 0; i < N; i++){
            y[i]+=dy[i];            
        }
      } 
      chi=errabs/eps;
      chi = pow(chi, (1.0/6.0));
       if(chi > 10)    chi = 10;
      if(chi < 0.1)   chi = 0.1;
       h*=  0.95/chi;
       if( t + h > b ) h = b - t;      
          /*  for(i = 0; i < 4; i++){
        printf("y[%i] = %.15lf \n", i, y[i]);
        }*/

   iteration++;
        fprintf(fpw ,"%.25lf %.25lf %.25lf %.25lf \n", 
         t,y[0],y[1],y[2]);

   
        if(iteration > 30000) break;
      }

      fclose(fpw);
     return 0;
   }

The initial conditions doesnot have and physical significance.

The code I used to solve this is

        #include<stdio.h>
        #include<math.h>
        #include<stdbool.h>
        #include  <stdlib.h>

        #define N 3

       double ddopri5(void fcn(double, double *, double *), 
       double *y);
      double alpha;
      void fcn(double t, double *y, double *f);
      double eps;

      int main(void){
     double y[N];
     //eps = 1.e-9;
      printf("Enter epsilon:\n"); 
      scanf("%lg", &eps);
      y[0]=1.0;
      y[1]=12.00;
      y[2]=30.00000567845; 
     ddopri5(fcn, y);

     }


    void fcn(double t, double *y, double *f){
     /*  double h = 0.25;*/
     double p,hub;
     p=0;
    hub=y[1]/y[0];
    f[0] = y[1];
    f[1] = -(y[0]*y[2]+3*p*y[0])/6.0;
    f[2] = -3*hub*y[2]-3*hub*p;

    }

      double ddopri5(void fcn(double, double *, double *), double 
    *y){
      double t, h, a, b, tw, chi;
      double w[N], k1[N], k2[N], k3[N], k4[N], k5[N], k6[N], 
     k7[N], err[N], dy[N];
      int i;
    double errabs;
     int iteration;
     FILE *fpw;
     fpw=fopen("case1.dat", "w");


      iteration = 0;

     //eps = 1.e-9;
     h = 1.0e-6;
     a = 0.0;
     b = 30;
     t = a;
     while(t < b -eps){

     /* printf("%lg\n", eps);*/
     fcn(t, y, k1);
      tw = t+ (1.0/5.0)*h;
      for(i = 0; i < N; i++){
        /*printf("k1[%i] = %.15lf \n", i, k1[i]);*/
        w[i] = y[i] + h*(1.0/5.0)*k1[i];    
       }
      fcn(tw, w, k2);
      tw = t+ (3.0/10.0)*h;
      for(i = 0; i < N; i++){
        /*printf("k2[%i] = %.15lf \n", i, k2[i]);*/
        w[i] = y[i] + h*((3.0/40.0)*k1[i] + (9.0/40.0)*k2[i]);  
      }

   fcn(tw, w, k3);
    tw = t+ (4.0/5.0)*h;
    for(i = 0; i < N; i++){
        /*printf("k3[%i] = %.15lf \n", i, k3[i]);*/
        w[i] = y[i] + h*((44.0/45.0)*k1[i] - (56.0/15.0)*k2[i] + 
      (32.0/9.0)*k3[i]); 
    }
    fcn(tw, w, k4);
    tw = t+ (8.0/9.0)*h;
    for(i = 0; i < N; i++){
        /*printf("k4[%i] = %.15lf \n", i, k4[i]);*/
        w[i] = y[i] + h*((19372.0/6561.0)*k1[i] - 
      (25360.0/2187.0)*k2[i] + (64448.0/6561.0)*k3[i] - 
        (212.0/729.0)*k4[i]);   
      }
       fcn(tw, w, k5);
       tw = t + h;
      for(i = 0; i < N; i++){
        /*printf("k5[%i] = %.15lf \n", i, k5[i]);*/
        w[i] = y[i] + h*((9017.0/3168.0)*k1[i] - 
     (355.0/33.0)*k2[i] + (46732.0/5247.0)*k3[i] + 
      (49.0/176.0)*k4[i] - (5103.0/18656.0)*k5[i]) ;   
    }
    fcn(tw, w, k6);

    tw = t + h;
    for(i = 0; i < N; i++){
        /*printf("k6[%i] = %.15lf \n", i, k6[i]);*/
        w[i] = y[i] + h*((35.0/384.0)*k1[i] + 
        (500.0/1113.0)*k3[i] + (125.0/192.0)*k4[i] - 
          (2187.0/6784.0)*k5[i] + (11.0/84.0)*k6[i]);  
       }
       fcn(tw, w, k7);


      errabs = 0;

    for(i = 0; i < N; i++){
    /*  printf("k7[%i] = %.15lf \n", i, k7[i]);*/
   /*          dy[i] = h*((71.0/57600.0)*k1[i]  - 
         (71.0/16695.0)*k3[i] + (71.0/1920.0)*k4[i] - 
            (17253.0/339200.0)*k5[i] + (22.0/525.0)*k6[i]);*/
        dy[i] = h*((35.0/384.0)*k1[i] + (500.0/1113.0)*k3[i] + 
          (125.0/192.0)*k4[i] - (2187.0/6784.0)*k5[i] + 
            (11.0/84.0)*k6[i]);
        /*err[i] =  h*((71.0/57600.0)*k1[i]  + 
          (71.0/16695.0)*k3[i] + (71.0/1920.0)*k4[i] - 
         (17253.0/339200.0)*k5[i] + (22.0/525.0)*k6[i] - 
         (1.0/40.0)*k7[i])*/;
        err[i] =  h*((71.0/57600.0)*k1[i]  - (71.0/16695.0)*k3[i] 
         + (71.0/1920.0)*k4[i] - (17253.0/339200.0)*k5[i] + 
            (22.0/525.0)*k6[i] - (1.0/40.0)*k7[i]);
          /*printf("err[%i] = %.15lf \n", i, err[i]);*/
        errabs+=err[i]*err[i];
         }


         errabs = sqrt(errabs);
         /*  printf("errabs = %.15lf\n", errabs); */
         if( errabs < eps){
        t+= h;
        /*  printf(" FROM IF \t t = %.25lf, \n h  = %.25lf, \n 
         errabs = %.25lf, \n iteration = %i . \n", t, h, errabs, 
        iteration);*/
        for(i = 0; i < N; i++){
            y[i]+=dy[i];            
        }
      } 
      chi=errabs/eps;
      chi = pow(chi, (1.0/6.0));
       if(chi > 10)    chi = 10;
      if(chi < 0.1)   chi = 0.1;
       h*=  0.95/chi;
       if( t + h > b ) h = b - t;      
          /*  for(i = 0; i < 4; i++){
        printf("y[%i] = %.15lf \n", i, y[i]);
        }*/

   iteration++;
        fprintf(fpw ,"%.25lf %.25lf %.25lf %.25lf \n", 
         t,y[0],y[1],y[2]);

   
        if(iteration > 30000) break;
      }

      fclose(fpw);
     return 0;
   }

The initial conditions doesnot have and physical significance.

edited title
Link
Urb
  • 2.7k
  • 4
  • 14
  • 26

Numerical Solutionsolution to Backgroundbackground equations of cosmology

deleted 19 characters in body
Source Link
Elio Fabri
  • 5.9k
  • 1
  • 11
  • 18
Loading
Source Link
Dori
  • 41
  • 4
Loading