Advertisement
Guest User

Untitled

a guest
Sep 14th, 2012
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. #include "stdafx.h" /* Libarary Definitions - Used for printf, scanf, etc */
  2. #include "math.h" /* Math Libarary for C - Used for Abs() = Absolute Value */
  3.  
  4.  
  5. /* User Created Function 1 - Print Error "Integration Cannot Be Done" */
  6. void Print_Integration_Error (int B, int C, int D, double X_Initial, double X_Final)
  7. {
  8. 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 */
  9. printf("===================================================================\n"); /* It Will Print The Data Values and Message Explaining Integration Cannot Be Done */
  10. printf(" Integration Cannot Be Done As The Function Is Not Defined For X=0 \n" );
  11. printf(" ===================================================================\n");
  12. }
  13.  
  14. /* User Created Function 2 - Prints Data Values and The Heading For Results */
  15. void Print_Top_Half_of_Table (int B, int C, int D, double X_Initial, double X_Final)
  16. {
  17. printf("\n");
  18. 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 */
  19. printf("===================================================\n"); /* It Will Print The Data Values and The Table for The Results of H, N and Total Area */
  20. printf(" | N | H | AREA |\n");
  21. printf(" |=================================================|\n");
  22. }
  23.  
  24. /* User Created Function 3 - Prints The Results In a Table */
  25. void Print_Results_In_Table (int N, double Height, double Area_Total)
  26. {
  27. 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 */
  28. }
  29. /* User Created Function - Calculates Area_i */
  30. void Area_i_Calculate (double Height, double X_a, double X_b, int B, int C, int D)
  31. {
  32. double Area_i;
  33. 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;
  34. }
  35.  
  36. /* User Created Function - Calculates N = Number of Trapziums */
  37. void N_Calculate (double X_Final, double X_Initial, double Height)
  38. {
  39. int N;
  40. N = (int) ( (X_Final - X_Initial) / (Height + 0.5 ));
  41.  
  42. }
  43.  
  44. /* User Created Function - Calculates The Total Area Under The Curve */
  45. void Area_Total_Calculate (double Area_i)
  46. {
  47. double Area_Total;
  48.  
  49. Area_Total = Area_i + Area_Total;
  50.  
  51. }
  52.  
  53. /* User Created Function - Calculates X_b*/
  54. void X_b_Calculate (double X_a, double Height)
  55. {
  56. double X_b;
  57.  
  58. X_b = X_a + Height;
  59. }
  60.  
  61. /* User Created Function - Calculates X_a */
  62. void X_a_Calculate (double X_b)
  63. {
  64. double X_a;
  65. X_a = X_b;
  66. }
  67.  
  68.  
  69. int
  70. main (void)
  71. {
  72. FILE *inp; /* Input File Pointer - The Variable inp will hold a descriptor for the file once it is open for input*/
  73. inp = fopen("C:\\input1.dat", "r"); /* The File Location/Path Directory - And The Selected Mode is Read Input File*/
  74.  
  75. int N, /* Output - The Number of Trapezium Calculations Required */
  76. B, /* Scanned Value - The B Value From The File - input1.dat */
  77. C, /* Scanned Value - The C Value From The File - input1.dat */
  78. D, /* Scanned Value - The D Value From The File - input1.dat */
  79. input_status; /* Status Value Returned by fscanf - Either Success or EOF */
  80.  
  81. double X_Initial, /* Scanned Value - The Read X Initial Value From The File - input1.dat */
  82. X_Final, /* Scanned Value - The Read X Final Value From The File - input1.dat */
  83. Height, /* Output - The Calculated Height Value for The Trapizium Rule */
  84. Area_Total, /* Output - The Calculated Total Area Under The Curve */
  85. Area_i, /* Calculated Value - The Mathematical Relationships Associated With Numerical Integration (Trapzium Rule) */
  86. X_a, /* Calculated Value - The X_a Value is X_Initial ( Xi ) */
  87. X_b; /* Calculated Value - The X_b Value is X_Initial + Height ( Xi + h ) */
  88.  
  89.  
  90. printf("\n"); /* Creates New Line - Purpose to Make the End Result Clearer */
  91.  
  92. if (inp == NULL) { /* If inp (Input File Pointer) Cannot Locate or Open the File */
  93. printf("Cannot Find or Open input1.dat File! \n"); /* It will Display a Error Message for The User */
  94. }
  95. else
  96. 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 */
  97.  
  98. printf("The Following are The Data Inputs: \n");
  99.  
  100. printf("\n"); /* Creates New Line - Purpose to Make the End Result Clearer */
  101.  
  102.  
  103. 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 */
  104. while (input_status != EOF){ /* Status Value Returned by fscanf - Either Success or EOF */
  105.  
  106. 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 */
  107. 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 */
  108. }
  109. else
  110. {
  111. 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 */
  112.  
  113. 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. */
  114. /* You can see that as h is reduced and the number of trapeziums increases, we will get a better estimate. */
  115. N = (X_Final - X_Initial) / ( Height ) + ( 0.5 ); /* Calculating The Value N */
  116.  
  117. X_a = X_Initial; /* X_a = Xi - Used in The Area_i Calculation */
  118. Area_Total = 0;
  119.  
  120. for ( int i = 0 ; i < N ; i++){
  121.  
  122. X_b = X_a + Height; /* X_b = Xi + h - (X_initial + Height) Used in The Area_i Calculation */
  123.  
  124. 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 */
  125.  
  126. 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. */
  127.  
  128. X_a = X_b;
  129.  
  130. }
  131.  
  132. 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 */
  133.  
  134. }
  135.  
  136. }
  137.  
  138. 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 */
  139.  
  140. }
  141.  
  142. fclose(inp); /* Closes The File */
  143.  
  144. printf("\n"); /* Creates New Line - Purpose to Make the End Result Clearer */
  145.  
  146. return 0;
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement