[pstricks] Curve fit using GNUplot backend
Buddy Ledger
buddyledger at gmail.com
Thu Feb 2 20:12:51 CET 2012
Hello Again,
I thought I would post this concept for curve fitting as well. I have done
a quick hack of gnuplottex in order to use GNUplot as a curve fitting
backend for pstricks - much like pgfplots. However, I propose not to
capture points but read the curvefit string - this gives pstricks full
control over plotting, extents, scales, limits, etc.
I am far from a Latex expert, I mostly copy bits and pieces from others.
So packaging this as a concise macro is a bit beyond my depth. Any takers?
Code example below - data file printout below that as GNUplot didn't like
filecontents headers.
\documentclass[]{article}
%
\usepackage[miktex]{gnuplottex}
\usepackage{pst-all}
\usepackage{pst-func}
\usepackage{pstricks-add}
\makeatletter
%%%%%%%%%Failure
\def\fitme#1#2#3#4#5#6{%
%\def\commstring{gnuplot -persist -e "set print 'out.fit'; f(x)=#2; fit
f(x) '#1' using 1:2 via #4; text=sprintf('\textbackslash
psplot\{#5\}\{#6\}\{#3\}',#4); print text; quit"}
%\commstring
%\immediate\write18{gnuplot -persist -e "set print 'out.fit'; f(x)=#2; fit
f(x) '#1' using 1:2 via #4; %text=sprintf('\textbackslash
psplot\{#5\}\{#6\}\{#3\}',#4); print text; quit"}
\immediate\write18{gnuplot -persist}
\immediate\write18{-e "set print 'out.fit';-persist"}
%\immediate\write18{-e "f(x)=#2; fit f(x) '#1' using 1:2 via #4;
%\immediate\write18{-e "text=sprintf('\textbackslash
psplot\{#5\}\{#6\}\{#3\}',#4); print text; quit"}
\immediate\write18{-e "text=sprintf('Hi Again'); print text; quit"}
%\immediate\write18{}%#}
%\immediate\write18{}%#}
%\immediate\write18{}
%\immediate\write18{}
%\immediate\write18{}
}%
%%%%%%%%%Failure
\long\gdef\gnuplotgraphicsinclude{{\input{fit.fit}}}
\def\PsEuler{2.71828182846}
\makeatother
%
\begin{document}
\pagestyle{empty}
\pagenumbering{arabic}
\readdata[ignoreLines=1]{\NLdata}{nl.dat}
\begin{figure}
\begin{psgraph}[arrows=->,Dx=1,Dy=0.1,xsubticks=1,ysubticks=1](0,0)(0,-1)(6,1){4.5in}{4.5in}
%\fitme{linear.dat}{m*x}{\%.2f*x}{m}{0}{6} - desired format of macro - hide
gnuplot commands
\begin{gnuplot}%need to properly adapt gnuplottex to eliminate unnecessary
commands for this operation.
set print 'fit.fit'
f(x)=a*exp(-b*x)*sin(c*x+d) #model should be token passed
fit f(x) 'nl.dat' using 1:2 via a,b,c,d #file name and m should be tokens
text=sprintf('\psplot[algebraic]{0}{6}{%.2f*(\PsEuler^(-%.2f*x))*sin(%.2f*x+%.2f)}',a,b,c,d)
#tokenize format and variable string, possibly exclude \psplot and assemble
that in Tex so that we can use the string from the file to label the fit.
print text
\end{gnuplot}
\rput[lb](3,0.4){$a*exp(-b*x)*sin(c*x+d)$} %retrieve string from file
\immediate\write18{del fit.fit} %delete temp file
\listplot[plotstyle=dots]{\NLdata}
\rput[cb](3,0.9){Curve Fit using GNUplot as backend}
\end{psgraph}
\end{figure}
\end{document}
Data file
nl.dat
#a*e^(-b*x)*sin(c*x+d), a=1, b=0.5, c=1, d=0.3
0 0.295520207
0.2 0.433802166
0.4 0.527440832
0.6 0.580302847
0.8 0.597394159
1 0.584427582
1.2 0.547436856
1.4 0.492446171
1.6 0.425200038
1.8 0.350954738
2 0.274329617
2.2 0.199214071
2.4 0.128724346
2.6 0.065203049
2.8 0.010253665
3 -0.035197822
3.2 -0.070821912
3.4 -0.096792333
3.6 -0.113686981
3.8 -0.122388578
4 -0.123989577
4.2 -0.119704847
4.4 -0.110794655
4.6 -0.098499563
4.8 -0.083988013
5 -0.068316672
5.2 -0.052403005
5.4 -0.037009104
5.6 -0.022735463
5.8 -0.010023168
6 0.000837115
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://tug.org/pipermail/pstricks/attachments/20120202/ff4ffed6/attachment.html>
More information about the PSTricks
mailing list