Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project!

## Re: [Matplotlib-users] polyfit

 Re: [Matplotlib-users] polyfit From: Darren Dale - 2005-05-31 12:20:34 Attachments: Message as HTML ```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 =3D load('Dosierung-pH.dat',comments=3D"#") > > x =3D X[:,0] > y =3D X[:,1] > > xlabel(r'\$Dosierung H_2P_2O_7~10~[ml]\$') > ylabel(r'\$pH-Wert\$') > > # Fitting: > coeffs =3D polyfit(x,y,4) > besty =3D polyval(coeffs,x) > xnew =3D 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 =3D 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 =3D a*x^3 + b*x^2 + c*x +d > > printing the values for a,b,c,d for further using. Your variable coeffs contains this data, note that your script is doing a 4= th=20 order fit: y=3Dcoeffs[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 scri= pt: #!/usr/bin/python from pylab import * X =3D load('Dosierung-pH.dat',comments=3D"#") x =3D X[:,0] y =3D X[:,1] xlabel(r'\$Dosierung H_2P_2O_7~10~[ml]\$') ylabel(r'\$pH-Wert\$') # Fitting: coeffs =3D polyfit(x,y,4) xnew =3D arange(0,5.1,0.1) besty =3D polyval(coeffs,xnew) plot (x,y,'bo',xnew,besty) show() Darren ```

 [Matplotlib-users] polyfit From: Dr. Werner Pessenhofer - 2005-05-31 11:16:14 ```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 ```
 Re: [Matplotlib-users] polyfit From: Darren Dale - 2005-05-31 12:20:34 Attachments: Message as HTML ```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 =3D load('Dosierung-pH.dat',comments=3D"#") > > x =3D X[:,0] > y =3D X[:,1] > > xlabel(r'\$Dosierung H_2P_2O_7~10~[ml]\$') > ylabel(r'\$pH-Wert\$') > > # Fitting: > coeffs =3D polyfit(x,y,4) > besty =3D polyval(coeffs,x) > xnew =3D 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 =3D 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 =3D a*x^3 + b*x^2 + c*x +d > > printing the values for a,b,c,d for further using. Your variable coeffs contains this data, note that your script is doing a 4= th=20 order fit: y=3Dcoeffs[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 scri= pt: #!/usr/bin/python from pylab import * X =3D load('Dosierung-pH.dat',comments=3D"#") x =3D X[:,0] y =3D X[:,1] xlabel(r'\$Dosierung H_2P_2O_7~10~[ml]\$') ylabel(r'\$pH-Wert\$') # Fitting: coeffs =3D polyfit(x,y,4) xnew =3D arange(0,5.1,0.1) besty =3D polyval(coeffs,xnew) plot (x,y,'bo',xnew,besty) show() Darren ```