我是否可以定义一个数学函数 f(x),例如 f(x) = sqrt(x),在给定点 p=2 处评估该函数并将结果四舍五入为小数点后 4 位?我更喜欢在 tikz 环境中使用此代码。我的困难是使用我定义的函数计算数字 m =1/2*f(p),并提供具有给定精度(精确到小数点后 4 位或 5 位)的结果。以下代码没有四舍五入。
在这里我提供我的代码:
\documentclass[tikz, border=.5cm]{article}
\usepackage{tkz-fct}
\usepackage{multido}
\usetikzlibrary{calc,math}
\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill,precision=5}
\usepackage{xfp}
\usepackage{float}
\usepackage{amsmath}
\begin{document}
\tikzmath{
real \a;
real \b;
\a = 0;
\b = 4;
}
\foreach \p in {2,1}{
\begin{tikzpicture}[declare function = {f(\x) = (\x)^0.5;}]
\clip (-1.5,-1.5) rectangle (4.5,3.5);
\draw[help lines] (-1,-1) grid (4,3);
\draw[->,>=stealth'] (-1,0) -- (4,0) node[right] {$x$};
\draw[->,>=stealth'] (0,-1) -- (0,3) node[above] {$y$};
\foreach \j in {-1,0,1,2,3,4}{
\draw (\j,2pt)--(\j,-2pt) node[below,fill=white]{{\footnotesize $\j$}};
}
\foreach \j in {-1,0,1,2,3,}{
\draw (2pt,\j)--(-2pt,\j) node[left,] {{\footnotesize $\j$}};
}
\draw[samples=1000,thick,blue] plot[domain=\a:\b](\x,{f(\x)}) node[right]{$f$};
\draw[samples=1000,thick,red] plot[domain=-1.5+\p:1.5+\p]({\x},{f(\p) + ( 1/(2*f(\p))*(\x - \p)}) node[above] {$t$};
\draw[fill] (\p,{f(\p)}) circle (1pt);
\draw[dotted] (\p,0) -- (\p,{f(\p)}) node[above,rotate=atan(1/(2*f(\p)))]{{\scriptsize $(\fpeval{round(\p,1)},\fpeval{round(\p^0.5,2)})$}} -- (0,{f(\p)});
\node[above] (2) at (1.5,3) {$m = \pgfmathparse{1/(2*f(\p)) }\pgfmathresult$};
\end{tikzpicture}
}
\end{document}
答案1
为了您的
\pgfkeys{/pgf/number format/.cd, fixed, fixed zerofill, precision=5}
要使用,您需要调用\pgfmathprintnumber
或\pgfmathprintnumberto
,如\pgfmathprintnumber{\pgfmathresult}
。但是,pgfmath
不是很准确,如下所示,xfp
(l3fp
)给出了更好的结果:
\documentclass{article}
\usepackage{tkz-fct}
\usetikzlibrary{math}
\pgfkeys{/pgf/number format/.cd, fixed, fixed zerofill, precision=5}
\usepackage{xfp}
\begin{document}
\tikzmath{
real \a;
real \b;
\a = 0;
\b = 4;
}
\foreach \p in {2,1}{
\begin{tikzpicture}[declare function = {f(\x) = (\x)^0.5;}]
\clip (-1.5,-1.5) rectangle (4.5,3.5);
\draw[help lines] (-1,-1) grid (4,3);
\draw[->,>=stealth'] (-1,0) -- (4,0) node[right] {$x$};
\draw[->,>=stealth'] (0,-1) -- (0,3) node[above] {$y$};
\foreach \j in {-1,0,1,2,3,4}{
\draw (\j,2pt)--(\j,-2pt) node[below,fill=white]{{\footnotesize $\j$}};
}
\foreach \j in {-1,0,1,2,3,}{
\draw (2pt,\j)--(-2pt,\j) node[left,] {{\footnotesize $\j$}};
}
\draw[samples=1000,thick,blue] plot[domain=\a:\b](\x,{f(\x)}) node[right]{$f$};
\draw[samples=1000,thick,red] plot[domain=-1.5+\p:1.5+\p]({\x},{f(\p) + ( 1/(2*f(\p))*(\x - \p)}) node[above] {$t$};
\draw[fill] (\p,{f(\p)}) circle (1pt);
\draw[dotted] (\p,0) -- (\p,{f(\p)}) node[above,rotate=atan(1/(2*f(\p)))]{{\scriptsize $(\fpeval{round(\p,1)},\fpeval{round(\p^0.5,2)})$}} -- (0,{f(\p)});
\node[above] (2) at (1.5,3)
{$m \approx \pgfmathparse{1/(2*f(\p))} \pgfmathprintnumber{\pgfmathresult}$};
\end{tikzpicture}
}
\foreach \p in {2,1}{
\begin{tikzpicture}[declare function = {f(\x) = (\x)^0.5;}]
\clip (-1.5,-1.5) rectangle (4.5,3.5);
\draw[help lines] (-1,-1) grid (4,3);
\draw[->,>=stealth'] (-1,0) -- (4,0) node[right] {$x$};
\draw[->,>=stealth'] (0,-1) -- (0,3) node[above] {$y$};
\foreach \j in {-1,0,1,2,3,4}{
\draw (\j,2pt)--(\j,-2pt) node[below,fill=white]{{\footnotesize $\j$}};
}
\foreach \j in {-1,0,1,2,3,}{
\draw (2pt,\j)--(-2pt,\j) node[left,] {{\footnotesize $\j$}};
}
\draw[samples=1000,thick,blue] plot[domain=\a:\b](\x,{f(\x)}) node[right]{$f$};
\draw[samples=1000,thick,red] plot[domain=-1.5+\p:1.5+\p]({\x},{f(\p) + ( 1/(2*f(\p))*(\x - \p)}) node[above] {$t$};
\draw[fill] (\p,{f(\p)}) circle (1pt);
\draw[dotted] (\p,0) -- (\p,{f(\p)}) node[above,rotate=atan(1/(2*f(\p)))]{{\scriptsize $(\fpeval{round(\p,1)},\fpeval{round(\p^0.5,2)})$}} -- (0,{f(\p)});
\node[above] (2) at (1.5,3)
{$m \approx \fpeval{round(1/(2*sqrt(\p)), 5)}$};
\end{tikzpicture}
}
\end{document}
通过使用:
\fpeval
里面\pgfmathparse
接着\pgfmathprintnumber{\pgfmathresult}
, 或;更直接地说
\pgfmathprintnumber{\fpeval{1/(2*sqrt(\p))}}
,
您可以打印小数位数固定精度很高(这里有 14 位正确的数字,并且第 15 位数字按照下面的内容正确四舍五入!):
\documentclass{article}
\usepackage{pgffor}
\usepackage{pgfmath}
\usepackage{pgf}
\usepackage{xfp}
\pgfkeys{/pgf/number format/.cd, fixed, fixed zerofill, precision=15}
\begin{document}
\foreach \p in {2,1} {%
When $p = \p$,
$m \approx \pgfmathprintnumber{\fpeval{1/(2*sqrt(\p))}}$.\par
}
\end{document}
格式化数字的另一种方法(这里用 计算\fpeval
,但这不是必需的)是使用siunitx
包:
\documentclass{article}
\usepackage{pgffor}
\usepackage{siunitx}
% See also option 'round-integer-to-decimal'.
\sisetup{round-mode = places, round-precision=15}
\usepackage{xfp}
\begin{document}
\foreach \p in {2,1} {%
When $p = \p$,
$m \approx \num{\fpeval{1/(2*sqrt(\p))}}$.\par
}
\end{document}