Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
7069 Discussions

How to make an analogue of the polyfit() function from Matlab using OneMKL

Serjio
New Contributor I
1,997 Views

Hello.

 

I need to make a function that performs the most suitable (in the sense of least squares) polynomial in one variable from Cartesian coordinates.

There is such a function in Matlab - it is called polyfit().
I know that OneMKL has a set of functions that do piecewise linear interpolation using splines.

I cannot figure out how to use OnMKL to make an analogue of the from Matlab's polyfit() function?

0 Kudos
1 Solution
mecej4
Honored Contributor III
1,955 Views

There is C code in an earlier post in this forum for using LapackE-GELS to fit a polynomial in two independent variables, x and y. You can adapt that code by simply removing the portions that contain y and adjusting the indices of the regression matrix A to match.

View solution in original post

0 Kudos
4 Replies
mecej4
Honored Contributor III
1,990 Views

Given m-vectors x and y, to fit a polynomial of degree n, form the m X (n+1) Vandermonde matrix, and call MKL/Lapack GELS.

0 Kudos
Serjio
New Contributor I
1,964 Views

Hi, mecej4.

Thanks for your reply.

I understood, I need to use MKL/Lapack GELS. Ok.

But could you explain more detail please, how to use, for example dgets().

 

In my opinion, I should send the input data x() and y() and receive in response the coefficients of the polynomial C0, C1. I don't understand where I should insert my x() and y() data. 

 

// Function prototype
dgels( const char* trans, const MKL_INT* m, const MKL_INT* n,
       const MKL_INT* nrhs, double* a, const MKL_INT* lda, double* b,
       const MKL_INT* ldb, double* work, const MKL_INT* lwork,
       MKL_INT* info  );

// I have 2 vectors:
std::vector<double> x = {2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; // the size is variable
std::vector<double> y = {10.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0};

// Prepare data for dgels()
#define PolynomOrder 1
int m = x.size();         // number of rows of the matrix A
int n = PolynomOrder + 1; // number of of columns of the matrix A
int nrhs = 1;             // the number of columns in B
int lda = std::max(1, m);
int ldb = std::max(std::max(1, m), n);
int lwork = std::min(m, n)+ std::max(std::max(1, m), std::max(n, nrhs));
double work;              // What is this?
int info = 0;

// ...form the Vandermonde matrix - mX(n+1) | How to do it?



 

 

 

0 Kudos
mecej4
Honored Contributor III
1,956 Views

There is C code in an earlier post in this forum for using LapackE-GELS to fit a polynomial in two independent variables, x and y. You can adapt that code by simply removing the portions that contain y and adjusting the indices of the regression matrix A to match.

0 Kudos
MRajesh_intel
Moderator
1,914 Views

  

Hi,

 

Thanks for accepting as a Solution.

 

As this issue has been resolved, we will no longer respond to this thread. If you require any additional assistance from Intel, please start a new thread. Any further interaction in this thread will be considered community only.

 

Have a Good day.

 

Regards

Rajesh

 

0 Kudos
Reply