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 9016569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:01:38+00:00 2026-06-16T04:01:38+00:00

I am trying to write a basic function to add some lines of best

  • 0

I am trying to write a basic function to add some lines of best fit to plots using nls.
This works fine unless the data just happens to be defined exactly by the formula passed to nls. I’m aware of the issues and that this is documented behaviour as reported here.

My question though is how can I get around this and force a line of best fit to be plotted regardless of the data exactly being described by the model? Is there a way to detect the data matches exactly and plot the perfectly fitting curve? My current dodgy solution is:

#test data
x <- 1:10
y <- x^2
plot(x, y, pch=20)

# polynomial line of best fit
f <- function(x,a,b,d) {(a*x^2) + (b*x) + d}
fit <- nls(y ~ f(x,a,b,d), start = c(a=1, b=1, d=1)) 
co <- coef(fit)
curve(f(x, a=co[1], b=co[2], d=co[3]), add = TRUE, col="red", lwd=2) 

Which fails with the error:

Error in nls(y ~ f(x, a, b, d), start = c(a = 1, b = 1, d = 1)) : 
  singular gradient

The easy fix I apply is to jitter the data slightly, but this seems a bit destructive and hackish.

# the above code works after doing...
y <- jitter(x^2)

Is there a better way?

  • 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-16T04:01:39+00:00Added an answer on June 16, 2026 at 4:01 am

    Use Levenberg-Marquardt.

    x <- 1:10
    y <- x^2
    
    f <- function(x,a,b,d) {(a*x^2) + (b*x) + d}
    fit <- nls(y ~ f(x,a,b,d), start = c(a=1, b=0, d=0)) 
    
    Error in nls(y ~ f(x, a, b, d), start = c(a = 1, b = 0, d = 0)) : 
      number of iterations exceeded maximum of 50
    
    library(minpack.lm)
    fit <- nlsLM(y ~ f(x,a,b,d), start = c(a=1, b=0, d=0))
    summary(fit)
    
    Formula: y ~ f(x, a, b, d)
    
    Parameters:
      Estimate Std. Error t value Pr(>|t|)    
    a        1          0     Inf   <2e-16 ***
    b        0          0      NA       NA    
    d        0          0      NA       NA    
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
    
    Residual standard error: 0 on 7 degrees of freedom
    
    Number of iterations to convergence: 1 
    Achieved convergence tolerance: 1.49e-08
    

    Note that I had to adjust the starting values and the result is sensitive to starting values.

    fit <- nlsLM(y ~ f(x,a,b,d), start = c(a=1, b=0.1, d=0.1))
    
    Parameters:
        Estimate Std. Error    t value Pr(>|t|)    
    a  1.000e+00  2.083e-09  4.800e+08  < 2e-16 ***
    b -7.693e-08  1.491e-08 -5.160e+00  0.00131 ** 
    d  1.450e-07  1.412e-08  1.027e+01  1.8e-05 ***
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
    
    Residual standard error: 6.191e-08 on 7 degrees of freedom
    
    Number of iterations to convergence: 3 
    Achieved convergence tolerance: 1.49e-08 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple game to teach some basic Welsh using the
I'm trying to write a php function that gets all the basic food data.
Apoligies for my basic understanding of jQuery. I am trying to write a function
I'm trying to write a basic program using DotNetOpenAuth to search for people on
So I'm trying to write a basic red black tree in c++. This is
I'm trying to write a basic image swap with javascript/jquery. For some reason, my
I was trying to write this basic JavaScript program which changes the background color
I'm trying to write a basic drawing widget using the Tkinter library. The very
I'm trying to write basic assert test: def assert_session_has ( sessionvar ) return assert_not_nil
I'm trying to write a basic script which creates a UI. I have reduced

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.