?trnlsq related routines for nonlinear fitting

?trnlsq 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[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;
}

 

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

 

publicaciones de 3 / 0 nuevos
Último envío
Para obtener más información sobre las optimizaciones del compilador, consulte el aviso sobre la optimización.

Hi,

It is look like you forget to implement case RCI_Request equal to 2 on which user need to calculate Jacobian of function. 

Thanks,

Alex

Cita:

Alexander Kalinkin (Intel) escribió:

Hi,

It is look like you forget to implement case RCI_Request equal to 2 on which user need to calculate Jacobian of function. 

Thanks,

Alex

Hi Alexander,

Thanks for the reply. You are right. I forgot to implement it.

Deje un comentario

Por favor inicie sesión para agregar un comentario. ¿No es socio? Únase ya