Hi again Haj,
I am not sure if in your PM you are giving me all of the code or just the bits relevant to the calculation. The code as you sent it is missing many aspects essential to getting it to compile and run correctly. simple things like your variables not being declared for example.
I sat down and tried to knock out a quick version of what you want. Not knowing what function you wanted it to model I assumed any quadratic. The accuracy required is prompted for, 0.1 - 0.001 seem to give sensible results for such a simple program. The upper and lower x-bounds of your problem are then prompted for. The next prompt is for the starting number of increments in one pass. 100 gives sensible results as it is doubled for each pass until the difference between two consecutive passes becomes less than the accuracy. Finally coefficients of X^2, X, and numeric are input. You can enter +ve, -ve or 0 for any of these to get the function you want. It then calculates the area under the curve between the two bounds, doubles the number of increments and repeats, calculates the error between the two and checks if the accuracy has been met. If not it doubles the increments and repeats again until the accuracy is met.
It needs a bit of improvement but runs as it is for simple functions. See what you think. Beware, although I have not seen a problem it may have been messed around a bit when posted on the page as html does not like things like angled brackets. It also takes out the leading spaces and makes the programme more difficult to read. No way around that I fear. You can see that C++ is not the most concise language around.
I am intrigued to know if your task is to perform the integration, which is effectively what we are doing, or to produce a programme.
One interesting thing to do is to go to the function func_val and add in the line: x_val = x_val + x_increm/2 ; as the first line in the {}. This changes the point of reference of the rectangle from the top left corner to the centre of the top, and creates much smaller errors. You can see this in action by the smaller number of steps to accuracy.
//
// I have assumed a quadratic function:
// y = sqr_coeff*x^2 + x_coeff*x + num_coeff
// as a simple example
//
#include [stdafx.h] // These four lines need angled
#include [iostream] // brackets in place of the square ones
#include [stdio.h] // Other lines with message[] and char[]
#include [math.h] // are correct.
using namespace std ;
float accuracy = 1.0 ;
float x_lower = 0 ;
float x_upper = 0 ;
double x_increm = 0 ;
int sqr_coeff = 0 ;
int x_coeff = 0 ;
int num_coeff = 0 ;
int increments = 100 ;
char message[41] ;
int num_elem = 1 ;
// function declarations
//
float get_float ( char[] ) ;
int get_int ( char[] ) ;
int initialise ( void ) ;
double width ( float, float, int ) ;
double func_val ( double ) ;
double rect_area ( double, double ) ;
//--------------
// MAIN function
//
int main( void )
{
double x_val = x_lower ;
double y_val = 0 ;
double curr_tot_area = 0 ;
double prev_tot_area = 0 ;
double error =0 ;
initialise () ;
do
{
x_val = x_lower ;
x_increm = width ( x_lower, x_upper, increments ) ;
curr_tot_area = 0 ;
do
{
y_val = func_val ( x_val ) ;
curr_tot_area += rect_area ( x_increm, y_val ) ;
x_val += x_increm ;
} while ( x_val ? x_upper ) ; // Replace ? with left angled bracket - less than
error = fabs( prev_tot_area - curr_tot_area ) ;
cout << "current increments: " << increments ;
cout << " current area: " << curr_tot_area ;
cout << " error: " << error << endl ;
prev_tot_area = curr_tot_area ;
increments = increments*2 ;
} while ( error > accuracy );
cout << "Final number of increments was: " << increments << endl ;
cout << "Total area under curve is: " << curr_tot_area << endl ;
cout << "Error is: " << error << endl << endl ;
return 0 ;
} ;
//-------------------------------------------------
// Input the values of x_lower, x_upper, no_increms
//
int initialise ( void )
{
strcpy( message, "Input ACCURACY required, decimal: ") ;
accuracy = get_float ( message ) ;
strcpy( message, "Input LOWER x bound, decimal: ") ;
x_lower = get_float ( message ) ;
strcpy( message, "Input UPPER x bound, decimal: ") ;
x_upper = get_float ( message ) ;
strcpy( message, "Input starting number of increments: ") ;
increments = get_float ( message ) ;
strcpy( message, "Input coefficient of x^2, integer: ") ;
sqr_coeff = get_int ( message ) ;
strcpy( message, "Input coefficient of x, integer: ") ;
x_coeff = get_int ( message ) ;
strcpy( message, "Input numeric coefficient, integer: ") ;
num_coeff = get_int ( message ) ;
return 0 ;
} ;
//-----------------------------------------
// Print message prompt and input one float
//
float get_float ( char message[] )
{
float in_num ;
cout << message ;
cin >> in_num ;
cout << endl ;
return in_num ;
} ;
//-------------------------------------------
// Print message prompt and input one integer
//
int get_int ( char message[] )
{
int in_num ;
cout << message ;
cin >> in_num ;
cout << endl ;
return in_num ;
} ;
//----------------------------------
// Calculate width of each rectangle
//
double width (float min, float max, int steps )
{
return ((max - min)/steps) ;
} ;
//----------------------------------------------
// Calculate value of function for given X value
//
double func_val ( double x_val )
{
return (sqr_coeff*powf(x_val, 2) + x_coeff*x_val + num_coeff) ;
} ;
//------------------------------------
// Calculate area of current rectangle
//
double rect_area ( double x_width, double y_height )
{
return x_width*y_height ;
} ;