Hello Again,<br><br>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.<br>
<br>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?<br><br>Code example below - data file printout below that as GNUplot didn't like filecontents headers.<br>
<br><br>\documentclass[]{article}<br>%<br>\usepackage[miktex]{gnuplottex}<br>\usepackage{pst-all}<br>\usepackage{pst-func}<br>\usepackage{pstricks-add}<br><br>\makeatletter<br><br>%%%%%%%%%Failure<br>\def\fitme#1#2#3#4#5#6{%<br>
%\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"}<br>%\commstring<br>
%\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"}<br>\immediate\write18{gnuplot -persist}<br>
\immediate\write18{-e "set print 'out.fit';-persist"}<br>%\immediate\write18{-e "f(x)=#2; fit f(x) '#1' using 1:2 via #4; <br>%\immediate\write18{-e "text=sprintf('\textbackslash psplot\{#5\}\{#6\}\{#3\}',#4); print text; quit"}<br>
\immediate\write18{-e "text=sprintf('Hi Again'); print text; quit"}<br>%\immediate\write18{}%#}<br>%\immediate\write18{}%#}<br>%\immediate\write18{}<br>%\immediate\write18{}<br>%\immediate\write18{}<br>}%<br>
%%%%%%%%%Failure<br><br>\long\gdef\gnuplotgraphicsinclude{{\input{fit.fit}}}<br>\def\PsEuler{2.71828182846}<br>\makeatother<br>%<br>\begin{document}<br>\pagestyle{empty}<br>\pagenumbering{arabic}<br><br>\readdata[ignoreLines=1]{\NLdata}{nl.dat}<br>
<br>\begin{figure}<br>\begin{psgraph}[arrows=->,Dx=1,Dy=0.1,xsubticks=1,ysubticks=1](0,0)(0,-1)(6,1){4.5in}{4.5in}<br>%\fitme{linear.dat}{m*x}{\%.2f*x}{m}{0}{6} - desired format of macro - hide gnuplot commands<br>\begin{gnuplot}%need to properly adapt gnuplottex to eliminate unnecessary commands for this operation.<br>
set print 'fit.fit'<br>f(x)=a*exp(-b*x)*sin(c*x+d) #model should be token passed<br>fit f(x) 'nl.dat' using 1:2 via a,b,c,d #file name and m should be tokens<br>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.<br>
print text<br>\end{gnuplot}<br>\rput[lb](3,0.4){$a*exp(-b*x)*sin(c*x+d)$} %retrieve string from file<br>\immediate\write18{del fit.fit} %delete temp file<br>\listplot[plotstyle=dots]{\NLdata}<br>\rput[cb](3,0.9){Curve Fit using GNUplot as backend} <br>
\end{psgraph}<br>\end{figure}<br><br>\end{document}<br><br>Data file<br>nl.dat<br>#a*e^(-b*x)*sin(c*x+d), a=1, b=0.5, c=1, d=0.3<br>0 0.295520207<br>0.2 0.433802166<br>0.4 0.527440832<br>0.6 0.580302847<br>0.8 0.597394159<br>
1 0.584427582<br>1.2 0.547436856<br>1.4 0.492446171<br>1.6 0.425200038<br>1.8 0.350954738<br>2 0.274329617<br>2.2 0.199214071<br>2.4 0.128724346<br>2.6 0.065203049<br>2.8 0.010253665<br>3 -0.035197822<br>
3.2 -0.070821912<br>3.4 -0.096792333<br>3.6 -0.113686981<br>3.8 -0.122388578<br>4 -0.123989577<br>4.2 -0.119704847<br>4.4 -0.110794655<br>4.6 -0.098499563<br>4.8 -0.083988013<br>5 -0.068316672<br>
5.2 -0.052403005<br>5.4 -0.037009104<br>5.6 -0.022735463<br>5.8 -0.010023168<br>6 0.000837115<br><br>