Question about integrating a linear function

I need some help with a line of code I just can't figure out how to deal with. I need to write a single line function that accepts 4 parameters m, b, x1, and xn that returns the definite integral of mx + b over the interval x1<= x <= xn. The code that I copied here isn't completely finished but I just wanted to write a rough draft, I just have no clue how to write the integration function. Any help would be appreciated greatly.


* Statement: Find the least squares line for the data in mp5.inp and
* integrate it from the first x value to the last x value.
* Specifications:
* Input - a sequence of (x, y) values from a sequential file
* Invokes- linreg to determine linear regression line and correlation coeff
* - integrate to determine the area under the curve
* Output - to a sequential file
* - the least squares line y = m*x + b
* - the correlation coefficient
* - the integral of m*x+b over the first x to the last
************************************************************************/

#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

// prototype of functions linreg & integrate

int main()
{
// numeric variable declarations
double m, b, firstx, lastx, r;

// file variable declarations and initialization
ifstream fin;
ofstream fout;

// 1) file variable initializations
fin.open("mp5.inp");
fout.open("mp5.out");

// 2) invoke functon linreg to calculate least squares line
linreg(fin, m, b, r, firstx, lastx);

// 3) display linear equation, correlation coefficient to the file
fout << fixed << setprecision(2)
<< setw(10) << "y = " << setw(6) << m
<< setw(10) << " * x + " << setw(6) << b << "\n";
fout << setw(26) << "Correlation Coefficient = "
<< setw(6) << r << endl;


// 4) invoke integrate and display result to the file

fout << setw(26) << "Integral = "
<< setw(6) << integrate(m,b,firstx,lastx) << endl;


// 5) disconnect from files
fin.close();
fout.close();
}

/*Function integrate
*
*receives - Slope m of least squares line
* - y intercept of least squares line
* - limits of integration
*returns - the antiderivative m*x^2/2 + b*x evaluated at x
*************************************************************************/
// Define function integrate below

integrate (double, double, double, double);

// 1) integrate a linear function

//NEED HELP HERE

/*Function linreg
*
*receives - input file object fin
*returns - Slope m of least squares line
* - y Intercept m of least squares line
* - Correlation coefficient r of least squares line
*************************************************************************/
// Define function linreg below

void linereg ( ifstream&, fin&, m&, b&, firstx&, lastx& );

// 1) reduction variable initialization

sum x = 0;
sum y = 0;
sum x*x = 0;
sum x*y = 0;
sum y*y = 0;
int n = 0;

// 2) loop forever

While fin.eof()
{


// 3) attempt to input an ordered pair

fin >> x >> y;

// 4) test for end of file

if fin.eof( )
{
// 5) leave when true
break;
}
// 6) test for first iteration

if ( n == 0 )
{

// 7) save lower limit of integration
x = firstx;
// 8) save upper limit of integration
} else {
x = lastx;
}
// 9) update reduction variables

sum x += x;
sum y += y;
sum x*x += x*x;
sum x*y += x*y;
sum y*y += y*y;
int n += 1;

}
// 10) calculate slope, y intercept and correlation coefficient

m = ( n *( sum x*y ) - (sum x) * (sum y)) / (n * sum x*x - sum x * sum x)

b =( sum y - m * sum x )/ n

p = (n * sum x * y - sum x *sum y) / (sqrt ( n* sum x*x - pow(sum x, 2))* (sqrt (n * sum y*y - pow(sum y, 2))
Integrate the function mx + b by hand to equal I for example.
Hard code the integral in terms of x in your function and return the value of I(x2) - I(x1)
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

double integral(int m, int b, int x1, int x2)
{
	return ((m/2)*(x2*x2) + b*x2)  - ((m/2)*(x1*x1) + b*x1);
}
int main()
{
	cout<<integral(2,1,0,2);	// f(x) = 2x + 1 :|0-2
	return 0;
}
thanks a lot, this is exactly what i was having difficulty putting together. Do I need to declare the function anywhere else?
Last edited on
I don't understand what you mean by any where else
The function is defined only once and can be called in several places.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

double integral(int m, int b, int x1, int x2)  //defined once
{
	return ((m/2)*(x2*x2) + b*x2)  - ((m/2)*(x1*x1) + b*x1);
}
int main()
{
	cout<<integral(2,1,0,2);	// f(x) = 2x + 1 :|0-2
        cout<<integral(4,5,2,5);  //called here
	return 0;
}
void testFunction()
{
       ......
      itn x =  integral(3,4,5,5); //called here again.
}
Oops sorry, thanks for clarifying.
Topic archived. No new replies allowed.