My code is as follows:

#include <iostream>
#include <cmath>
using namespace std;

#define MATHLIB_STANDALONE 1

extern "C"
{
        #include "R_ext/Applic.h"
}

typedef struct TT{
        double ** tempX;
        double * tempY;
        int tempN;
} TT, *MM;

double fn(int N, double * beta, void * ex){
        double total = 0;
        int i = 0,j = 0;
        double * betaFn = new double[N];
        MM tmp = (MM)ex;
        for(i = 0; i < N; i++)
        {
                betaFn[i] = exp(beta[i]);
        }
        for(j = 0; j < tmp->tempN; j++) {
                double temp = 0;
                for(i = 0; i < N; i ++) {
                        temp += tmp->tempX[j][i] * betaFn[i];
                }
                total += pow(tmp->tempY[j] - temp,2);
        }
        return total;
}
void mgr(int, double *, double *, void *){}

int main()
{
        //cout<<pchisq(2,7,1,0)<<endl;
        //cout << qnorm(0.7, 0.0, 1.0, 0, 0) << endl;

        int n = 5;
        double *dpar, *opar;
        int fncount, fail;
        dpar = new double [n];
        opar = new double [n];
        dpar[0] = 13.67318;
        dpar[1] = 0;
        dpar[2] = 17.02707;
        dpar[3] = 0;
        dpar[4] = 24.08231;
        double value;
        double abstol = 1e-16;
        double intol = 1e-8;
        
        int y = 12;
        MM t = (MM)malloc(y*(n+2)*sizeof(TT));
        t-> tempX = new double * [y];
        for(size_t ii = 0; ii < y; ii++) {
                t->tempX[ii] = new double [n];
        }

        t->tempX[0][1] = -0.03;
        t->tempX[1][1] = -0.02;
        t->tempX[2][1] = -0.015;
        t->tempX[3][1] = -0.01;
        t->tempX[4][1] = -0.005;
        t->tempX[5][1] = 0;
        t->tempX[6][1] = 0.005;
        t->tempX[7][1] = 0.01;
        t->tempX[8][1] = 0.015;
        t->tempX[9][1] = 0.02;
        t->tempX[10][1] = 0.03;
        t->tempX[11][1] = 0.04;
        for(int ii = 0; ii < n; ii++) {
                for(int yy = 0; yy < y; yy++)
                        t->tempX[yy][ii] = pow(t->tempX[yy][1], ii);
        }

        t-> tempY = new double [y];
        t->tempY[0] = 930862;
        t->tempY[1] = 893736;
        t->tempY[2] = 882721;
        t->tempY[3] = 872066;
        t->tempY[4] = 871729;
        t->tempY[5] = 868550;
        t->tempY[6] = 865100;
        t->tempY[7] = 862132;
        t->tempY[8] = 870266;
        t->tempY[9] = 870493;
        t->tempY[10] = 893980;
        t->tempY[11] = 952914;

        t->tempN = y;

        cout << fn(n,dpar,(void*)t)<<endl;
        nmmin(n, dpar, opar, &value, fn,
           &fail, abstol, intol, (void *)t,
           1, 0.5, 2, 0,
           &fncount, 500);
        for(int aa = 0 ; aa < n; aa++) {
                cout<< opar[aa]<<",";
        }
        cout<<"\n";
        return 0;       
}



I run this code, It told me  "Segmentation fault". I dont know if the
parameter is wrong or not. 
Thank you very much.

DJade
-- 
View this message in context: 
http://www.nabble.com/R-help-for-invoking-nmmin%28%29-tp20258786p20258786.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to