Tikz 多项式非常不稳定

Tikz 多项式非常不稳定

我正在尝试绘制一个多项式,其系数是通过插值找到的。结果在 Tikz 中非常不稳定。请问我该如何改进?

\begin{tikzpicture}[scale=1]
\tikzset{declare function={f(\t)=0.01-0.0000186012 *(\t-8.852) * (\t-6)* (\t+0.0767179)* (\t+2)* (\t+5.90209) *(\t*\t-12.349*\t+72.7683) ;} } 

\def\xmin{-3} \def\xmax{8} \def\ymin{-3} \def\ymax{2}\def\xstep{0.2}\def\ystep{0.2}\def\major{1}\def\minor{1}

\draw [step=\minor,gray,very thin]  (\xmin,\ymin) grid (\xmax,\ymax);
\draw [step=\major,black,thin] (\xmin,\ymin) grid (\xmax,\ymax);
\draw [thick, ->] (\xmin,0)--(\xmax,0) node [below right] {$x$}; 
\draw [thick, ->] (0,\ymin)--(0,\ymax) node [above right] {$y$};
\foreach \x in {\xmin,...,\xmax }   \draw [very thick] (\x,3pt)--(\x,-3pt) node [below, blue ] {};  %\small\x
\foreach \y in {\ymin,...,\ymax }   \draw [very thick] (3pt,\y)--(-3pt,\y) node [left, blue ] {};   %\small\y
\node[below left] at (0,0) {0};
\node[below] at (1,0) {1};
\node[left] at (0,1) {1};
\clip (\xmin,\ymin) rectangle (\xmax,\ymax);

\draw[thick,blue,samples=500,domain=-3:8] plot (\x,{f(\x)}) node[left] {$f(x)$};
\end{tikzpicture}

我最大的努力!

答案1

这是标准算法的精度问题。一种可能性是使用 LaTeX3 核心的 FPU,如下所示:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{expl3}% not needed in recent LaTeX

\ExplSyntaxOn
\cs_new_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff

\usetikzlibrary{arrows.meta,positioning,calc}
\begin{document}
\begin{tikzpicture}[]

\def\xmin{-3} \def\xmax{8} \def\ymin{-3} \def\ymax{2}\def\xstep{0.2}\def\ystep{0.2}\def\major{1}\def\minor{1}

\draw [step=\minor,gray,very thin]  (\xmin,\ymin) grid (\xmax,\ymax);
\draw [step=\major,black,thin] (\xmin,\ymin) grid (\xmax,\ymax);
\draw [thick, ->] (\xmin,0)--(\xmax,0) node [below right] {$x$};
\draw [thick, ->] (0,\ymin)--(0,\ymax) node [above right] {$y$};
\foreach \x in {\xmin,...,\xmax }   \draw [very thick] (\x,3pt)--(\x,-3pt) node [below, blue ] {};  %\small\x
\foreach \y in {\ymin,...,\ymax }   \draw [very thick] (3pt,\y)--(-3pt,\y) node [left, blue ] {};   %\small\y
\node[below left] at (0,0) {0};
\node[below] at (1,0) {1};
\node[left] at (0,1) {1};
\clip (\xmin,\ymin) rectangle (\xmax,\ymax);

\draw[thick,blue,samples=500,domain=-3:8, ] plot (\x,{\fpeval{0.01-0.0000186012 *(\x-8.852) * (\x-6)* (\x+0.0767179)* (\x+2)* (\x+5.90209) *(\x*\x-12.349*\x+72.7683)}}) node[left] {$f(x)$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

...我不知道如何在中定义一个函数l3fp,并且为了安全起见,我会添加一个()around \x(我不知道是否需要)。

PD 我检查了使用情况:

\tikzset{declare function={f(\t)=\fpeval{0.01-0.0000186012 *(\t-8.852) * (\t-6)* (\t+0.0767179)* (\t+2)* (\t+5.90209) *(\t*\t-12.349*\t+72.7683)} ;} }

%[...]

\draw[thick,blue,samples=500,domain=-3:8] plot (\x,{f(\x)}) node[left] {$f(x)$};

确实有效,但我不确定这是否是作弊行为或者它是否会一直有效。

答案2

为了目的比较。

如果你知道图形方程,Asymptote 可以轻松地绘制它。

import graph;
import math;
unitsize(cm);

add(shift(-3,-3)*grid(11,5,gray));

int xmin=-3,xmax=8,ymin=-3,ymax=2;
real step=0.1;

draw(Label("$x$",EndPoint),(xmin,0)--(xmax,0),Arrow);
draw(Label("$y$",EndPoint),(0,ymin)--(0,ymax),Arrow);

for (int i=xmin; i<=xmax; ++i) draw((i,-step)--(i,step),linewidth(.8bp));
for (int j=ymin; j<=ymax; ++j) draw((-step,j)--(step,j),linewidth(.8bp));

real f(real t){return 0.01-0.0000186012*(t-8.852)*(t-6)*(t+0.0767179)*(t+2)*(t+5.90209)*(t*t-12.349*t+72.7683);}
label("$1$",(1,0),1.5dir(-90));
label("$0$",(0,0),1.5dir(-135));
label("$1$",(0,1),1.5dir(180));
draw(Label("$f(x)$",Relative(.99)),graph(f,xmin,xmax,500),blue);

shipout(bbox(mm,invisible));

在此处输入图片描述

相关内容