SourceForge logo
SourceForge logo
Menu

matplotlib-users

From: Dr. W. P. <w.p...@tu...> - 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
From: Darren D. <dd...@co...> - 2005年05月31日 12:20:34
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
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.
Thanks for helping keep SourceForge clean.
X





Briefly describe the problem (required):
Upload screenshot of ad (required):
Select a file, or drag & drop file here.
Screenshot instructions:

Click URL instructions:
Right-click on the ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)

More information about our ad policies

Ad destination/click URL:

AltStyle によって変換されたページ (->オリジナル) /