# Interpolation and curve fitting - Tikzpicture? Gnuplot? Pgfplot?

well, I am fairly new to LaTeX, but I tried my hardest do find a solution for my problem but I couldn't find a proper answer, so I hope someone here has the answer or another suggestion for me.

I would like to "smooth" my curve, but if I do so the curve will be partially negative, which is physically impossible and therefore no solution for my thesis. I have read that this happens due to the algorithm used.

Does someone know how to fix that? Is there any other way to smooth my graph maybe using another package or command?

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{tabularx}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{booktabs}
\usepackage{float}

\begin{document}

\begin{figure}[H]
\begin{center}
\begin{tikzpicture}
\begin{axis}[
%title=Without legend box,
legend style={draw=none},
grid = major,
%ymin=0,
%ymax=0.000012,
xmax=144,
xmin=100,
%legend columns=2,
width=0.65\textwidth,
height=6.8cm,
legend style={
cells={anchor=east},
legend pos=outer north east,
}]
\datatable
\addplot[smooth, no markers, color=black, line width=1.25pt] table[y = P3] from \datatable ;
\addplot[smooth, no markers, color=blue, line width=1.25pt] table[y = P4] from \datatable ;

\end{axis}
\end{tikzpicture}
\end{center}
\end{figure}

\end{document}


The data

f P3 P4
100 0.000004 0
102 0.000014 0
104 0.000008 0
106 0.000017 0
108 0.000021 0
110 0.000043 0
112 0.000126 0.000005
114 0.000347 0.000023
116 0.0012 0.000113
118 0.00599 0.000735
120 0.061 0.0036
122 0.9 0.144
122.227 1.5 0.273
124 0.13 0.007
126 0.0053 0.0012
128 0.0022 0.00043
130 0.0003 0.0002
132 0.000246 0.000103
134 0.000132 0.000065
136 0.000072 0.00004
138 0.000045 0.000027
140 0.000032 0.00002
142 0.000024 0.000013
144 0.000016 0.000011

-
provide the data files – Herbert Aug 8 '14 at 19:02
How can I do that? I tried but I don't know how – Nadine Aug 8 '14 at 19:03
f P3 P4 100 0.000004 0 102 0.000014 0 104 0.000008 0 106 0.000017 0 108 0.000021 0 110 0.000043 0 112 0.000126 0.000005 114 0.000347 0.000023 116 0.0012 0.000113 118 0.00599 0.000735 120 0.061 0.0036 122 0.9 0.144 122.227 1.5 0.273 124 0.13 0.007 126 0.0053 0.0012 128 0.0022 0.00043 130 0.0003 0.0002 132 0.000246 0.000103 134 0.000132 0.000065 136 0.000072 0.00004 138 0.000045 0.000027 140 0.000032 0.00002 142 0.000024 0.000013 144 0.000016 0.000011 – Nadine Aug 8 '14 at 19:05
This remark is independent of the actual solutions of smoothing algorithms: Note that smoothing a data point can easily hide relevant information - in particular if the smoothened area consists only of three data points. You could display markers in order to stress that this is what your measurements reveal - and everyone will understand that the smooth interpolation (or the sharp interpolation) is a "model assumption". In an optimal case, you would recompute your model and put more samples around the peak. – Christian Feuersänger Aug 8 '14 at 20:44

The tension key can adjust how smooth the cornering should be performed and in this case you can reduce it just a bit. However, for scientific representation of the plots, never ever use smooth because it distorts the data.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplotstable}
f P3 P4
100 0.000004 0
102 0.000014 0
104 0.000008 0
106 0.000017 0
108 0.000021 0
110 0.000043 0
112 0.000126 0.000005
114 0.000347 0.000023
116 0.0012 0.000113
118 0.00599 0.000735
120 0.061 0.0036
122 0.9 0.144
122.227 1.5 0.273
124 0.13 0.007
126 0.0053 0.0012
128 0.0022 0.00043
130 0.0003 0.0002
132 0.000246 0.000103
134 0.000132 0.000065
136 0.000072 0.00004
138 0.000045 0.000027
140 0.000032 0.00002
142 0.000024 0.000013
144 0.000016 0.000011
}\datatable

\begin{document}

\begin{tikzpicture}
\begin{axis}[
legend style={draw=none},
grid = major,
xmax=144,xmin=100,
width=0.65\textwidth,
height=6.8cm,
legend style={
cells={anchor=east},
legend pos=outer north east,
}]
\addplot[smooth,tension=0.3,no markers, color=black, line width=1.25pt] table[y = P3] from \datatable ;
\addplot[smooth,tension=0.25,no markers, color=blue, line width=1.25pt] table[y = P4] from \datatable ;

\end{axis}
\end{tikzpicture}

\end{document}


-
Hi, thanks for your help. The strange representation is due to an outlier in my measurement series. I would like to smooth the curve without editing the series of measurements. I have already tried "tension", but it made hardly any difference. With a low value, it seems like the original curve and with a high value of tension , the curve slips into the negative range again. – Nadine Aug 8 '14 at 19:33
@Nadine which one is the outlier? Also it doesn't matter, just put the curve as is. It's still not ok to change the plot. – percusse Aug 8 '14 at 19:39
@Herbert Thank you so much, but LaTeX gives me an error when using your way. ! Undefined control sequence. <recently read> \c@lor@to@ps l.38 \readdata [ignoreLines=1]{\data}{file.data} @ percusse At the point that looks as if the graph has a kink. – Nadine Aug 8 '14 at 19:46
@Nadine you have to run xelatex instead of pdflatex – Herbert Aug 8 '14 at 19:51
@Herbert That's it! Thanks again! I guess there is no other way of doing this with tikzpicture and pgfplot, right? – Nadine Aug 8 '14 at 20:13

Run it with xelatex:

\documentclass[pstricks,border=10pt]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{file.data}
f P3 P4
100 0.000004 0
102 0.000014 0
104 0.000008 0
106 0.000017 0
108 0.000021 0
110 0.000043 0
112 0.000126 0.000005
114 0.000347 0.000023
116 0.0012 0.000113
118 0.00599 0.000735
120 0.061 0.0036
122 0.9 0.144
122.227 1.5 0.273
124 0.13 0.007
126 0.0053 0.0012
128 0.0022 0.00043
130 0.0003 0.0002
132 0.000246 0.000103
134 0.000132 0.000065
136 0.000072 0.00004
138 0.000045 0.000027
140 0.000032 0.00002
142 0.000024 0.000013
144 0.000016 0.000011
\end{filecontents*}
\usepackage{pst-plot}

\begin{document}

\begin{pspicture}(-5mm,-10mm)(5.5,3)
\psset{yunit=2}
\psaxes[Ox=100,Dx=10,dx=1,Dy=0.5,axesstyle=frame, xticksize=-0.25 1.5,yticksize=0 5,
subticks=5,subticksize=1,subtickcolor=black!15,labelFontSize=\scriptstyle](0,0)(0,-0.25)(5,1.5)
\pstScalePoints(1,1){100 sub 10 div }{}
\pslistplot[linecolor=blue,linewidth=1pt,plotNo=1,plotNoMax=2,plotstyle=bezier,xEnd=123]{\data}
\pslistplot[linecolor=blue,linewidth=1pt,plotNo=1,plotNoMax=2,plotstyle=curve,xStart=122.2]{\data}
\pslistplot[linecolor=red,linewidth=1pt,plotNo=2,plotNoMax=2,plotstyle=bezier,xEnd=123]{\data}
\pslistplot[linecolor=red,linewidth=1pt,plotNo=2,plotNoMax=2,plotstyle=curve,xStart=122.2]{\data}

\end{pspicture}

\end{document}


-
You changed the data points completely though, it's not reaching 1.5 anymore. – percusse Aug 8 '14 at 19:42
For some reason Herbert's code does not work. I opened a new file and pasted the code. The same error message appears as before. Unfortunately I am too inexperienced in latex in order to understand the error message. – Nadine Aug 8 '14 at 20:01
at least tell us the error message ;-) – Herbert Aug 8 '14 at 20:04
@Herbert It was the same message as before. Sorry, I did not see your answer about xelatex when I posted that. I am really sorry, still a little confused about where to look for posts at this forum! But again, thank you so much for your help. – Nadine Aug 9 '14 at 8:14