Community
cancel
Showing results for
Did you mean:
Highlighted
Beginner
16 Views

## strnlsq related routines for nonlinear fitting

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.

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.

```#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 = {1.0, 2.0, 3.0, 4.0};
float fy1 = {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;
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;

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 = 0.00001;
}
x = 1.0;
x = 0.0;

for (i = 0; i < m; i++)
fvec = 0.0;
for (i = 0; i < m * n; i++)
fjac = 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)
{
}
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 << 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*fx1*fx1 + x*fx1;
f = abs(temp - fy1);
cout << "f = " << f << endl;
}

return;
}
```

```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```

2 Replies
Highlighted
Black Belt
16 Views

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.

Highlighted
Beginner
16 Views

``` if (RCI_Request == 2){