# [SciPy-user] Curve fitting and LaTeX output

David Lonie loniedavid@gmail....
Thu Aug 28 07:56:48 CDT 2008

```Thanks for the fast reply! I plugged in my data to that script and
added some output, but I get an invalid fit out of it. The fit I got
from oocalc is approx

y = 32 - 0.85^x, and fmin is returning
y = 12 - 0.65^x, which for some reason returns an array of non-numbers
when I try to generate plot data?

Also, changing the guess around causes large changes in the fit. Is this normal?

Thanks again,

Dave

The code:
========================================================
from scipy.optimize import *
from numpy import *
from pylab import *
from scipy import *

y = array((31,14,2.9,2.0))
x = array((0,6,12,18))

def mycost(c):
y_model = c[0] + c[1]**x
error_vect = y - y_model
return sum(error_vect**2)

c_initial_guess = [5000 , 5000]

c_final = fmin(mycost, c_initial_guess)
xr = linspace(0,18)
yr = c_final[0] + c_final[1] ** xr

print "***************************************************************"
print c_final
print "***************************************************************"
print xr
print "***************************************************************"
print yr
print "***************************************************************"

scatter(x,y)
plot(xr,yr)
show()

On Wed, Aug 27, 2008 at 9:27 PM, Ryan Krauss <ryanlists@gmail.com> wrote:
> You can use optimize.fmin to fit a curve of an arbitrary form.  You have to
> be careful to specify the output of your cost function correctly - you
> probably want the sum of the squared error.  The first input to the cost
> function must be a vector of unknown coefficients.
<snip>
```