Need help with polynomial fitting
-
As the title suggest, i found this algorithm there:
https://www.bragitoff.com/2018/06/polynomial-fitting-c-program/But it's not what i want, because i need to get back the fitted values from the function, like here:
https://tinypic.pl/dxqmk52uj2qt
Instead, that's what i get back:
https://tinypic.pl/04257o7blkm3
I lost more than few days, i didn't find anything, that would return just the fitted values.
If someone knows where to get something like this, i will appreciate it, but it has to be polynomial, i tried other curves, but it will not work how i want. -
How is this related to Qt?
Then, simply read the last line of the provided example, it prints out the equation.@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(); }
-
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.
@JohanSolo Show me then, what has to be done.
-
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. -
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.@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?
-
@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?
-
@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?@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. -
@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.@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. -
@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.There are two types of people:
- 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.
-
There are two types of people:
- 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.
-
@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.
@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 -
@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 -
@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?
@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
-
@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
@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 -
@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