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

curve-fit a tanh() function

1,511 views
Skip to first unread message

Faisal Zaman

unread,
Jul 2, 2002, 12:20:30 PM7/2/02
to
Hello Everyone,

I need some help curve-fitting a set of data. In the past I have been able
to curve-fit a linear or a polynomial function, but now I need to figure out
a way to curve-fit a hyperbolic tan (tanh) function. Does anyone have
experience in using such a function?

Any help in this regard will be greatly appreciated.

Faisal


--
Mohammad Faisal Zaman
E-Mail: md.f...@resnet.gatech.edu

Randy Poe

unread,
Jul 2, 2002, 12:31:44 PM7/2/02
to
Faisal Zaman wrote:
>
> Hello Everyone,
>
> I need some help curve-fitting a set of data. In the past I have been able
> to curve-fit a linear or a polynomial function, but now I need to figure out
> a way to curve-fit a hyperbolic tan (tanh) function. Does anyone have
> experience in using such a function?
>
> Any help in this regard will be greatly appreciated.
>

Do you have the Optimization toolbox? If so, you need lsqnonlin.

Otherwise you can adapt fzero or fminsearch to do what you need,
to find the minimum of the least-squares error surface.

- Randy

Martin Cohen

unread,
Jul 2, 2002, 6:32:16 PM7/2/02
to

A simple-minded start:

Assume we want to fit y = a*tanh(b*x+c)+d to data (xx, yy)(1:n).

Since d-a <= y <= d+a, set d+a = max(yy), d-a = min(yy). This gives a
and d.

Suppose yy = 0 at x = x0. Then b*x0+c = 0.

We need one more relation.

Suppose y'(x0) = s0 (the slope when yy = 0).

Since tanh'(x) = 1/cosh^2(x),

y'(x) = a*b/cosh^2(b*x+c) so

s0 = y'(x0) = a*b so b = s0/a and c = -b*x0.

If the y values are not a full tanh curve, but do have a half curve
(-inf to 0 or 0 to inf), estimate a and d from, say,
d+a = max(yy), a = yy(where yy has maximum slope).

Anyway, this could give reasonable starting parameter values
for a nonlinear fit.

Martin Cohen

rek...@gmail.com

unread,
Apr 19, 2018, 9:15:04 AM4/19/18
to
Respected Martin sir,
I am trying to fit my data into y = a*tanh(b*x+c)+d curve. As it is assumed yy=0 at x0, but in my dataset yy value does not 0 value. Value of yy ranges (-0.001043 to 0.2394) and xx ranges from (0 to -1). So can you help what value should I take for x0.
Regards
Rekib
0 new messages