Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

curve fitting problems

840 views
Skip to first unread message

Jernej

unread,
Jun 2, 2014, 7:09:11 AM6/2/14
to
I am trying to fit data to this equation b*(coth(x/a)-a/x) using the curve fitting toolbox. I get this error NaN computed by model function, fitting cannot continue.
Try using or tightening upper and lower bounds on coefficients. Here is the screenshot: https://www.dropbox.com/s/2bgx89frrx2pjbo/b_h.jpg

Steven Lord

unread,
Jun 2, 2014, 9:47:10 AM6/2/14
to

"Jernej " <jfas...@gmail.com> wrote in message
news:lmhm0n$5ih$1...@newscl01ah.mathworks.com...
> I am trying to fit data to this equation b*(coth(x/a)-a/x) using the curve
> fitting toolbox. I get this error NaN computed by model function, fitting
> cannot continue.

That is correct.

> Try using or tightening upper and lower bounds on coefficients. Here is
> the screenshot: https://www.dropbox.com/s/2bgx89frrx2pjbo/b_h.jpg

What happens if you try to evaluate that expression for nonzero b and a
(let's assume without loss of generality that both b and a are positive) at
x = 0? You get b*(coth(0)-Inf). Since coth(0) is Inf, you're computing
Inf-Inf inside the parentheses and that's NaN.

You will need to modify your expression so that it does not try to compute
Inf-Inf or any other operation for which the result is not defined.

http://en.wikipedia.org/wiki/NaN

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

John D'Errico

unread,
Jun 2, 2014, 2:37:08 PM6/2/14
to
"Steven Lord" <Steve...@mathworks.com> wrote in message <lmhv94$m0$1...@newscl01ah.mathworks.com>...
As Steve points out, this function returns NaN at zero.

fun = @(x) coth(x) - 1./x;
fun(0)
ans =
NaN

Of course, for small arguments, the function has numerical
problems. You should expect this.

format long g
fun(1000*eps)
ans =
0

fun(1000000*eps)
ans =
0

fun(10000000000*eps)
ans =
7.40168616175652e-07

We can get a limit for fun at zero of course. I'll use my
limit tool (as always, found on the file exchange.)

[lim,limerr] = limest(fun,0)
lim =
6.0737e-13
limerr =
1.3371e-12

ezplot sees no problem, plotting a smooth curve.

Regardless, I'd suggest you choose a functional form that
lacks singularities of this sort in curve fitting, at least if
you have data that is at or near zero.

Even better is to use a spline form for your curve fit. If
your only issue is a monotonic fit, my SLM tools allow that
quite nicely. (Also on the file exchange.)

John
0 new messages