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. Need help with polynomial fitting
Forum Updated to NodeBB v4.3 + New Features

Need help with polynomial fitting

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 4 Posters 3.0k Views 1 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.
  • JohanSoloJ JohanSolo

    How is this related to Qt?
    Then, simply read the last line of the provided example, it prints out the equation.

    L Offline
    L Offline
    Loc888
    wrote on last edited by
    #3

    @JohanSolo Qt and C++, in this way.
    That's the code, i tried for a week, if i was able to do it by my way, i would do that days ago. I found some other, and they all print out some matrix values, instead of fitted Y axis.

    #include <QCoreApplication>
    #include<iostream>
    #include<iomanip>
    #include<cmath>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        QCoreApplication app(argc, argv);
    
        int i,j,k,n,N;
        cout.precision(4);                        //set precision
        cout.setf(ios::fixed);
        cout<<"\nEnter the no. of data pairs to be entered:\n";        //To find the size of arrays that will store x,y, and z values
        cin>>N;
        double x[N],y[N];
        cout<<"\nEnter the x-axis values:\n";                //Input x-values
        for (i=0;i<N;i++)
            cin>>x[i];
        cout<<"\nEnter the y-axis values:\n";                //Input y-values
        for (i=0;i<N;i++)
            cin>>y[i];
        cout<<"\nWhat degree of Polynomial do you want to use for the fit?\n";
        cin>>n;                                // n is the degree of Polynomial
        double X[2*n+1];                        //Array that will store the values of sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2n)
        for (i=0;i<2*n+1;i++)
        {
            X[i]=0;
            for (j=0;j<N;j++)
                X[i]=X[i]+pow(x[j],i);        //consecutive positions of the array will store N,sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2n)
        }
        double B[n+1][n+2],a[n+1];            //B is the Normal matrix(augmented) that will store the equations, 'a' is for value of the final coefficients
        for (i=0;i<=n;i++)
            for (j=0;j<=n;j++)
                B[i][j]=X[i+j];            //Build the Normal matrix by storing the corresponding coefficients at the right positions except the last column of the matrix
        double Y[n+1];                    //Array to store the values of sigma(yi),sigma(xi*yi),sigma(xi^2*yi)...sigma(xi^n*yi)
        for (i=0;i<n+1;i++)
        {
            Y[i]=0;
            for (j=0;j<N;j++)
            Y[i]=Y[i]+pow(x[j],i)*y[j];        //consecutive positions will store sigma(yi),sigma(xi*yi),sigma(xi^2*yi)...sigma(xi^n*yi)
        }
        for (i=0;i<=n;i++)
            B[i][n+1]=Y[i];                //load the values of Y as the last column of B(Normal Matrix but augmented)
        n=n+1;                //n is made n+1 because the Gaussian Elimination part below was for n equations, but here n is the degree of polynomial and for n degree we get n+1 equations
        cout<<"\nThe Normal(Augmented Matrix) is as follows:\n";
        for (i=0;i<n;i++)            //print the Normal-augmented matrix
        {
            for (j=0;j<=n;j++)
                cout<<B[i][j]<<setw(16);
            cout<<"\n";
        }
        for (i=0;i<n;i++)                    //From now Gaussian Elimination starts(can be ignored) to solve the set of linear equations (Pivotisation)
            for (k=i+1;k<n;k++)
                if (B[i][i]<B[k][i])
                    for (j=0;j<=n;j++)
                    {
                        double temp=B[i][j];
                        B[i][j]=B[k][j];
                        B[k][j]=temp;
                    }
    
        for (i=0;i<n-1;i++)            //loop to perform the gauss elimination
            for (k=i+1;k<n;k++)
                {
                    double t=B[k][i]/B[i][i];
                    for (j=0;j<=n;j++)
                        B[k][j]=B[k][j]-t*B[i][j];    //make the elements below the pivot elements equal to zero or elimnate the variables
                }
        for (i=n-1;i>=0;i--)                //back-substitution
        {                        //x is an array whose values correspond to the values of x,y,z..
            a[i]=B[i][n];                //make the variable to be calculated equal to the rhs of the last equation
            for (j=0;j<n;j++)
                if (j!=i)            //then subtract all the lhs values except the coefficient of the variable whose value                                   is being calculated
                    a[i]=a[i]-B[i][j]*a[j];
            a[i]=a[i]/B[i][i];            //now finally divide the rhs by the coefficient of the variable to be calculated
        }
        cout<<"\nThe values of the coefficients are as follows:\n";
        for (i=0;i<n;i++)
            cout<<"x^"<<i<<"="<<a[i]<<endl;            // Print the values of x^0,x^1,x^2,x^3,....
        cout<<"\nHence the fitted Polynomial is given by:\ny=";
        for (i=0;i<n;i++)
            cout<<" + ("<<a[i]<<")"<<"x^"<<i;
        cout<<"\n";
    
        return app.exec();
    }
    
    
    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #4

      If you have the polynomial coefficients and the desired x value(s), then your "problem" is just to evaluate a polynomial at a given x value... This has nothing to do with Qt or C++: it's an elementary school math issue.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      L 1 Reply Last reply
      3
      • JohanSoloJ JohanSolo

        If you have the polynomial coefficients and the desired x value(s), then your "problem" is just to evaluate a polynomial at a given x value... This has nothing to do with Qt or C++: it's an elementary school math issue.

        L Offline
        L Offline
        Loc888
        wrote on last edited by
        #5

        @JohanSolo Show me then, what has to be done.

        1 Reply Last reply
        -1
        • JohanSoloJ Offline
          JohanSoloJ Offline
          JohanSolo
          wrote on last edited by
          #6

          How did you come to fit a polynomial if you don't know how to evaluate it? Fitting correctly a polynomial is way more difficult than evaluating it... Basically you created a rocket to go to the moon but you don't know how to start the engines.
          Anyway, you could look at this.

          `They did not know it was impossible, so they did it.'
          -- Mark Twain

          L 1 Reply Last reply
          2
          • JohanSoloJ JohanSolo

            How did you come to fit a polynomial if you don't know how to evaluate it? Fitting correctly a polynomial is way more difficult than evaluating it... Basically you created a rocket to go to the moon but you don't know how to start the engines.
            Anyway, you could look at this.

            L Offline
            L Offline
            Loc888
            wrote on last edited by Loc888
            #7

            @JohanSolo This will not help, at that point, i can pay someone to do that, will cost me much more less then learning it. How much you want, to correct this? My time is out for this, and i has to do it.

            Btw. If i was able to do it alone, then why would i look for any already created algorithm?

            jsulmJ 1 Reply Last reply
            -1
            • L Loc888

              @JohanSolo This will not help, at that point, i can pay someone to do that, will cost me much more less then learning it. How much you want, to correct this? My time is out for this, and i has to do it.

              Btw. If i was able to do it alone, then why would i look for any already created algorithm?

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @Loc888 But why do you ask in a Qt forum? Your question does not have ANY relation to Qt.
              Why don't you ask in a forum more related to math and algorithms?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              L 1 Reply Last reply
              2
              • jsulmJ jsulm

                @Loc888 But why do you ask in a Qt forum? Your question does not have ANY relation to Qt.
                Why don't you ask in a forum more related to math and algorithms?

                L Offline
                L Offline
                Loc888
                wrote on last edited by
                #9

                @jsulm I tried, but every time i ask a question, and add "c++", guess how many responses i get? Yes, 0.
                If you are that worried about the forum, i can delete the topic later, so it will not litter it. I just need help, because i am out of patience, and out of time, and i am more frustrated, because i know someone knows how to do it, but he will not help, because it's too easy, and i should do it alone, i tried, many times, and i didn't achieve what i need.

                jsulmJ J.HilkJ 2 Replies Last reply
                0
                • L Loc888

                  @jsulm I tried, but every time i ask a question, and add "c++", guess how many responses i get? Yes, 0.
                  If you are that worried about the forum, i can delete the topic later, so it will not litter it. I just need help, because i am out of patience, and out of time, and i am more frustrated, because i know someone knows how to do it, but he will not help, because it's too easy, and i should do it alone, i tried, many times, and i didn't achieve what i need.

                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #10

                  @Loc888 I don't say you should not ask here. But don't forget that this is Qt forum and questions should be related to Qt. There is one forum though which would be better for your question than this one: https://forum.qt.io/category/34/c-gurus
                  And the fact that you're frustrated and out of patience will not help, people here are volunteers and do not have any obligation to help you.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • L Loc888

                    @jsulm I tried, but every time i ask a question, and add "c++", guess how many responses i get? Yes, 0.
                    If you are that worried about the forum, i can delete the topic later, so it will not litter it. I just need help, because i am out of patience, and out of time, and i am more frustrated, because i know someone knows how to do it, but he will not help, because it's too easy, and i should do it alone, i tried, many times, and i didn't achieve what i need.

                    J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #11

                    @Loc888

                    There are two types of people:

                    1. those that can extrapolate from incomplete data.

                    You should be part of the second group.

                    It's very unlikely that you will find someone that does all the work for you.

                    First of you should know the math behind what you're actually trying to do. Math forums are the way to go, if you don't have a teacher or books at hand.

                    Step two is translating your math knowledge to code (c++ in your case)

                    As step 3 somewhere, Maybe Qt comes into play. so far you do not use anything related to Qt.

                    But you could, Qt has QVecter2d and QVecter3D classes and QMatrix class for vector&matrix operations that could be useful in adopting a programmatically solution.


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    L 1 Reply Last reply
                    1
                    • L Offline
                      L Offline
                      Loc888
                      wrote on last edited by
                      #12

                      I didn't ask for the whole work, i asked if you can correct the part, of the matrix, that's it.

                      1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @Loc888

                        There are two types of people:

                        1. those that can extrapolate from incomplete data.

                        You should be part of the second group.

                        It's very unlikely that you will find someone that does all the work for you.

                        First of you should know the math behind what you're actually trying to do. Math forums are the way to go, if you don't have a teacher or books at hand.

                        Step two is translating your math knowledge to code (c++ in your case)

                        As step 3 somewhere, Maybe Qt comes into play. so far you do not use anything related to Qt.

                        But you could, Qt has QVecter2d and QVecter3D classes and QMatrix class for vector&matrix operations that could be useful in adopting a programmatically solution.

                        L Offline
                        L Offline
                        Loc888
                        wrote on last edited by Loc888
                        #13

                        @J.Hilk At least can you help me with c++17 compiler? I need a minGw, i downloaded Qt 5.8 with MinGw 5.30, but when i run it, it has instant crash.

                        jsulmJ 1 Reply Last reply
                        0
                        • L Loc888

                          @J.Hilk At least can you help me with c++17 compiler? I need a minGw, i downloaded Qt 5.8 with MinGw 5.30, but when i run it, it has instant crash.

                          jsulmJ Online
                          jsulmJ Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by jsulm
                          #14

                          @Loc888 Please provide more information, else all others can do is guessing. What does crash? The compiler? Your app? How are you building? When it crashes: what is the error?
                          Also, if you need c++17 you better install MinGW 7.3, see https://gcc.gnu.org/projects/cxx-status.html

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          L 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @Loc888 Please provide more information, else all others can do is guessing. What does crash? The compiler? Your app? How are you building? When it crashes: what is the error?
                            Also, if you need c++17 you better install MinGW 7.3, see https://gcc.gnu.org/projects/cxx-status.html

                            L Offline
                            L Offline
                            Loc888
                            wrote on last edited by
                            #15

                            @jsulm My GPU died, and i work with a integrated GPU< it's an driver issue, because i am rly out of time, i need anything with c++17. Does that QT creator and QT 5.2.2 support it? What version of MinGw i have to get?

                            jsulmJ 1 Reply Last reply
                            0
                            • L Loc888

                              @jsulm My GPU died, and i work with a integrated GPU< it's an driver issue, because i am rly out of time, i need anything with c++17. Does that QT creator and QT 5.2.2 support it? What version of MinGw i have to get?

                              jsulmJ Online
                              jsulmJ Online
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #16

                              @Loc888 C++17 is a C++ standard, so the C++ compiler has to support it not Qt and not QtCreator. You should install latest available GCC. In Qt Maintenance tool you can select MinGW 7.3, this one should cover most parts of C++17. See https://gcc.gnu.org/projects/cxx-status.html

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              L 1 Reply Last reply
                              1
                              • jsulmJ jsulm

                                @Loc888 C++17 is a C++ standard, so the C++ compiler has to support it not Qt and not QtCreator. You should install latest available GCC. In Qt Maintenance tool you can select MinGW 7.3, this one should cover most parts of C++17. See https://gcc.gnu.org/projects/cxx-status.html

                                L Offline
                                L Offline
                                Loc888
                                wrote on last edited by Loc888
                                #17

                                @jsulm I dowanloaded version 8 or something, and it's not compatible with Qt

                                skipping incompatible \lib\libQt5Cored.a when searching for -lQt5Cored

                                Is there anything already compiled? I can't compile it by my way.
                                This could work? https://mingw-w64.org/doku.php/download/cygwin

                                jsulmJ 1 Reply Last reply
                                0
                                • L Loc888

                                  @jsulm I dowanloaded version 8 or something, and it's not compatible with Qt

                                  skipping incompatible \lib\libQt5Cored.a when searching for -lQt5Cored

                                  Is there anything already compiled? I can't compile it by my way.
                                  This could work? https://mingw-w64.org/doku.php/download/cygwin

                                  jsulmJ Online
                                  jsulmJ Online
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #18

                                  @Loc888 You should simply install MinGW 7.3 through Qt Maintenance Tool together with Qt for MinGW 7.3 - this will work.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0

                                  • Login

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