예제 #1
0
///---------------------------------------------------------------------------------
///
///---------------------------------------------------------------------------------
StateVector3D SmokeUpdateStrategy::RK4( const StateVector3D& initialState, const double& deltaT )
{
    StateVector3D s1 = initialState;

    StateVector3D s1Derivative = CalculateDerivative( s1 );
    StateVector3D s2 = initialState + s1Derivative * (float)(deltaT / 2.0);

    StateVector3D s2Derivative = CalculateDerivative( s2 );
    StateVector3D s3 = initialState + s2Derivative * (float)(deltaT / 2.0);

    StateVector3D s3Derivative = CalculateDerivative( s3 );
    StateVector3D s4 = initialState + s3Derivative * (float)deltaT;

    StateVector3D s4Derivative = CalculateDerivative( s4 );

    StateVector3D nextState = initialState + (s1Derivative + (s2Derivative * 2.0f) + (s3Derivative * 2.0f) + s4Derivative) * (float)(deltaT / 6.0);

    return nextState;
}
예제 #2
0
/*
 * The overall architecture is to repeat the same process,
 * terminating only if a negative degree is entered.
 *
 * Thus, we repeat the process READ->COMPUTE->PRINT.
 *
 */
int main(void)
{
    int maxDegree;                  /* the maximum degree of the polynomial the user wants to enter */
    int *polynomial = NULL;  /* the polynomial is represented by its coefficients, stored here */
    int *derivative = NULL;    /* the reciprocal polynomial is represented by the coefficients stored here */
    
    while (1) {
        /*
         * Clear the data structures from previous iteration
         * NB It's safe to free a NULL pointer
         */
        free(polynomial);
        free(derivative);
        polynomial = derivative = NULL;
        
        /*
         * Read data
         */
        printf("Please enter the maximum degree of the polynomial: ");
        scanf("%d", &maxDegree);
        
        // SENTINEL: Quit program if user inputs negative degree
        if (maxDegree < 0) break;
        
        /*
         * Allocate memory
         */
        polynomial = calloc(maxDegree+1, sizeof(int) );
        derivative = calloc(maxDegree, sizeof(int) );
        if (!polynomial || !derivative) {
            fprintf(stderr, "Error: unable to allocate memory.\n");
            exit(EXIT_FAILURE);
        }
        
        printf("Please enter the coefficients: ");
        ReadPolynomial(polynomial, maxDegree);
        
        /*
         * Compute the derivative
         */
        CalculateDerivative(polynomial, derivative, maxDegree);
        
        /*
         * Print the result
         */
        PrintPolynomial("The polynomial is", polynomial, maxDegree);
        PrintPolynomial("Its derivative is", derivative, maxDegree);
        printf("\n");
    }
    
    return 0;
}