Aller au contenu

Mathc initiation/Fichiers h : x 21a3

Un livre de Wikilivres.

Sommaire

Installer ce fichier dans votre répertoire de travail.

x_runge.h
utilitaire
/* ---------------------------------- */
/* save as x_runge.h                  */
/* ---------------------------------- */
double RungeKutta_Method(
double a,
double b,
double n,
double ya,
double (*P_f)(double x,double y)
)
{
double i;
double h;
double x_k;
double K1,K2,K3,K4;

    h = ((b-a)/n);

    for(i=1,x_k=a; i<=n; i++)
        {
         K1 = h * (*P_f)(x_k,       ya        );
         K2 = h * (*P_f)(x_k + .5*h,ya + .5*K1);
         K3 = h * (*P_f)(x_k + .5*h,ya + .5*K2);
         K4 = h * (*P_f)(x_k +    h,ya +    K3);

         ya = ya +(1/6.)*(K1+2*K2+2*K3+K4);
         x_k +=h;
        }

 return(ya);
}
/* ---------------------------------- */
void p_RungeKutta_Method(
double a,
double b,
double n,
double ya,
double (*P_f)(double x,double y)
)
{
double i;
double h;
double x_k;
double K1,K2,K3,K4;

    h = ((b-a)/n);

    printf("  k  | x_k   | y_k         \n");
    printf(" --------------------------\n");
    for(i=1,x_k=a; i<=n; i++)
        {
         K1 = h * (*P_f)(x_k,       ya        );
         K2 = h * (*P_f)(x_k + .5*h,ya + .5*K1);
         K3 = h * (*P_f)(x_k + .5*h,ya + .5*K2);
         K4 = h * (*P_f)(x_k +    h,ya +    K3);

         ya = ya +(1/6.)*(K1+2*K2+2*K3+K4);
         x_k +=h;
         printf(" %3.f | %.3f | %.4f\n",i,x_k,ya);
        }
}
/* ---------------------------------- */
/* ---------------------------------- */
Déclaration des fonctions de calcul.