Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h" /* Libarary Definitions - Used for printf, scanf, etc */
- #include "math.h" /* Math Libarary for C - Used for Abs() = Absolute Value */
- /* User Created Function 1 - Print Error "Integration Cannot Be Done" */
- void Print_Integration_Error (int B, int C, int D, double X_Initial, double X_Final)
- {
- printf(" %d %d %d %lf %lf \n\n ", B, C, D, X_Initial, X_Final); /* If X_Initial is Less Than or Equal to 0.0 and X_Final is Greater or Equal to 0.0 */
- printf("===================================================================\n"); /* It Will Print The Data Values and Message Explaining Integration Cannot Be Done */
- printf(" Integration Cannot Be Done As The Function Is Not Defined For X=0 \n" );
- printf(" ===================================================================\n");
- }
- /* User Created Function 2 - Prints Data Values and The Heading For Results */
- void Print_Top_Half_of_Table (int B, int C, int D, double X_Initial, double X_Final)
- {
- printf("\n");
- printf(" %d %d %d %lf %lf \n\n ", B, C, D, X_Initial, X_Final); /* If X_Initial isn't Less Than or Equal to 0.0 and X_Final isn't Greater or Equal to 0.0 */
- printf("===================================================\n"); /* It Will Print The Data Values and The Table for The Results of H, N and Total Area */
- printf(" | N | H | AREA |\n");
- printf(" |=================================================|\n");
- }
- /* User Created Function 3 - Prints The Results In a Table */
- void Print_Results_In_Table (int N, double Height, double Area_Total)
- {
- printf(" | %6d | %8.5f | %8.5f |\n\n", N, Height, Area_Total); /* User Created Function 3 - Prints The Results in a Table With The N, Height and Area Total for Each Data Set */
- }
- /* User Created Function - Calculates Area_i */
- void Area_i_Calculate (double Height, double X_a, double X_b, int B, int C, int D)
- {
- double Area_i;
- Area_i = Height * (abs(X_a*X_a + B*X_a + C + D/X_a) + abs(X_b*X_b + B*X_b + C + D/X_b)) * 0.5;
- }
- /* User Created Function - Calculates N = Number of Trapziums */
- void N_Calculate (double X_Final, double X_Initial, double Height)
- {
- int N;
- N = (int) ( (X_Final - X_Initial) / (Height + 0.5 ));
- }
- /* User Created Function - Calculates The Total Area Under The Curve */
- void Area_Total_Calculate (double Area_i)
- {
- double Area_Total;
- Area_Total = Area_i + Area_Total;
- }
- /* User Created Function - Calculates X_b*/
- void X_b_Calculate (double X_a, double Height)
- {
- double X_b;
- X_b = X_a + Height;
- }
- /* User Created Function - Calculates X_a */
- void X_a_Calculate (double X_b)
- {
- double X_a;
- X_a = X_b;
- }
- int
- main (void)
- {
- FILE *inp; /* Input File Pointer - The Variable inp will hold a descriptor for the file once it is open for input*/
- inp = fopen("C:\\input1.dat", "r"); /* The File Location/Path Directory - And The Selected Mode is Read Input File*/
- int N, /* Output - The Number of Trapezium Calculations Required */
- B, /* Scanned Value - The B Value From The File - input1.dat */
- C, /* Scanned Value - The C Value From The File - input1.dat */
- D, /* Scanned Value - The D Value From The File - input1.dat */
- input_status; /* Status Value Returned by fscanf - Either Success or EOF */
- double X_Initial, /* Scanned Value - The Read X Initial Value From The File - input1.dat */
- X_Final, /* Scanned Value - The Read X Final Value From The File - input1.dat */
- Height, /* Output - The Calculated Height Value for The Trapizium Rule */
- Area_Total, /* Output - The Calculated Total Area Under The Curve */
- Area_i, /* Calculated Value - The Mathematical Relationships Associated With Numerical Integration (Trapzium Rule) */
- X_a, /* Calculated Value - The X_a Value is X_Initial ( Xi ) */
- X_b; /* Calculated Value - The X_b Value is X_Initial + Height ( Xi + h ) */
- printf("\n"); /* Creates New Line - Purpose to Make the End Result Clearer */
- if (inp == NULL) { /* If inp (Input File Pointer) Cannot Locate or Open the File */
- printf("Cannot Find or Open input1.dat File! \n"); /* It will Display a Error Message for The User */
- }
- else
- printf("The File Was Found \n\n"); /* If the file was found it will Display a Success Message And Print a Heading for The Data Inputs */
- printf("The Following are The Data Inputs: \n");
- printf("\n"); /* Creates New Line - Purpose to Make the End Result Clearer */
- input_status = fscanf(inp, "%d %d %d %lf %lf", &B, &C, &D, &X_Initial, &X_Final); /* Scans The File for Data Values and Stores Them at a Given Location */
- while (input_status != EOF){ /* Status Value Returned by fscanf - Either Success or EOF */
- if( X_Initial <= 0.0 && X_Final >= 0.0){ /* If X_Initial is Less Than or Equal to 0.0 and X_Final is Greater or Equal to 0.0 */
- Print_Integration_Error (B, C, D, X_Initial, X_Final); /* User Created Function 1 - It Will Print The Data Values and Message Explaining Integration Cannot Be Done */
- }
- else
- {
- Print_Top_Half_of_Table (B, C, D, X_Initial, X_Final); /* User Created Function 2 - It Will Print The Data Values and Top Half of The Table if Integration Cannot Be Done */
- for ( Height = 0.1; Height >= 0.00001; Height = Height * 0.1 ){ /* Starting with an h value of 0.1 and then continuing with h = 0.01, 0.001, etc. until the latest two calculations are within 0.0005 of each other, or h becomes 0.00001. */
- /* You can see that as h is reduced and the number of trapeziums increases, we will get a better estimate. */
- N = (X_Final - X_Initial) / ( Height ) + ( 0.5 ); /* Calculating The Value N */
- X_a = X_Initial; /* X_a = Xi - Used in The Area_i Calculation */
- Area_Total = 0;
- for ( int i = 0 ; i < N ; i++){
- X_b = X_a + Height; /* X_b = Xi + h - (X_initial + Height) Used in The Area_i Calculation */
- Area_i = Height * (abs(X_a*X_a + B*X_a + C + D/X_a) + abs(X_b*X_b + B*X_b + C + D/X_b)) * 0.5; /* Calculating Area_i */
- Area_Total = Area_i + Area_Total; /* The Estimate of the Total Area Under The Curve is Simply The Sum of all the Trapezium Area Calculations. */
- X_a = X_b;
- }
- Print_Results_In_Table (N, Height, Area_Total); /* User Created Function 3 - Prints The Results in a Table With The N, Height and Area Total for Each Data Set */
- }
- }
- input_status = fscanf(inp, "%d %d %d %lf %lf", &B, &C, &D, &X_Initial, &X_Final); /* Once The First Data Has Been Done It Will Reloop Untill EOF */
- }
- fclose(inp); /* Closes The File */
- printf("\n"); /* Creates New Line - Purpose to Make the End Result Clearer */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement