You're assuming that the fit function is going to return the variables
in the same order in both cases, so that (e.g.) a and k1 will be the
first in both cases.  That's not true, though.  I found:

[a == 3.4102956225507519, b == 0.010344876276231638, c ==
-0.00094076216744204172, d == (1.8097013599216266e-05), f ==
(-1.7547848824431721e-07), g == (1.0168442135198695e-09), h ==
(-3.7548063250237949e-12), j == (8.9493796546766707e-15), k ==
(-1.3366803967120946e-17), l == (1.1398034585075684e-20), n ==
(-4.2382572237693009e-24), o == 3.0511588067264963]

[b == 0.010358179948437265, c == -0.00094126295808369734, d ==
(1.8106258279315326e-05), f == (-1.7557568485259962e-07), g ==
(1.0174721357045694e-09), h == (-3.7573869398464435e-12), j ==
(8.9561476716220824e-15), k == (-1.3377771928232302e-17), k1 ==
3.4101714386786477, l == (1.1408041039846924e-20), n ==
(-4.242187409468519e-24), o == 3.0512548011942671]

so the indexing is all off.  Not surprising the second one explodes.
Return order looks to be alphabetical, but I'm too lazy to find out if
that's guaranteed.  In any case, we can use a solution_dict instead to
make life much easier.

So what I'd recommend, given your V, is something like this:

sage: N_order = 12
sage:
sage: # make a list of symbolic variables
sage: coeffs = list(var("c_%d" % i) for i in range(N_order))
sage: coeffs
[c_0, c_1, c_2, c_3, c_4, c_5, c_6, c_7, c_8, c_9, c_10, c_11]
sage:
sage: # build the model [here just a polynomial]
sage: model(x) = sum(c_i*x**i for i, c_i in enumerate(coeffs))
sage: model
x |--> c_11*x^11 + c_10*x^10 + c_9*x^9 + c_8*x^8 + c_7*x^7 + c_6*x^6 +
c_5*x^5 + c_4*x^4 + c_3*x^3 + c_2*x^2 + c_1*x + c_0
sage:
sage: # actually fit it, returning a dictionary
sage: mfit = find_fit(V, model, solution_dict=True)
sage: mfit
{c_7: -3.7564661583891436e-12, c_0: 3.0514755095206816, c_6:
1.0172525553243703e-09, c_11: -4.2407560895324755e-24, c_10:
1.1404399242138324e-20, c_3: -0.00094112724266502126, c_9:
-1.3373790954916053e-17, c_2: 0.010355836374653954, c_4:
1.8103350226003258e-05, c_1: 3.4101603849319178, c_8:
8.9537056388588457e-15, c_5: -1.7554291229990257e-07}
sage:
sage: # substitute these values into the model, getting a function
sage: mfunc = model.subs(mfit)
sage: mfunc
x |--> -(4.2407560895324755e-24)*x^11 + (1.1404399242138324e-20)*x^10
- (1.3373790954916053e-17)*x^9 + (8.9537056388588457e-15)*x^8 -
(3.7564661583891436e-12)*x^7 + (1.0172525553243703e-09)*x^6 -
(1.7554291229990257e-07)*x^5 + (1.8103350226003258e-05)*x^4 -
0.00094112724266502126*x^3 + 0.010355836374653954*x^2 +
3.4101603849319178*x + 3.0514755095206816
sage:

This way I don't have to worry about keeping all those letters
straight.  Note though that unless you're sure it really is a
polynomial, in general fitting data with a high-order polynomial is a
very bad idea (pretty unstable).

Does that help?


Doug

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to