/******************************************************* * This program calculates the discrete primitive of a * * user defined real function F(x) in n points by using * * a classical Runge-Kutta method of order 4 (better * * than a Simpson method of order 2). * * ---------------------------------------------------- * * SAMPLE RUN: * * (Calculate 20 points of the primitive of F(x)=x * * from x=-5 to x=5 with a finesse of 2. the value of * * the primitive at x=-5 will be given as 25/2). * * * * Calculate the discrete primitive of a function F(x) * * * * Value of x begin: -5 * * Value of x end : 5 * * Value of primitive in x begin: 12.5 * * Number of points to calculate: 20 * * Number of intermediate points: 2 * * * * x1 = -4.500000 y1 = 10.125000 * * x2 = -4.000000 y2 = 8.000000 * * x3 = -3.500000 y3 = 6.125000 * * x4 = -3.000000 y4 = 4.500000 * * x5 = -2.500000 y5 = 3.125000 * * x6 = -2.000000 y6 = 2.000000 * * x7 = -1.500000 y7 = 1.125000 * * x8 = -1.000000 y8 = 0.500000 * * ----------------------------------- * * x16 = 3.000000 y16 = 4.500000 * * x17 = 3.500000 y17 = 6.125000 * * x18 = 4.000000 y18 = 8.000000 * * x19 = 4.500000 y19 = 10.125000 * * x20 = 5.000000 y20 = 12.500000 * * * * (Exact values are yi = xi*xi/2). * * * * C++ version by J-P Moreau, Paris. * * (www.jpmoreau.fr) * *******************************************************/ #include #include double fa,xa,xb; int finesse,n; // given user defined function F(x) double F(double x) { return (x); } /****************************************************** * Calculate the discrete primitive of a real function * * F(x) by using a Runge-Kutta method of order 4. * * --------------------------------------------------- * * INPUTS: * * a begin x * * b end x * * fa value of primitive at x=a * * n number of calculated points * * fi number of intermediate points * * (finesse) * * * * OUTPUTS: * * The subroutine displays the n calculated * * points (xi,yi) of the primitive of the * * user defined function F(x). * * --------------------------------------------------- * * Ref.: "Mathematiques en Turbo Pascal by Alain Rever-* * chon and Marc Ducamp, Armand Colin Editor, * * Paris, 1990". * ******************************************************/ void Primitive(double a,double b,double fa, int n, int fi) { double h,h2,h6,ly,lr,x,y,u; int i,j; long ni; h=(b-a)/fi/n; h2=h/2; h6=h/6; lr=fa; ly=F(a); for (i=1; i<=n; i++) { ni=(i-1)*fi-1; for (j=1; j<=fi; j++) { x=a+h*(ni+j); u=4*F(x+h2); x=x+h; y=F(x); lr=lr+h6*(ly+u+y); ly=y; } printf(" x%d = %10.6f y%d = %10.6f\n", i, x, i, lr); } } void main() { printf("\n Calculate the discrete primitive of a function F(x)\n\n"); printf(" Value of x begin: "); scanf("%lf", &xa); printf(" Value of x end : "); scanf("%lf", &xb); printf(" Value of primitive in x begin: "); scanf("%lf", &fa); printf(" Number of points to calculate: "); scanf("%d", &n); printf(" Number of intermediate points: "); scanf("%d", &finesse); printf("\n\n"); Primitive(xa,xb,fa,n,finesse); printf("\n\n"); } // end of file primitiv.cpp