# [SciPy-User] Curve fitting questions

Gökhan Sever gokhansever@gmail....
Tue Oct 19 14:20:46 CDT 2010

```On Tue, Oct 19, 2010 at 1:04 PM,  <josef.pktd@gmail.com> wrote:
> I think you could take logs and you would have a linear function in
> param=log(x), and you could use linalg to solve for param, and then
> transform back exp(param).  Or this would give you a starting value if
> you want the non-linear optimization.

Using 3 and 5 data-points the curve_fit usually does a good job, even without
the initial estimates provided. When it's necessary we usually constrain the
initial parameters with max CCN concentration for C (param[0]) and a typical
k (param[1]) values.

This even works with 2 data-points:

I[34]: ccn_ss1 = [0.27, 0.34]

I[35]: ccn_conc1 = np.array([383.51237409766452, 424.82669523141652])

I[36]: tfit2, pcov2 = curve_fit(my_ck, ccn_ss1, ccn_conc1, p0=(424,
0.5), ftol=1)

provides me reasonable estimations. However, having another data-point
would surely
improve the quality of the fit and estimations.

>> ccn_ss1 = 0.27
>> ccn_conc1 = 383.51237409766452
>> # One data point estimation fails with IndexError: index out of range for array
>> tfit3, pcov3 = curve_fit(my_ck, ccn_ss1, ccn_conc1, p0=tfit1, ftol=1)
>
> If you have one parameter to estimate and only one observations, then
> you should be able to solve it exactly with one of the solvers/
> rootfinders in scipy. optimize.
>
> Josef

I want to estimate two parameters using one observation (which is a
data-pair for my case --one for ccn_ss1 and one for ccn_conc1.)
Probably, in this current version fsolve can't do give me any roots.

def my_ck(x, a, b):
return a*x**b

fsolve(my_ck, x0=tfit1, args=(ccn_ss1, ccn_conc1), xtol=1)

rather gives a couple of overflow warnings:
Warning: overflow encountered in power

In one data-pair situation my function looks like:

a*x**b = 383.5

Now there are two unknowns providing the x as ccn_ss1 as a*0.27**b =
383.5. I should make one more assumption otherwise it is still
unsolvable. Probably making an assumption for a, then I can hand solve
this easily. OK, with a = 350 assumption in 350*0.27**b == 383.5, here
solving for b results ~ -0.065

With some modifications on the original fitfunc:

def my_ck(x):
return 350*0.27**x - 383

fsolve nicely estimates what I want.

fsolve(my_ck, x0=0.5)
-0.068815047568104443

--
Gökhan
```