You simply have problems with your model. What LsqFit returns has
dramatically smaller chi^2 than the value you post above from Mathematica.
The problem is entirely with those points for which xdata .< 1, in which
your model diverges to infinity. Try restricting the data set to x-points
greater than 1, e.g.:
sel = xdata .> 1
fit = curve_fit(model, xdata[sel], ydata[sel], [0.5, 0.5])
--Blake
On Thursday, October 2, 2014 12:47:11 PM UTC-4, Helgi Freyr wrote:
>
> Hello,
>
> I have been trying Julia out today. In particular, I have been playing
> around with fitting some data with LsqFit.
>
> However, it's not working as well as I would have hoped.
>
> Here is the code and the two data files and the output figure are attached:
>
> using LsqFit
> using PyPlot
>
> model(x, p) = p[1]./x + p[2]./x.^2
>
> xdata = readdlm("gridx.dat")
> ydata = readdlm("F1.dat")
>
> fit = curve_fit(model, xdata[1:end], ydata[1:end], [0.5, 0.5])
> println(fit.param)
> errors = estimate_errors(fit, 0.95)
> println(errors)
>
>
> plot(xdata,ydata,color="blue")
> xlim(0,120)
> plot(xdata,model(xdata, fit.param),color="green")
> savefig("lsq.png")
>
>
> Playing around with the model itself does do something. For example, as
> this code is basically just the one from the readme file of LsqFit, I tried:
>
> model(x, p) = p[1]*exp(-x.*p[2])
>
> which is used there. It does fit somewhat better but not quite. I ran the
> same thing in Mathematica and obtained:
>
> p[1]: 0.229263
> p[2]: -0.0949777
>
> which gets most of the curve right. Am I missing something in trying to
> fit this curve to the data or are there any particular tricks to use with
> curve_fit that I should know about?
>
> Best regards,
> Helgi
>