Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

It turns out that this has even showed up on Stack Overflow.

Luckily, that page refers to the function I had completely forgotten about - find_fit.

sage: find_fit?

String Form:    <function find_fit at 0x10bee5cf8>
Namespace:      Interactive
File:           /Applications/MathApps/sage/local/lib/python2.6/site-packages/sage/numerical/optimize.py
Definition:     find_fit(data, model, initial_guess=None, parameters=None, variables=None, solution_dict=False)
Docstring:
       Finds numerical estimates for the parameters of the function model
       to give a best fit to data.

So this might work, and looks decent.

sage: R = [[1,2],[3.45,4],[6,5],[4,3]]
sage: var('a,b')
(a, b)
sage: model(x) = a*x+b
sage: find_fit(R,model)
[a == 0.56881365890949054, b == 1.445160655902004]
sage: points(R)+plot(model(a=find_fit(R,model)[0].rhs(),b=find_fit(R,model)[1].rhs()),(x,0,10),color='red')

If you are serious about your needs, though, you should probably use some of the tools in Scipy or R (numerous YouTube videos on this, though for reason I can't watch them right now.