#A very simple example of fitting a function or model to data by using least squares. #The Data: Here are some hypothetical data in the form of (t,y) pairs: data = [[1.1, 1.24], [1.9, 0.83], [2.3, 0.71], [4.1, 0.29], [5.5, 0.15]]; #A plot: plt1 = scatter_plot(data); show(plt1) #The Model and Sum of Squares: Let's fit a model f(t) = a*exp(b*t) to this #data by adjusting a and b. First form a sum of squares SS(a,b): var('t a b'); f(t,a,b) = a*exp(b*t); #The model function to fit SS = function('SS')(a,b); SS(a,b) = add((f(data[i][0],a,b)-data[i][1])^2 for i in range(5)); #With only two parameters "a" and "b", a visual estimate of the best choice #(the choices of a and b that minimize SS) can be found by plotting. It's #clear that b<0 since the data decays, and also that a>1. Let us plot log(SS): #A quick plot of log(SS(a,b)) plot3d(log(SS), (a,0,3), (b,-1,0)).show() #Rotating the graph around shows a minimum around a = 2, b = -0.5. #We could minimize using Calculus 3 techniques, but Sage has a #built-in command "find_fit" to find the least-squares model fit to the data: model(t) = f(t,a,b); #Specify the model to be fit, with "t" as the independent variable sol = find_fit(data,model,parameters=[a,b],initial_guess=[2.0,-0.5]) #Fit the model by adjusting k and b abest = sol[0].rhs(); bbest = sol[1].rhs(); bestf(t) = model(a=abest,b=bbest) #Define the best fit function of the form in "model" #Plot the function bestf(t) with these best choices for a and b. plt2 = plot(bestf(t), t, 1.1, 5.5); pp = plt1 + plt2; show(plt1+plt2)