Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8850175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:50:22+00:00 2026-06-14T12:50:22+00:00

i am very new in c programming. i am using a c code from

  • 0

i am very new in c programming. i am using a c code from the book ‘numerical recipes in C’ for polynomial regression. in this program i need to replace (*funcs) function with fpoly function.but i dont know how to do that and how to make changes in fpoly function to become like (*fpoly).could you please help me with that?

I really appreciate any help.

void fpoly(float x, float p[], int np)
//Fitting routine for a polynomial of degree np-1, with coefficients in the array p[1..np].
{
    int j;
    p[1]=1.0;
    for (j=2;j<=np;j++) p[j]=p[j-1]*x;
}



void lfit( float x[], float y[], float sig[], int ndat, float a[], int ia[], int ma, float **covar, float *chisq, void (*funcs)(float, float [], int))

and here is the complete program:

void lfit(float x[], float y[], float sig[], int ndat, float a[], int ia[],
      int ma, float **covar, float *chisq, void (*funcs) (float,float[],  int))

/*Given a set of data points x[1..ndat], y[1..ndat] with individual standard deviations
sig[1..ndat], use χ2 minimization to fit for some or all of the coefficients a[1..ma] of
a function that depends linearly on a, y =sum(i)( ai × afunci(x)). The input array ia[1..ma]
indicates by nonzero entries those components of a that should be fitted for, and by zero entries
those components that should be held fixed at their input values. The program returns values
for a[1..ma], χ2 = chisq, and the covariance matrix covar[1..ma][1..ma]. (Parameters
held fixed will return zero covariances.)Th e user supplies a routine funcs(x,afunc,ma) that
returns the ma basis functions evaluated at x = x in the array afunc[1..ma].*/
{

    void covsrt(float **covar, int ma, int ia[], int mfit);
    void gaussj(float **a, int n, float **b, int m);

    int i, j, k, l, m, mfit = 0;
    float ym, wt, sum, sig2i, **beta, *afunc;

    beta = matrix(1, ma, 1, 1);
    afunc = vector(1, ma);

    for (j = 1; j <= ma; j++)
        if (ia[j])
          mfit++;
    if (mfit == 0)
       nrerror("lfit: no parameters to be fitted");

    for (j = 1; j <= mfit; j++) {   //Initialize the (symmetric)mat rix.
        for (k = 1; k <= mfit; k++)
            covar[j][k] = 0.0;
        beta[j][1] = 0.0;
    }

    for (i = 1; i <= ndat; i++) {
    (*funcs) (x[i], afunc, ma);
    ym = y[i];
    if (mfit < ma) {              //Subtract off dependences on known pieces
        for (j = 1; j <= ma; j++)     //of the fitting function.
        if (!ia[j])
            ym -= a[j] * afunc[j];
    }
    sig2i = 1.0 / SQR(sig[i]);
    for (j = 0, l = 1; l <= ma; l++) {
        if (ia[l]) {
        wt = afunc[l] * sig2i;
        for (j++, k = 0, m = 1; m <= l; m++)
            if (ia[m])
            covar[j][++k] += wt * afunc[m];
        beta[j][1] += ym * wt;
        }
    }
    }
    for (j = 2; j <= mfit; j++)      //Fill in above the diagonal from symmetry.
        for (k = 1; k < j; k++)
           covar[k][j] = covar[j][k];
    gaussj(covar, mfit, beta, 1);      //Matrix solution.
    for (j = 0, l = 1; l <= ma; l++)
         if (ia[l])
           a[l] = beta[++j][1];    //Partition solution to appropriate coefficients
    *chisq = 0.0;   

    for (i = 1; i <= ndat; i++) {      //Evaluate χ2 of the fit.
        (*funcs) (x[i], afunc, ma);
            for (sum = 0.0, j = 1; j <= ma; j++)
                sum += a[j] * afunc[j];
    *chisq += SQR((y[i] - sum) / sig[i]);
    }
    covsrt(covar, ma, ia, mfit);      //Sort covariance matrix to true order of fittin
    free_vector(afunc, 1, ma);    //coefficients.
    free_matrix(beta, 1, ma, 1, 1);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:50:23+00:00Added an answer on June 14, 2026 at 12:50 pm

    In

    void lfit( float [], float [], float [], int, float [], int [], int, float**, float*, void (*funcs)(float, float [], int))
    

    “void (*funcs)(float, float [], int)” is the type signature of the function pointer.
    If it is in-scope, you can just pass the name of your function (fpoly) in place of “void (*funcs)(float, float [], int)”, without parentheses or anything. You can also take its address with the & operator but I believe it’s equivalent:

    lfit( all_the_other_args, ..., fpoly);
    

    You could also have a local function pointer which holds fpoly:

    void (*local_function_pointer_variable)(float, float [], int) = fpoly;
    lfit( all_the_other_args, ..., local_function_pointer_variable);
    

    In C the syntax of function pointer types is somehow inconvenient but hopefully you can define a type to hide this to some extent

    typedef void (*poly_fitter)(float, float [], int);
    poly_fitter function_pointer_var_of_type_poly_fitter = fpoly;
    lfit( all_the_other_args, ..., function_pointer_var_of_type_poly_fitter)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to C programming. I'm using C code from the book
I'm very new to programming and I am trying to write a program that
I am very new to MPI programming, I am trying to run a program
I am very new to the C# programming language. I code primarily in PHP/MySQL
I am very new to programming and I can't seem to find a way
I'm still very new at programming, and our local SSIS genius isn't here today
I am very new to programming with jQuery and I am trying to retrieve
I am very new to programming in general, while reading about PHP I saw
I am very new to python programming and have yet to buy a textbook
I'm very much new to programming and have been doing fairly well so far.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.