有没有办法使用 gnuplot 进行复杂的计算?
例如
\pgfmathsetmacro{\x}{0.02} \pgfmathsetmacro{\y}{2*11000*(1 - 1.40576 - cos(\x) + sqrt(1.40576^2 - sin(\x)^2))}
我得到“尺寸太大”。
% arara: pdflatex: {shell: yes}
\documentclass[margin=5mm, varwidth]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\pgfmathsetmacro{\x}{0.02}
%\pgfmathsetmacro{\y}{2*11000*(1 - 1.40576 - cos(\x) + sqrt(1.40576^2 - sin(\x)^2))}
Calculating works not: % \y
\begin{tikzpicture}[]
\newcommand\Curve[1]{2*11000*(1 - 1.40576 - cos(#1) + sqrt(1.40576^2 - sin(#1)^2))}
\begin{axis}[title=Graphing works:]
\addplot[blue,domain = {-0.07:0.07}] plot gnuplot[samples=500,id=curve]{\Curve{x}} node[anchor=east]{gnuplot: good};
\addplot[red,domain = {-0.07:0.07}, trig format plots=rad]{\Curve{x}} node[pos=0.5,anchor=north]{pgfplots: bad};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
gnuplot 更适合绘图,但您也可以使用 expl3 的浮点数:
\documentclass{article}
\usepackage{xfp}
\begin{document}
\newcommand{\x}{0.02}
\edef\y{\fpeval{2*11000*(1 - 1.40576 - cos(\x) + sqrt(1.40576^2 - sin(\x)^2))}}
\typeout{y=\y}
\edef\y{\fpeval{2*11000*(1 - 1.40576 - cos(\x deg) + sqrt(1.40576^2 - sin(\x deg)^2))}}
\typeout{y=\y}
\end{document}
生产
y=1.270132770126
y=0.000386870374
(注意 PGF 在三角函数中默认使用度数)
答案2
如果我按照手册的建议去做,那就没有问题了。特别是,你可能想
- 打开
fpu
才能使用它。只加载pgfplots
并不能打开它。事实上,你需要在启动之前再次关闭它tikzpicture
。 - 用于
lualatex -shell-escape
编译。
那么结果就完全一致了。
\documentclass[margin=5mm, varwidth]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\pgfmathsetmacro{\x}{0.02}
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=sci}
\pgfmathsetmacro{\y}{2*11000*(1 - 1.40576 - cos(\x) + sqrt(1.40576^2 -
pow(sin(\x),2)))}
Calculating works: \y
\pgfkeys{/pgf/fpu=false}
\begin{tikzpicture}[declare function={f(\x)=2*11000*(1 - 1.40576 - cos(\x) +
sqrt(1.40576^2 - sin(\x)^2));}]
\newcommand\Curve[1]{2*11000*(1 - 1.40576 - cos(#1) + sqrt(1.40576^2 - sin(#1)^2))}
\begin{axis}[title=Graphing works:]
\addplot[blue,domain = {-0.07:0.07}] plot
gnuplot[samples=500,id=curve]{\Curve{x}} node[anchor=east]{gnuplot: good};
\addplot[red,dashed,domain = {-0.07:0.07}, trig format plots=rad]{f(x)}
node[pos=0.5,anchor=north]{pgfplots: good};
\end{axis}
\end{tikzpicture}
\end{document}
结果是否\y
是您想要的或者您需要添加什么\pgfkeys{/pgf/trig format=rad}
或者我不知道。
答案3
R 在进行这些计算时不会遇到任何问题,也无法用足够的点绘制结果:
\documentclass{article}
\begin{document}
<<echo=F,results='asis', fig.width=4,fig.height=4>>=
x = seq(-0.07,0.07,0.0001)
y=2*11000*(1 - 1.40576 - cos(x) + sqrt(1.40576^2 - sin(x)^2))
plot(x,y,type="l",lwd=3,col="blue")
@
\end{document}
好的,是相同的抛物线,但不是相同的图形。现在使用 tikz 外观和感觉:
\documentclass{article}
\begin{document}
<<echo=F,results='asis',dev="tikz",fig.width=4,fig.height=4>>=
x = seq(-0.07,0.07,0.0001)
y=2*11000*(1 - 1.40576 - cos(x) + sqrt(1.40576^2 - sin(x)^2))
plot(x*100,y,type="l",lwd=3,col="blue",
xlab="$x \\times10^{-2}$", ylab="$y$", xlim=c(-8.5,8.5),
xaxp = c(-10,10,10), tck=0.02)
axis(side = 4, tck=0.02, labels = NA)
axis(side = 3, tck=0.02, at=seq(-8,8,2), labels = NA)
@
\end{document}
答案4
类似这样的:
短的:
% arara: pdflatex: {shell: yes}
\documentclass[margin=5mm, varwidth]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.16}
\newsavebox{\mycalc}
% See too: http://gnuplot.sourceforge.net/docs_4.2/node53.html
\begin{document}
\newcommand\Function[3][fixed,precision=6]{%%%%%%%%%
\sbox{\mycalc}{%%
\begin{tikzpicture}[]
\begin{axis}[]
\addplot[blue,domain = {#3:#3+0.1}] plot gnuplot[id=mycalc]{#2};
%\addplot[blue,domain = {pi:2*pi}] plot gnuplot[id=mycalc]{5};
\end{axis}
\end{tikzpicture}
}%%%
%\usebox{\mycalc}
%
\pgfplotstableread[header=false]{\jobname.mycalc.table}\tempdata%
%\pgfplotstabletypeset[string type]{\tempdata}
%
\pgfplotstablegetelem{0}{1}\of\tempdata
\pgfmathsetmacro\y{\pgfplotsretval}
\pgfmathprintnumber[#1]{\y}%
}%%%%%%%%%%%%%%%
\section{Easy}
$\sin(\frac\pi6) = \Function{sin(x)}{pi/6}$
\section{Difficult}
\newcommand\Curve[1]{2*11000*(1 - 1.40576 - cos(#1) + sqrt(1.40576^2 - sin(#1)^2))
}
\newcommand\curve[1]{2\cdot 11000\cdot \left(1 - 1.40576 - \cos(#1) + \sqrt{1.40576^2 - \sin(#1)^2}\right)}
Let $f(x) =\curve{x}$.
\pgfmathsetmacro\x{0.05}
$f(\x) = \Function{\Curve{x}}{\x}$
\end{document}
长的:
% arara: pdflatex: {shell: yes}
\documentclass[margin=5mm, varwidth]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.16}
\newcommand\Curve[1]{2*11000*(1 - 1.40576 - cos(#1) + sqrt(1.40576^2 - sin(#1)^2))
}
\newcommand\curve[1]{2\cdot 11000\cdot \left(1 - 1.40576 - \cos(#1) + \sqrt{1.40576^2 - \sin(#1)^2}\right)}
\begin{document}
Let $f(x) =\curve{x}$.
\pgfmathsetmacro\x{0.05}
We need $f(\x)$
\newsavebox{\mycalc}
\sbox{\mycalc}{%%%%%%%%%%%%%%%%%
\begin{tikzpicture}[]
\begin{axis}[]
\addplot[blue,domain = {\x:\x+0.1}] plot gnuplot[id=mycalc]{\Curve{x}};
%\addplot[blue,domain = {pi:2*pi}] plot gnuplot[id=mycalc]{5};
\end{axis}
\end{tikzpicture}
}%%%%%%%%%%%%%%%%%%%%%%%
\section{Show graph - for Info}
\usebox{\mycalc}
\section{Show gnuplot-generated table - for Info}
\pgfplotstableread[header=false]{\jobname.mycalc.table}\tempdata
\pgfplotstabletypeset[string type]{\tempdata}
\section{Read out value}
\pgfplotstablegetelem{0}{1}\of\tempdata
%\pgfkeys{/pgf/number format/.cd,sci}
%\pgfkeys{/pgf/number format/.cd,fixed,precision=6}
\pgfmathsetmacro\y{\pgfplotsretval}
$f(\x) = \pgfmathprintnumber[fixed,precision=6]{\y}$
$f(\x) = \pgfmathprintnumber[sci,precision=6]{\y}$
\end{document}