topic The ?trnlsp solvers are in IntelĀ® oneAPI Math Kernel Library
https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016018#M19489
<P>The ?trnlsp solvers are intended to minimize the euclidean norm, and are not likely to work with the absolute norm. Furthermore, you have not written code to handle the RCI request code 2, which requires you to evaluate the Jacobian.</P>Thu, 15 May 2014 21:57:57 GMTmecej42014-05-15T21:57:57Zstrnlsq related routines for nonlinear fitting
https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016017#M19488
<P style="font-size: 12px;">I want to solve a nonlinear least square fitting problem to estimate a and b. g(x)=a(x^2)+bx. I have two vectors working as the training data. fx1={1.0, 2.0, 3.0, 4.0}; fx2={2.0, 4.0, 6.0, 8.0}. I defined my objective function as f(x) = |g(fx1)-fx2|. It's obvious that the optimal a and b, in this case, should be 0 and 2 respectively, to minimize the objective function.</P>
<P style="font-size: 12px;">I used the MKL ?trnlsp related routines without boundary constraints to conduct the fitting. However, it didn't work. My code and the result are attached. It would be great if you guys can help to give some suggestions. Thanks.</P>
<PRE class="brush:cpp;">#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <complex>
#include "mkl_rci.h"
#include "mkl_types.h"
#include "mkl_service.h"
using namespace std;
float fx1[4] = {1.0, 2.0, 3.0, 4.0};
float fy1[4] = {2.0, 4.0, 6.0, 8.0};
int main(){
extern void rss(MKL_INT *, MKL_INT *, float *, float *);
MKL_INT n = 2, m = 4;
float eps[6];
float *x = NULL;
MKL_INT iter1 = 1000, iter2 = 100;
float rs = 1.0;
MKL_INT RCI_Request; // reverse communication interface variable
MKL_INT successful;
float *fvec = NULL;
float *fjac = NULL;
MKL_INT iter;
MKL_INT st_cr;
float r1, r2;
_TRNSP_HANDLE_t handle; // TR solver handle
MKL_INT i;
MKL_INT error;
MKL_INT info[6];
x = (float *) malloc (sizeof (float) * n);
fvec = (float *) malloc (sizeof (float) * m);
fjac = (float *) malloc (sizeof (float) * m * n);
for (i = 0; i < 6; i++)
{
eps<I> = 0.00001;
}
x[0] = 1.0;
x[1] = 0.0;
for (i = 0; i < m; i++)
fvec<I> = 0.0;
for (i = 0; i < m * n; i++)
fjac<I> = 0.0;
strnlsp_init (&handle, &n, &m, x, eps, &iter1, &iter2, &rs);
RCI_Request = 0;
successful = 0;
while (successful == 0)
{
strnlsp_solve (&handle, fvec, fjac, &RCI_Request);
if (RCI_Request == -1 ||
RCI_Request == -2 ||
RCI_Request == -3 ||
RCI_Request == -4 || RCI_Request == -5 || RCI_Request == -6)
successful = 1;
if (RCI_Request == 1)
{
rss (&m, &n, x, fvec);
}
cout << "RCI_Request = " << RCI_Request << endl;
cout << "successful = " << successful << endl;
}
strnlsp_get (&handle, &iter, &st_cr, &r1, &r2);
for(int i=0; i<n; i++){
cout << x<I> << endl;
}
cout << "iter = " << iter << endl;
strnlsp_delete (&handle);
free (fjac);
free (fvec);
free (x);
MKL_Free_Buffers ();
if (r2 < 0.00001)
{
printf ("Succeeded\n");
return 0;
}
else
{
printf ("Failed\n");
return 1;
}
}
/* nonlinear system equations without constraints */
/* routine for extendet powell function calculation
m in: dimension of function value
n in: number of function variables
x in: vector for function calculating
f out: function value f(x) */
void rss(MKL_INT * m, MKL_INT * n, float *x, float *f){
double temp;
for(int j=0; j<(*m); j++){
temp = x[0]*fx1<J>*fx1<J> + x[1]*fx1<J>;
f<J> = abs(temp - fy1<J>);
cout << "f = " << f<J> << endl;
}
return;
}
</J></J></J></J></J></J></I></I></I></I></PRE>
<P> </P>
<PRE class="brush:bash;">f = 1
f = 0
f = 3
f = 8
RCI_Request = 1
successful = 0
RCI_Request = 2
successful = 0
RCI_Request = -4
successful = 1
1
0
iter = 0
Failed</PRE>
<P> </P>Thu, 15 May 2014 20:05:11 GMThttps://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016017#M19488Cheng_C_2014-05-15T20:05:11ZThe ?trnlsp solvers are
https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016018#M19489
<P>The ?trnlsp solvers are intended to minimize the euclidean norm, and are not likely to work with the absolute norm. Furthermore, you have not written code to handle the RCI request code 2, which requires you to evaluate the Jacobian.</P>Thu, 15 May 2014 21:57:57 GMThttps://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016018#M19489mecej42014-05-15T21:57:57ZThanks very much for the
https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016019#M19490
<P>Thanks very much for the reply! Your suggestion works.</P>
<P>I simply added the following sentences and it produced a reasonable result, though not as accurate as I expect, maybe because of the form of the objective function as you pointed out.</P>
<PRE class="brush:cpp;"> if (RCI_Request == 2){
sjacobi (&rss, &n, &m, fjac, x, eps);
} </PRE>
<P> </P>Thu, 15 May 2014 22:27:07 GMThttps://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016019#M19490Cheng_C_2014-05-15T22:27:07Z