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

  • Home
  • SEARCH
  • 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 970799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:51:37+00:00 2026-05-16T02:51:37+00:00

I am trying to fit collected data to a polynomial equation and I found

  • 0

I am trying to fit collected data to a polynomial equation and I found the lfit function from Numerical Recipes. I only have access to the second edition, so am using that.

I have read about the lfit function and its parameters, one of which is a function pointer, given in the documentation as

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

with the help

The user supplies a routine funcs(x,afunc,ma) that returns the ma basis functions evaluated at x = x in the array afunc[1..ma].

I am struggling to understand how this lfit function works. An example function I found is given below:

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;
}

When I run through the source code for the lfit function in gdb I can see no reference to the funcs pointer. When I try and fit a simple data set with the function, I get the following error message.

Numerical Recipes run-time error...
gaussj: Singular Matrix
...now exiting to system...

Clearly somehow a matrix is getting defined with all zeroes.

I am going to involve this function fitting in a large loop so using another language is not really an option. Hence why I am planning on using C/C++.

For reference, the test program is given here:

int main()
{

    float x[5] = {0., 0., 1., 2., 3.};
    float y[5] = {0., 0., 1.2, 3.9, 7.5};
    float sig[5] = {1., 1., 1., 1., 1.};

    int ndat = 4;
    int ma = 4; /* parameters in equation */
    float a[5] = {1, 1, 1, 0.1, 1.5};
    int ia[5] = {1, 1, 1, 1, 1};
    float **covar = matrix(1, ma, 1, ma);

    float chisq = 0;

    lfit(x,y,sig,ndat,a,ia,ma,covar,&chisq,fpoly);

    printf("%f\n", chisq);  
    free_matrix(covar, 1, ma, 1, ma);


    return 0;
}

Also confusing the issue, all the Numerical Recipes functions are 1 array-indexed so if anyone has corrections to my array declarations let me know also!

Cheers

edit: Ok just discovered the problem to this.

When copying the numerical recipes code, I accidentally commented out some real code thinking it was just a comment. I no longer get the singular value error

  • 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-05-16T02:51:38+00:00Added an answer on May 16, 2026 at 2:51 am

    I have since switched to the 3rd edition of NR which has a more understandable routine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to fit a transformation from one set of coordinates to another.
I am trying to write a chi square goodness-of-fit test for Beta distribution from
Trying to honor a feature request from our customers, I'd like that my application,
I'm trying to create a very simple best-fit scheduling system. Everyone submits their free/busy
I am trying to implement a network core-periphery measure from an article (link: Borgatti
.. been trying to fit the textbox (auto adjust the textbox width) within HTML
Background I am trying to fit a distribution to a 95% CI and mode.
I'm trying to expand a div to fit text without having to specify an
so currently i'm trying to crop and resize a picture to fit it into
I'm trying to shrink the caption box to fit its text content. Here's what

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.