Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Integrate a function between a and b
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Integrate a function between a and b

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    newe12
    wrote on 9 Jun 2015, 16:47 last edited by newe12 6 Oct 2015, 07:44
    #1

    Hi!

    I want to integrate a function between the lower boundary "a" and the upper boundary "b".

    I have got:

    header.h:
    inline double integrate(double(*f)(double), double a, double b)
    {
        double sum = 0;
        double n = 20;
        double result = (b-a)/n;
        for (double k = 1.; k < n-1; k+=1.)
        sum += f(a + k*result);
        return result * ((f(a) + f(b)) / 2 + sum);
    }
    ///
    and in mainwindow.cpp (in the void function):
              double cp = lineEdit->text().toDouble(&ok);
              double x;
              double a=.025;
              double b=.075;
              double function=1-(cp*(1-x)); 
              double integrateresult = integrate(function, a, b);
    

    But it does not integrate the function...

    Does someone see the mistake?
    Please share!
    Thanks in advance!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 9 Jun 2015, 17:50 last edited by
      #2

      integrate() expects a pointer to function and you are giving it a double called "function". Naming a variable "function" does not make it a function :)

      In this code "function" variable is not a function. It's a variable of type double and undefined value (undefined because you never gave value to x so the result is garbage).

      1 Reply Last reply
      0
      • N Offline
        N Offline
        newe12
        wrote on 10 Jun 2015, 07:26 last edited by newe12 6 Oct 2015, 07:28
        #3

        Hello @Chris-Kawa !

        Thanks for pointing out some problems.

        I have done some improvements and the following code compiles without any problem:
        in the header-file:

        inline double f(double x)
        {
             return x*x;
        }
        

        in mainwindow.cpp in the function which is emitted with a QPushButton:

                    double aB = .1;
                    double bB = 2;
                    int n = 10000;
                    double h = (bB-aB)/n;
                    double area = (f(aB)+f(bB))/2;
        
                    for (int i=1;i<n;i++)
                    {
                        area += f(aB+i*h);
                    }
                    QString resultString = "";
                    lineEdit->setText(resultString.setNum(area));
        

        Furthermore, it even calculates the "area" and gives the result "14033.3". This is unrealistically high and incorrect!

        Mathematica gives me for the integration of the same equation:
        integral_0.1^2 x^2 dx = 2.66633
        which is correct!

        If someone has a helpful idea, please share!

        K 1 Reply Last reply 10 Jun 2015, 07:39
        0
        • N Offline
          N Offline
          newe12
          wrote on 10 Jun 2015, 07:38 last edited by
          #4

          Solved!

          area = area*h; 
          

          was missing after the for-loop!

          Now the result is: 2.66633
          the same is with Mathematica: 2.66633
          (For math-friends: at least approximately, if not "too" many digits are compared; Nevertheless setting "n" from "n=10000" to a higher accuracy, the area will be approximated more correctly!)

          Thanks anyway! :-)

          1 Reply Last reply
          0
          • N newe12
            10 Jun 2015, 07:26

            Hello @Chris-Kawa !

            Thanks for pointing out some problems.

            I have done some improvements and the following code compiles without any problem:
            in the header-file:

            inline double f(double x)
            {
                 return x*x;
            }
            

            in mainwindow.cpp in the function which is emitted with a QPushButton:

                        double aB = .1;
                        double bB = 2;
                        int n = 10000;
                        double h = (bB-aB)/n;
                        double area = (f(aB)+f(bB))/2;
            
                        for (int i=1;i<n;i++)
                        {
                            area += f(aB+i*h);
                        }
                        QString resultString = "";
                        lineEdit->setText(resultString.setNum(area));
            

            Furthermore, it even calculates the "area" and gives the result "14033.3". This is unrealistically high and incorrect!

            Mathematica gives me for the integration of the same equation:
            integral_0.1^2 x^2 dx = 2.66633
            which is correct!

            If someone has a helpful idea, please share!

            K Offline
            K Offline
            KiwiJeff
            wrote on 10 Jun 2015, 07:39 last edited by
            #5

            @newe12 said:

            double h = (bB-aB)/n;
            double area = (f(aB)+f(bB))/2;

            What is h for?
            And why are you starting the area with the average value of the two boundary points?

            Shouldn't it be more like:

             area += f(interval*i)
            

            where i goes from boundary to boundary and interval is .1?

            1 Reply Last reply
            1

            1/5

            9 Jun 2015, 16:47

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved