MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Thread Subject: Nonlinear Curve Fitting Problem

 Subject: Nonlinear Curve Fitting Problem From: Anton Date: 21 Apr, 2010 20:24:04 Message: 1 of 9 Hello, I need to fit a curve very accurately. The function is n = ( k * sqrt(1 + x^2 / h^2)+phi ) / pi where k, h, phi are fitting parameters and the data is n = 2 3 4 5 6 7 x = 3.7910 4.0640 4.2850 4.4930 4.6750 4.8440 I tried doing a nonlinear least squares, but it comes up a fitted curve that is essentially straight, so the residuals go up and then down, and are not randomly distributed. Can you suggest a better method? Thank you, Antonmaz
 Subject: Nonlinear Curve Fitting Problem From: John D'Errico Date: 21 Apr, 2010 21:41:05 Message: 2 of 9 "Anton " wrote in message ... > Hello, I need to fit a curve very accurately. The function is > > n = ( k * sqrt(1 + x^2 / h^2)+phi ) / pi > > where k, h, phi are fitting parameters > and the data is > n = > 2 > 3 > 4 > 5 > 6 > 7 > > x = > 3.7910 > 4.0640 > 4.2850 > 4.4930 > 4.6750 > 4.8440 > > I tried doing a nonlinear least squares, but it comes up a fitted curve that is essentially straight, so the residuals go up and then down, and are not randomly distributed. > > Can you suggest a better method? Sigh. You need to fit this curve very accurately. Of course, the model that you pose does not look even remotely like the data that you have. You cannot fit a model simply by wanting very much for it to fit. Prayer won't help either. Since you apparently have no idea of a good model for this data, we cannot help you. The best I can offer is the simple polyfit.   P = polyfit(x,n,2); At least it fits quite nicely. And anything of a higher order than a quadratic polynomial is pretty much a waste of cpu cycles. John
 Subject: Nonlinear Curve Fitting Problem From: James Phillips Date: 23 May, 2010 13:04:04 Message: 3 of 9 This data set fits extremely well to the standard Steinhart-Hart equation: n = coeff_A + coeff_B * ln(x) + coeff_C * ln(x)^3 and appears to both interpolate and extrapolate smoothly. I found this by using the 'Function Finder' at http://zunzun.com Wikipedia link to Steinhart-Hart equation: http://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation      James Phillips      zunzun@zunzun.com      http://zunzun.com "Anton " wrote in message ... > Hello, I need to fit a curve very accurately. The function is > > n = ( k * sqrt(1 + x^2 / h^2)+phi ) / pi > > where k, h, phi are fitting parameters > and the data is > n = > 2 > 3 > 4 > 5 > 6 > 7 > > x = > 3.7910 > 4.0640 > 4.2850 > 4.4930 > 4.6750 > 4.8440 > > I tried doing a nonlinear least squares, but it comes up a fitted curve that is essentially straight, so the residuals go up and then down, and are not randomly distributed. > > Can you suggest a better method? > > Thank you, > > Antonmaz
 Subject: Nonlinear Curve Fitting Problem From: John D'Errico Date: 23 May, 2010 13:53:03 Message: 4 of 9 "James Phillips" wrote in message ... > This data set fits extremely well to the standard Steinhart-Hart equation: > > n = coeff_A + coeff_B * ln(x) + coeff_C * ln(x)^3 > > and appears to both interpolate and extrapolate smoothly. I found this by using the 'Function Finder' at http://zunzun.com > > Wikipedia link to Steinhart-Hart equation: http://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation > > James Phillips > zunzun@zunzun.com > http://zunzun.com Sorry, but this is just silly. Ridiculous in the extreme. Note that the model you pose results in a fit that while accurate, is in fact poorer than the fit that you get from a simple quadratic polynomial. The model you pose has a residual standard deviation of 0.012987. The R^2 is 0.99996. While these are reasonably good numbers for a curve fit, note that had you tried a simple quadratic polynomial, you would have found that it yields a lower residual error and a higher r^2. The quadratic model has a residual standard deviation of 0.011293, with R^2 = 0.99996. And the last that I checked, quadratic polynomials also extrapolate fairly smoothly. Note that the quadratic has the same number of coefficients, (three) as the model that you pulled out of a hat. My point is that throwing a canned routine at your data to automatically test a variety of models until you find one that fits is a remarkably fatuous strategy, especially if you can't bother to compare the results to a simple quadratic polynomial fit on the same data. Yes, you are proud of your code. But next time try showing some common sense when you post a response like this. John
 Subject: Nonlinear Curve Fitting Problem From: James Phillips Date: 23 May, 2010 17:57:03 Message: 5 of 9 "John D'Errico" wrote in message ... > > Sorry, but this is just silly. Ridiculous in the extreme. I apologize for posing an well-understood alternative physical model that might help explain the underlying physics of the data set, even though you did not. Better? Note the number of digits of precision in the data set.      James
 Subject: Nonlinear Curve Fitting Problem From: John D'Errico Date: 23 May, 2010 19:56:05 Message: 6 of 9 "James Phillips" wrote in message ... > "John D'Errico" wrote in message ... > > > > Sorry, but this is just silly. Ridiculous in the extreme. > > I apologize for posing an well-understood alternative physical model that might help explain the underlying physics of the data set, even though you did not. Better? > > Note the number of digits of precision in the data set. No. You picked out an arbitrary, random model, that happens to fit well. Now you claim that it will help to explain the underlying physics. There is no physical justification for choosing this model from the OP's query. That it happens to fit well does not suggest that it explains anything. The quadratic fits just as well. Does it explain anything? Of course not. But I'm not trying to claim that it will. Both models extrapolate smoothly. So what? The number of digits of precision provided tells you nothing here. Sorry, but this is just an abuse of modeling. John
 Subject: Nonlinear Curve Fitting Problem From: Mark Shore Date: 23 May, 2010 20:31:04 Message: 7 of 9 "James Phillips" wrote in message ... > "John D'Errico" wrote in message ... > > > > Sorry, but this is just silly. Ridiculous in the extreme. > > I apologize for posing an well-understood alternative physical model that might help explain the underlying physics of the data set, even though you did not. Better? > > Note the number of digits of precision in the data set. > > James One usually uses a more complex model where there is either an understood physical basis for doing so, or where trial and error over a range of parameters has eliminated simpler methods. Here we are given a grand total of six points, with no explanation of what they might represent physically - if anything. With a little digging, I find that the Steinhart–Hart approximation is used to model thermistor resistance vs. temperature. OK, good, but is there any indication this is relevant for the set of numbers shown? After all, a fifth-order polynomial would fit the data both perfectly, and pointlessly.
 Subject: Nonlinear Curve Fitting Problem From: James Phillips Date: 23 May, 2010 21:41:03 Message: 8 of 9 Perhaps this is a good place for the original poster to chime in. Anton, any thoughts or remarks on this thread?      James
 Subject: Nonlinear Curve Fitting Problem From: Star Strider Date: 31 May, 2010 01:54:05 Message: 9 of 9 "Anton " wrote in message ... > Hello, I need to fit a curve very accurately. The function is > > n = ( k * sqrt(1 + x^2 / h^2)+phi ) / pi > > where k, h, phi are fitting parameters > and the data is > n = > 2 > 3 > 4 > 5 > 6 > 7 > > x = > 3.7910 > 4.0640 > 4.2850 > 4.4930 > 4.6750 > 4.8440 > > I tried doing a nonlinear least squares, but it comes up a fitted curve that is essentially straight, so the residuals go up and then down, and are not randomly distributed. > > Can you suggest a better method? > > Thank you, > > Antonmaz With due note to the fact that I may be missing something, I didn't have any trouble fitting it: nfit = inline('( B(1) .* sqrt(1 + x.^2 ./ (B(2).^2) ) + B(3)) ./ pi', 'B', 'x'); % B(1) = k, B(2) = h, B(3) = phi B0 = rand(3,1); [Bfit, nrsd, nJ, covB, mse] = nlinfit(x, n, nfit, B0); This estimates: Parameters: k = 604.376 h = 12.857 phi = -624.226 MSE = 0.017 If I wrote your equation correctly in my "inline" function, this works. If I didn't, rewrite it and post your rewrite. Run it to see if it fits your data, and post your parameters. Frank

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.