5

I tried this unsuccessfully:

find_fit(data, quadratic_residues)

I am trying to find the best-fit for data about water flow rates: http://dl.getdropbox.com/u/175564/rate.png

---edit after the comment---

The new code:

var('x')
model(x) = x**2
find_fit((xlist, reqlist), model)

The error message:

Traceback (click to the left for traceback)
...
TypeError: data has to be a list of lists, a matrix, or a numpy array

---edit

The error message is now:

Traceback (click to the left for traceback)
...
ValueError: each row of data needs 2 entries, only 5 entries given

The same here as a picture: http://dl.getdropbox.com/u/175564/sage.png

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

3 Answers3

7
mydata = [[1,3],[2,7],[3,13],[4,24]]
var('a,b,c')
mymodel(x) = a*x^2 + b*x + c 
myfit = find_fit(mydata,mymodel,solution_dict=True)
points(mydata,color='purple') + plot(
  mymodel(
    a=myfit[a],
    b=myfit[b],
    c=myfit[c]
    ), 
    (x,0,4,),
    color='red'
  )
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
Andre Mikulec
  • 1,302
  • 2
  • 10
  • 8
3

I think your problem is that quadratic_residues probably doesn't mean what you think it means. If you are attempting to fit the best quadratic model I think you want to do something like.

var('a, b, c, x')
model(x) = a*x*x + b*x + c
find_fit(data, model)
Steven Noble
  • 10,204
  • 13
  • 45
  • 57
2

Trying Steven his example I also ran into the error:

ValueError: each row of data needs 5 entries, only 2 entries given

Here is an more explicit example that I've tested to be working in sage 4.7.

sage: l=[4*i^2+7*i+134+random() for i in xrange(100)]
sage: var('a,b,c,x')
(a, b, c, x)
sage: model=a*x^2+b*x+c
sage: find_fit(zip(xrange(100),l),model,variables=[x])
[a == 4.0000723084513217, b == 6.9904742307159697, c == 134.74698715254667]

Apperently you need the variables=[x] to tell sage which of a,b,c and x corresponds to the variable in your model.

Maarten Derickx
  • 1,502
  • 1
  • 16
  • 27