[SciPy-user] Curve fitting and LaTeX output

Ryan Krauss ryanlists at gmail.com
Thu Aug 28 11:10:50 EDT 2008


optimize.fmin will find local minima of your cost.  Defining the cost
carefully and giving good initial guesses are very important.

On 8/28/08, David Lonie <loniedavid at gmail.com> wrote:
>
> 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 at 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>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20080828/8bf4dfd6/attachment.html>


More information about the SciPy-User mailing list