-1

I am both tired, new to C++ and real bad at dealing with polynomials. That's a bad combo for my assignment. Nevertheless I am trying to solve it. Please note that I might have misunderstood certain parts both mathematically and language-wise. Maybe even terminology.

The first task of my assignment was to create a class for storing polynomials. I figured the important parts were coefficiants and the degree of the polynomial. As such I have a polynomial class that (partly) looks like this:

class Polynomial {

private:
    double* Coefficients;  //Array of coefficients in order of ascending power
    int Degree; //The degree of the polynomial

...

The class should have a method for finding the integral of the polynomial within a lower and upper bound. But I really do not know how to work with it.

Since it's bad practise not to show what I've done, this is what I currently have, and it probably does not make a lot of sense, but please, if you can, point me in the right direction?

Polynomial Polynomial::ComputeIntegral(double lower, double upper) {
    //Values needed to create new polynomial
    //degree will be one more than the original polynomial
    int degree = Degree + 1;
    double* coefficients = new double[degree + 1];

    coefficients[0] = 0;

    for (int i = 0; i < degree +1; i++) {
        coefficients[i + 1] = Coefficients[i] / (double)(i + 1);
    }

    Polynomial integral(degree, coefficients);
    return integral;
}

That I can see myself, it is messed up because a) I do not use the bounds, and b) I am pretty sure per the assignment description I should end up with a value rather than a new polynomial.

Google tells me there are algorithms to deal with finding integrals (Trapezoid for example), but I can not wrap my head around matching that with my representation of a polynomial.

Treeline
  • 475
  • 1
  • 8
  • 23
  • 2
    Hint: how would you integrate a polynomial with paper and pencil? – Mitch Wheat Jun 05 '15 at 00:50
  • Ehhr. I'm not sure how to answer that, except "As I tried to integrate with the algorithm". But again, i'm quite sure i have sidetracked myself in my solution. I have tried to translate what I know into C++, but again, polynomials was something I struggled with back in school, and C++ is new to me. – Treeline Jun 05 '15 at 01:10
  • If you don't know how to integrate a polynomial with paper and pencil, then how are you going to implement it! – Mitch Wheat Jun 05 '15 at 01:11
  • I am well aware that you are telling me to go read up on the topic of integration, rather than trying to code. What I am trying to say is that I am unable to get any further than I am now. My problem is that I do not know what exactly I am missing. I was hoping someone would take a look at the code, and be able to see what I am misunderstanding exactly. – Treeline Jun 05 '15 at 01:18

2 Answers2

3

A few pointers:

  1. Use std::vectors instead of pointers and new. (If you are new to C++, there are very few circumstances when you actually need to use new.)
  2. ComputeIntegral(double, double) will need to return a double, since it is obviously computing a definite integral. (The function you have at the moment would be something like GetPrimitive(), as it returns the primitive of the polynomial, which is another poly.
  3. The definite integral is the difference of the primitive evaluated at the bounds (First Fundamental theorem of calculus).
  4. There are a number of ways you could represent the polynomial as a data structure, but I would suggest a single std::vector<double> coeffs that represents all of the coefficients up to the degree of the poly, then the degree can be calculated at coeffs.size(). In some cases there may be zeroes in that coeffs though.
tahsmith
  • 1,643
  • 1
  • 17
  • 23
0

In the general case, it is possible to use the boost library to compute integrals: https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/quadrature.html

There is also the library ALGLIB that I have used to compute integrals. Here is an example to compute integrals with ALGLIB: Integrate a public but non static member function with ALGLIB

Dharman
  • 30,962
  • 25
  • 85
  • 135
mistral
  • 35
  • 7