Anda di halaman 1dari 2

/*******************************************************************

* Differential equations of order 1 *


* by Runge-Kutta method of order 4 *
* *
* C++ version by J-P Moreau, Paris *
* ---------------------------------------------------------------- *
* Reference: "Analyse en Turbo Pascal versions 5.5 et 6.0 By Marc *
* DUCAMP et Alain REVERCHON - Eyrolles, Paris 1991" *
* [BIBLI 03]. *
* ---------------------------------------------------------------- *
* SAMPLE RUN: *
* *
* Example: integrate y"=4x*(y+sqrt(y))/1+x^2 from x=0 to x=1 *
* *
* DIFFERENTIAL EQUATION WITH 1 VARIABLE OF ORDER 1 *
* of type y" = f(x,y) *
* *
* begin value x : 0 *
* end value x : 1 *
* y value at x0 : 1 *
* number of points: 11 *
* finesse : 10 *
* *
* X Y *
* ------------------------- *
* 0.000000 1.000000 *
* 0.100000 1.040400 *
* 0.200000 1.166400 *
* 0.300000 1.392400 *
* 0.400000 1.742400 *
* 0.500000 2.250000 *
* 0.600000 2.958400 *
* 0.700000 3.920400 *
* 0.800000 5.198400 *
* 0.900000 6.864400 *
* 1.000000 9.000000 *
* *
*******************************************************************/
#include <stdio.h>
#include <math.h>
#define SIZE 50
int fi,m;
double xi,xf,yi,T[SIZE];
double fp(double x, double y) {
//Example: y'=4x(y+rac(y))/(1+x²)
return 4*x*(y+sqrt(y))/(1+x*x);
}
void Affiche(double *t,int m,double xi,double xf) {
double h,x; int i;
h=(xf-xi)/(m-1);
x=xi-h;
printf("\n X Y \n");
printf("------------------------\n");
for (i=1; i<m+1; i++) {
x+=h;
printf(" %9.6f %9.6f\n", x, t[i]);
}
}
/***************************************************************************
* SOLVING DIFFERENTIAL EQUATIONS WITH 1 VARIABLE OF ORDER 1 *
* of type y' = f(x,y) *
* ----------------------------------------------------------------------- *
* INPUTS: *
* fp Given equation to integrate (see test program) *
* xi, xf Begin, end values of variable x *
* yi Begin Value of y at x=xi *
* m Number of points to calculate *
* fi finesse (number of intermediate points) *
* *
* OUTPUTS: *
* t real vector storing m results for function y *
**************************************************************************/
void Equadif1(double *t,double xi,double xf, double yi,int m,int fi) {
int i,j,ni;
double a,b,c,d,h,x,y;
if (fi < 1) return;
h = (xf - xi) / fi / (m-1);
y = yi;
t[1] = yi;
for (i = 1; i<m+1; i++) {
ni = (i - 1) * fi - 1;
for (j = 1; j<fi+1; j++) {
x = xi + h * (ni + j);
a = h * fp(x,y);
b = h * fp(x+h/2,y+a/2);
c = h * fp(x+h/2,y+b/2);
x = x + h;
d = h * fp(x,y+c);
y = y + (a + b + b + c + c + d) / 6;
}
t[i+1] = y;
}
Affiche(t,m,xi,xf);
}
void main() {
printf("\n DIFFERENTIAL EQUATION WITH 1 VARIABLE OF ORDER 1\n");
printf(" of type y' = f(x,y)\n\n");
printf(" begin value x : "); scanf("%lf",&xi);
printf(" end value x : "); scanf("%lf",&xf);
printf(" y value at x0 : "); scanf("%lf",&yi);
printf(" number of points: "); scanf("%d",&m);
printf(" finesse : "); scanf("%d",&fi);
Equadif1(T,xi,xf,yi,m,fi);
}
// End of file teqdif1.cpp