# [SciPy-User] Leastsq questions

Timothy Kinney timothyjkinney@gmail....
Mon Jun 14 15:28:14 CDT 2010

```Scipy Community,

I am using the scipy leastsq method to fit some cooling data, such
that the temperature is defined by an exponential decay function
(Newton's Cooling Law). However, there are some other factors which
also influence the cooling rate and I am attempting to account for
them in the cooling law. I have some questions about leastsq:

1) When I fit the data in Excel I get a different fit than when I fit
the same data in Scipy. Why is this? The fits are not very different,
but they are consistently different.

2) How do I calculate the goodness of fit (R squared) for the leastsq
algorithm? I think it's just the sum of the squared errors divided by
something, but shouldn't this be easily called from the output?

I would like to iterate over a computation where I change one of the
values and see how it effects the goodness of the fit. I'm not sure
how to calculate the r-squared from the plsq that is returned from
leastsq.

My goal is to find the value of a single parameter that best optimizes
the leastsq fit. Should I be using one of the other optimizing
functions for this instead? Basically, I calculate a value from the
theory and compare it to the experimental data. I fit the theory to
the data and look at the r-squared. I want to adjust the theory to
account for some other factors by adjusting one of the terms in a way
that maximizes the goodness of fit.

-Tim
```