How can I compute an integral using C++?

//HERE'S WHAT I HAVE SO FAR.
//I DON'T KNOW HOW TO FINISH UP THE QUADRATURE FUNCTION.




using namespace std;
#include <iostream>
#include <cmath>

double function(double n);
double quadrature(double a, double b, int n);


int main(){

double a; // left edge of interval
double b; // right edge of interval
int n; // number of "slices"

// prompt user and read in a, b, and n
// read in a, b, and n.

double areaQuad = quadrature(a,b,n); // get quadrature answer
double areaTheo = (b*b*b - a*a*a) / 3; // theoretical answer
double absErr = abs(areaQuad - areaTheo); // absolute error
double relErr = absErr / abs(areaTheo); // relative error

cout << "\nArea computed: " << areaQuad;
cout << "\nArea theoretical: " << areaTheo;
cout << "\nAbsolute error: " << absErr;
cout << "\nRelative error: " << relErr;

return EXIT_SUCCESS;
}

//**************************************

double function(double x){

return x*x;
}

//**************************************

double quadrature(double a, double b, int n){

double h = (b-a)/n; // divides interval up into n equal subintervals
double sum = 0;

double


return sum;
}
Isn't that more of a math question rather than a C++ one?
I understand how to do it in math, but incorporating it into C++ is where I'm stuck.
Ok... for starters, how would you write down to steps to do it (mathematically)?
Yea, I seem to need templates and classes to do functional programming in C++. First, get an understanding of those. Then, we'll talk.

The other thing is it's really difficult for the computer to figure out what a limit should be, I'd guess, and since the derivative (I highly suggest you try this first, it's a bit easier to compute) and the integral rely on the limit, it's not an easy task to tell a computer how to do calculus.
Yea, I seem to need templates and classes to do functional programming in C++. First, get an understanding of those. Then, we'll talk.


This doesn't require functional programming. Like, at all. Also, functional programming is just a different paradigm, it doesn't add or remove from the things you can do (it just makes you go at problems somewhat differently).


The other thing is it's really difficult for the computer to figure out what a limit should be, I'd guess, and since the derivative (I highly suggest you try this first, it's a bit easier to compute) and the integral rely on the limit, it's not an easy task to tell a computer how to do calculus.


If the function is integrable, you can just approximate the value of it's integral over an interval, which is quite apparently what the OP's going for.
True, it doesn't require functional programming. It does require functional programming, though, to do this without having to rewrite the code for each function ... right?
Last edited on
I don't think you know what functional programming is.
http://en.wikipedia.org/wiki/Functional_programming
Last edited on
Effectively higher-order functions that can take functions as arguments and/or return functions, right? I'm primarily a mathematician, I'm quite used to it if I got the definition right.

Ohhh ... the OP is asking for the integral with bounds, not the antiderivative. :')
Last edited on
Nope, not everything that uses higher order functions is automatically functional. You can use higher order functions and still program more imperative than assembly.
Topic archived. No new replies allowed.