[Tutor] Fitting data to error function
Colin Ross
colin.ross.dal at gmail.com
Fri Mar 13 18:13:14 CET 2015
Hi all,
I am attempting to optimize the parameters I_0 and w_0 in the function
(func(x,I_0,w_0) shown below) to fit a set of data. However when I run this
code I recieve the error shown below. Any suggestions would be greatly
appreciated.
Code:
import numpy as np
import math
from math import *
# data
xdata = np.array([0.1,1.0,2.0,3.0,4.0,5.0])
ydata = np.array([0.1,0.9,2.2,2.8,3.9,5.1])
# Initial guess.
x0 = np.array([0.0, 0.0])
# Data errors:
sigma = np.array([1.0,1.0,1.0,1.0,1.0,1.0])
# Error function
def func(x,a,b):
return ((a)/2.)*(math.erfc((np.sqrt(2.)*x*1.E-3)/b))
# Optimization
import scipy.optimize as optimization
print optimization.curve_fit(func, xdata, ydata, x0, sigma)
The error I am recieving is as follows:
TypeError: only length-1 arrays can be converted to Python scalars
More information about the Tutor
mailing list