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

## How to make an analogue of the polyfit() function from Matlab using OneMKL New Contributor I
741 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?

1 Solution Black Belt
699 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.

4 Replies Black Belt
734 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. New Contributor I
708 Views

Hi, mecej4.

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?

`````` Black Belt
700 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. Moderator
658 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 