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

Problems using Nonlinear Least Squares Solver (without Constraints)

queinferno
Beginner
296 Views
Hi all,

I'm want to use the nonlinear solverof MKL but I couldn't acomplish my mission til now! I have readthe example offered with the documentation and some forumthreads, but there might still be somethingthat I didn't understand. I would appreciate some help.

My experimental code:
I tried to use the solver to fit something really easy. Let my function to be a curve with10 points (i=0:10)given by a*(i-5)^2 + b. My "true coefficients" are a = 5 and b = 6 and I give the solver a=8 and b = 10 as a first estimation. The finals coefficients I got were: a = 6.37 and b = 2.66. What's wrong?

Here my code:


bool

CurveFit::executeFitting(void)

{

int nCoefficientsSize = 2;

int nValuesSize = 10;

int nMaxNumberOfIterations = 1000000;

int nMaxNumberOfIterationsTrialStep = 100;

double dSizeOfTrueRegion = 100;

double coefficients[2] = {8,10};

double stopCriteriaPrecisions[6] = {0.00001, 0.00001, 0.00001, 0.00001, 0.00001, 0.00001};

double values[10];

double jacobian[10];

_TRNSP_HANDLE_t handle;

int nOK = dtrnlsp_init(&handle, &nCoefficientsSize, &nValuesSize, &coefficients[0], &stopCriteriaPrecisions[0], &nMaxNumberOfIterations , &nMaxNumberOfIterationsTrialStep, &dSizeOfTrueRegion);

memset(&values[0], 0, nValuesSize*sizeof(double));

memset(&jacobian[0], 0, nValuesSize*nCoefficientsSize*sizeof(double));

int nRCI_Request = 0;

while( nRCI_Request >= 0)

{

if (nRCI_Request == 2)

{

djacobi (extended_powell, &nCoefficientsSize, &nValuesSize, &jacobian[0], &coefficients[0], &stopCriteriaPrecisions[0]);

}

else if(nRCI_Request == 1)

{

extended_powell (&nValuesSize, &nCoefficientsSize, &coefficients[0], &values[0]);

}

nOK = dtrnlsp_solve(&handle, &values[0], &jacobian[0], &nRCI_Request);

}

int nNumberOfIterations, nStopCriteriaValue;

double nFirstResidual, nFinalResidual;

nOK = dtrnlsp_get(&handle, &nNumberOfIterations, &nStopCriteriaValue, &nFirstResidual, &nFinalResidual);

nOK = dtrnlsp_delete(&handle);

return true;

}

/* routine for extended powell function calculation

m in: dimension of function value

n in: number of function variables

x in: vector for function calculation

f out: function value f(x) */

void CurveFit::extended_powell (int *m, int *n, double *x, double *f)

{

double a = *x;

double b = *(x+1);

double a0 = 5;

double b0 = 6;

int nSize = *m;

double dCenter = 5;

double dist, yValue, fValue;

for (int i = 0; i < nSize; i++)

{

dist = i - dCenter;

yValue = a0*dist*dist + b0*dist;

fValue = a*dist*dist + b*dist;

*(f + i) = yValue - fValue;

}

return;

}

thank you, Vanessa

0 Kudos
1 Reply
queinferno
Beginner
296 Views
my mistake!!! :-/
the jacobian vector has wrong size....
0 Kudos
Reply