from pylab import * from numpy import * from numpy.random import normal from scipy.optimize import fmin # parametric function, x is the independent variable # and c are the parameters. # it's a polynomial of degree 2 fp = lambda c, x: c[0]+c[1]*x+c[2]*x*x real_p = rand(3) # error function to minimize e = lambda p, x, y: (abs((fp(p,x)-y))).sum() # generating data with noise n = 30 x = linspace(0,1,n) y = fp(real_p,x) + normal(0,0.05,n) # fitting the data with fmin p0 = rand(3) # initial parameter value p = fmin(e, p0, args=(x,y)) print 'estimater parameters: ', p print 'real parameters: ', real_p xx = linspace(0,1,n*3) plot(x,y,'bo', xx,fp(real_p,xx),'g', xx, fp(p,xx),'r') show()The following figure will be showed, in green the original curve used to generate the noisy data, in blue the noisy data and in red the curve found in the minimization process:
The parameters will be printed also:
Optimization terminated successfully. Current function value: 0.861885 Iterations: 77 Function evaluations: 146 estimater parameters: [ 0.92504602 0.87328979 0.64051926] real parameters: [ 0.86284356 0.95994753 0.67643758]