1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| #include <stdlib.h>
#include <stdio.h>
#include <math.h>
FILE *fp1, *fp2, *fp3 ; /* fichiers de stockage des résultats */
float x_init, y_init, z_init ; /* conditions initiales */
float sigma, rho, beta ; /* paramètres du système */
float h ; /* pas d'intégration */
int N ; /* nombre d'itérations */
float *Y1, *Y2, *Y3 ; /* vecteurs de stockage des résultats */
int i ;
float k1, k2, k3, k4 ; /* variables intermédiaires pour équation 1 */
float l1, l2, l3, l4 ; /* variables intermédiaires pour équation 2 */
float m1, m2, m3, m4 ; /* variables intermédiaires pour équation 3 */
/* fonctions */
float func1(float x, float y, float s) ;
float func2(float x, float y, float z, float r) ;
float func3(float x, float y, float z, float b) ;
main()
{
/* Saisie des coordonnées initiales */
printf("\n===> Saisie des coordonnees initiales : ") ;
printf("\nx0 = ") ;
scanf("%f", &x_init) ;
printf("y0 = ") ;
scanf("%f", &y_init) ;
printf("z0 = ") ;
scanf("%f", &z_init) ;
/* Saisie des paramètres du système */
printf("\n===> Saisie des parametres du systeme : ") ;
printf("\nsigma = ") ;
scanf("%f", &sigma) ;
printf("rho = ") ;
scanf("%f", &rho) ;
printf("beta = ") ;
scanf("%f", &beta) ;
/* Saisie des paramètres d'intégration */
printf("\n===> Saisie des parametres d'integration : ") ;
printf("\npas d'integration : h = ") ;
scanf("%f", &h) ;
printf("nombre d'iterations : N = ") ;
scanf("%d", &N) ;
/* Allocation de mémoire */
Y1 = (float*) malloc(N*sizeof(float)) ;
Y2 = (float*) malloc(N*sizeof(float)) ;
Y3 = (float*) malloc(N*sizeof(float)) ;
/* Conditions initiales */
Y1[0] = x_init ;
Y2[0] = y_init ;
Y3[0] = z_init ;
fp1 = fopen("X.m","w") ;
fp2 = fopen("Y.m","w") ;
fp3 = fopen("Z.m","w") ;
fprintf(fp1,"x(%d) = %f ;\n", 1, Y1[0]) ;
fprintf(fp2,"y(%d) = %f ;\n", 1, Y2[0]) ;
fprintf(fp3,"z(%d) = %f ;\n", 1, Y3[0]) ;
i = 0 ;
while (i < N-1)
{
/* Résolution du système par la méthode de Runge-Kutta d'ordre 4 */
k1 = func1(Y1[i],Y2[i],sigma) ;
k2 = func1(Y1[i] + 0.5*h*k1,Y2[i] + 0.5*h*k1,sigma) ;
k3 = func1(Y1[i] + 0.5*h*k2,Y2[i] + 0.5*h*k2,sigma) ;
k4 = func1(Y1[i] + h*k3,Y2[i] + h*k3,sigma) ;
l1 = func2(Y1[i],Y2[i],Y3[i],rho) ;
l2 = func2(Y1[i] + 0.5*h*l1,Y2[i] + 0.5*h*l1,Y3[i] + 0.5*h*l1,rho) ;
l3 = func2(Y1[i] + 0.5*h*l2,Y2[i] + 0.5*h*l2,Y3[i] + 0.5*h*l2,rho) ;
l4 = func2(Y1[i] + h*l3,Y2[i] + h*l3,Y3[i] + h*l3,rho) ;
m1 = func3(Y1[i],Y2[i],Y3[i],beta) ;
m2 = func3(Y1[i] + 0.5*h*m1,Y2[i] + 0.5*h*m1,Y3[i] + 0.5*h*m1,beta) ;
m3 = func3(Y1[i] + 0.5*h*m2,Y2[i] + 0.5*h*m2,Y3[i] + 0.5*h*m2,beta) ;
m4 = func3(Y1[i] + h*m3,Y2[i] + h*m3,Y3[i] + h*m3,beta) ;
Y1[i+1] = Y1[i] + (h/6.)*(k1 +2.*k2 + 2.*k3 + k4) ;
Y2[i+1] = Y2[i] + (h/6.)*(l1 +2.*l2 + 2.*l3 + l4) ;
Y3[i+1] = Y3[i] + (h/6.)*(m1 +2.*m2 + 2.*m3 + m4) ;
fprintf(fp1,"x(%d) = %f ;\n", i+2, Y1[i+1]) ;
fprintf(fp2,"y(%d) = %f ;\n", i+2, Y2[i+1]) ;
fprintf(fp3,"z(%d) = %f ;\n", i+2, Y3[i+1]) ;
i += 1 ;
}
fclose(fp1) ;
fclose(fp2) ;
fclose(fp3) ;
free(Y1) ;
free(Y2) ;
free(Y3) ;
return (EXIT_SUCCESS) ;
}
/* f1(x,y,s) = s*(y - x) */
float func1(float x, float y, float s)
{
float res ;
res = s*(y - x) ;
return res ;
}
/* f2(x,y,z,r) = x*(r - z) - y */
float func2(float x, float y, float z, float r)
{
float res ;
res = x*(r - z) - y ;
return res ;
}
/* f3(x,y,z,b) = x*y - b*z */
float func3(float x, float y, float z, float b)
{
float res ;
res = x*y - b*z ;
return res ;
} |
Partager