polyfit

Hi,

I try to fit some data but do not get the result I want. The problem is, that I have only few data points:

Data file:

# Dosierung pH
0 9.35
1 8.70
2 8.34
3 8.06
4 7.85
5 7.67

What I want, is to create a fitting curve with the corresponding values e.g. the parameters of a cubic fitting.

I tried this with polyfit:

#!/usr/bin/python

from pylab import *

X = load('Dosierung-pH.dat',comments="#")

x = X[:,0]
y = X[:,1]

xlabel(r'$Dosierung H_2P_2O_7~10~[ml]$')
ylabel(r'$pH-Wert$')

# Fitting:
coeffs = polyfit(x,y,4)
besty = polyval(coeffs,x)
xnew = arange(0,5,0.1)
plot (x,y,'bo',x,besty)

show()

Due to the view x-values, the curve is not a curve but a line with kinks.

Using

xnew = arange(0,5,0.1) and
plot (x,y,'bo',xnew,besty)

do not work, because xnew and besty have then not the same length.

The second question is, how can I get the curve values a,b,c,d out ? I would something expect like

y = a*x^3 + b*x^2 + c*x +d

printing the values for a,b,c,d for further using.

Regards

Werner

Your variable coeffs contains this data, note that your script is doing a 4th
order fit:

y=coeffs[0]*x^4 + coeffs[1]*x^3 + coeffs[2]*x^2 + coeffs[3]*x +coeffs[4]

As for your first question, you need to pass xnew to polyval. Try this script:

#!/usr/bin/python

from pylab import *

X = load('Dosierung-pH.dat',comments="#")

x = X[:,0]
y = X[:,1]

xlabel(r'$Dosierung H_2P_2O_7~10~[ml]$')
ylabel(r'$pH-Wert$')

# Fitting:
coeffs = polyfit(x,y,4)
xnew = arange(0,5.1,0.1)
besty = polyval(coeffs,xnew)
plot (x,y,'bo',xnew,besty)

show()

Darren

ยทยทยท

On Tuesday 31 May 2005 7:15 am, Dr. Werner Pessenhofer wrote:

Hi,

I try to fit some data but do not get the result I want. The problem is,
that I have only few data points:

Data file:

# Dosierung pH
0 9.35
1 8.70
2 8.34
3 8.06
4 7.85
5 7.67

What I want, is to create a fitting curve with the corresponding values
e.g. the parameters of a cubic fitting.

I tried this with polyfit:

#!/usr/bin/python

from pylab import *

X = load('Dosierung-pH.dat',comments="#")

x = X[:,0]
y = X[:,1]

xlabel(r'$Dosierung H_2P_2O_7~10~[ml]$')
ylabel(r'$pH-Wert$')

# Fitting:
coeffs = polyfit(x,y,4)
besty = polyval(coeffs,x)
xnew = arange(0,5,0.1)
plot (x,y,'bo',x,besty)

show()

Due to the view x-values, the curve is not a curve but a line with kinks.

Using

xnew = arange(0,5,0.1) and
plot (x,y,'bo',xnew,besty)

do not work, because xnew and besty have then not the same length.

The second question is, how can I get the curve values a,b,c,d out ? I
would something expect like

y = a*x^3 + b*x^2 + c*x +d

printing the values for a,b,c,d for further using.