![動機](https://rvso.com/image/400377/%E5%8B%95%E6%A9%9F.png)
我已經使用 GeoGebra 繪製一些圖形有一段時間了,因為我對 TikZ 完全陌生。到目前為止,我對使用 GeoGebra 創建的圖形感到滿意,儘管程式碼非常複雜。然而,這次我遇到了一個無法解決的錯誤。
錯誤訊息如下:
Dimension too large.
<to be read again>
\relax
l.34 \end{axis}
I can't work with sizes bigger than about 19 feet. Continue and I'll use the largest value I can.
雖然我收到了這條訊息,但我可以看到情節,如下所示:
我的(複雜的)程式碼如下所示:
\documentclass[10pt]{article}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm, scale = 1.2, transform shape]
\begin{axis}[
x=1.0cm,y=1.0cm,
axis lines=middle,
xmin=-1.129485186780251,
xmax=5.72744433329757,
ymin=-0.96997054991852,
ymax=5.616499082403562,
xtick={0.0},
ytick={0.0}, restrict y to domain=-0.96997054991852:5.616499082403562,]
\clip(-1.129485186780251,-0.76997054991852) rectangle (5.82744433329757,5.916499082403562);
\draw[line width=0.8pt,smooth,samples=100,domain=-3.129485186780251:10.42744433329757] plot(\x,{(\x)-2.25E-50});
\draw[line width=0.8pt,smooth,samples=100,domain=-3.129485186780251:10.42744433329757] plot(\x,{0-0.07123888916965247*(\x)^(4.0)+1.5096421986752417*(\x)^(3.0)-9.579592272284225*(\x)^(2.0)+23.93776936035761*(\x)-17.586966972004486});
\draw (5.196549774492815,0.49322601353710884) node[anchor=north west] {$x$};
\draw (0.020629571335991016,5.575885328535428) node[anchor=north west] {$f(x)$};
\draw [line width=0.8pt,dash pattern=on 2pt off 2pt] (1.5,1.5)-- (1.5,0.);
\draw [line width=0.8pt,dash pattern=on 2pt off 2pt] (3.,0.)-- (3.,3.);
\draw [line width=0.8pt,dash pattern=on 2pt off 2pt] (4.5,4.5)-- (4.501485877435332,0.);
\draw (1.2137496293807978,0.021729881572188525) node[anchor=north west] {$x_0$};
\draw (2.700437452518166,0.021729881572188525) node[anchor=north west] {$x_1$};
\draw (4.228710949030006,0.021729881572188525) node[anchor=north west] {$x_2$};
\begin{scriptsize}
\draw [fill=black] (1.5,1.5) circle (1.2pt);
\draw [fill=black] (4.5,4.5) circle (1.2pt);
\draw [fill=black] (3.,3.) circle (1.2pt);
\end{scriptsize}
\end{axis}
\end{tikzpicture}
\end{document}
已有用戶報告類似的錯誤訊息。有時添加restrict y to domain
似乎已經解決了問題,但就我而言,似乎沒有。有人可以幫忙解決我的問題嗎?
答案1
恕我直言,GeoCobra 是一個不能用來繪製圖表的工具。如果您想繪製一個函數,pgfplots
您加載的 是合適的,但並不是您不理解的某個黑盒子的所有輸出,這些輸出會產生這些錯誤。相反,您可以只繪製函數,讓 TikZ 找到交點等等。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:5,axis lines=middle,ymin=-1,ymax=6,xticklabels={},
yticklabels={},xlabel={$x$},ylabel={$f(x)$}]
\addplot[no marks,smooth,name path=curve]{0.5*(x-2)^3-0.5*(x-2)^2+2.25};
\addplot[no marks,name path=line]{x};
\path (0,0) coordinate (O);
\draw[dashed,name intersections={of=curve and line,total=\t}]
foreach \X in {1,...,\t} {(intersection-\X) node[circle,fill,inner sep=1pt]{}
-- (intersection-\X|-O) node[below]{$x_{\the\numexpr\X-1}$}};
\end{axis}
\end{tikzpicture}
\end{document}
因此,這並不是技術意義上對您問題的真正答案,而是試圖說服您不要使用像 GeoCobra 這樣的東西來解決存在替代方案的問題,這些替代方案可以讓您系統地解決問題。
答案2
動機
目前接受的答案沒有解釋原因
Dimension too large
restrict y to domain
在OP的例子中不起作用。
此外,它主要依賴硬編碼來切割曲線(我在本段的其餘部分x domain
簡稱為“ ”)。domain
我們必須先查看圖表(以視覺/分析方式),然後才能選擇合適的domain
. (多項式/指數)曲線可以在整個圖的右/左部分快速增加/減少。指示機器自動裁剪適當的domain
.
討論
我將從三個部分來分析錯誤:
- 最小問題範例(MPE)
- PGF 最大尺寸
- 簡要說明
restrict y to domain
MPE
生成的 TikZ 程式碼中的大部分行都很好。換句話說,可以丟棄那些正確的行而不影響錯誤訊息。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-5,
xmax=13.6,
ymin=-5,
ymax=6.55,
]
\clip (-5,-5) rectangle (13.6,6.55);
\draw (-4.9,-4.9) rectangle (13.3,6.5);
\draw[domain=-5:13.6] plot (\x,{1/2*(\x)^3-2*\x});
\end{axis}
\end{tikzpicture}
\end{document}
我的 MPE 和您的程式碼之間存在一些細微差別:
\documentclass
而是放大圖片並刪除周圍不必要的空白standalone
。article
compat=1.18
而不是compat=1.15
像我的 PDFLaTeX 建議的那樣。- 數字四捨五入到小數點後兩位,以便於閱讀。
- 我已經把這裡帶走了
restrict y to domain={...}
。即使您將其添加回我的 MPE,編譯器也會給出相同的錯誤。我將在本節的最後一小節中詳細說明這一點。 - 我已經在and中刪除了
x=1cm
和,但這很好,因為這些是 TikZ 和 PGF 圖中的預設值。y=1cm
\begin{tikzpicture}
\begin{axis}
我將留下視覺輸出
pdftoppm -png graph.pdf > graph.png
到下一小節。
此時,我們看到這個問題不是關於地理代數,因為上面的 MPE 可以在沒有 Geometry suite 的情況下建構。
PGF 最大尺寸
這相關問題的已接受答案 \usepgflibrary{fpu}
表示600cm
大於\maxdimen
,這是 TeX-dimensions 的最大可能值。 \maxdimen
是(2^14-2e-5)pt = 16383.99998pt
,但600cm
大於17000pt
,因此用作600cm
長度會觸發錯誤Dimension too large
。
給定的三次曲線定義為y=X立方/2 - 2X在[-5,13.6]上。什麼時候X= 13,y= 1072.5 > 600 x=1cm
。y=1cm
y- 座標1072.5cm
轉換為pt
,會超出\maxdimen
,導致錯誤。
簡要說明restrict y to domain
密鑰可在(第 4.22 節跳過或更改座標 - 過濾器的)PGF Plots 手冊,但 TikZ 手冊中沒有。我嘗試把它放在後面\draw[]
,系統無法識別該密鑰/tikz/restrict y to domain
。我也嘗試將其放在後面plot
,但這沒有幫助,因為它\draw
是 TikZ 命令而不是 PGF Plots 命令。
當您放入restrict y to domain
inside時\begin{axis}
,此 PGF Plots 鍵將套用於此環境內的每個\addplot
(或) ,但不適用於元素。\addplot+
axis
\draw
解決方案
在真正解決你的問題之前,我將先解決我的更簡單的問題。
MPE
轉換\draw[domain={...}] plot (\x,{...});
為\addplot[domain={...}] (x,{...});
。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-5,
xmax=13.6,
ymin=-5,
ymax=6.55,
restrict y to domain=-5:6.55,
]
\clip (-5,-5) rectangle (13.6,6.55);
\draw (-4.9,-4.9) rectangle (13.3,6.5);
\addplot[domain=-5:13.6] (x,{1/2*(x)^3-2*x});
\end{axis}
\end{tikzpicture}
\end{document}
這個限制尚未完全使用,但至少編譯器不再抱怨。
為了拉伸曲線,我發現這[3*ymin,3*ymax]
是一個合適的y domain
。我不知道縮放因子2
在這裡不起作用。
這種方法更容易編程:您可以(正規表示式)搜尋 的出現\begin{axis}[
,然後注入行「restrict y to domain=xxx:yyy
」。您可以開發函數來計算 和 的上限和下限xxx
,yyy
其ymin
數值ymax
可以透過程式提取。例如,我只是將兩個數字乘以3
。
原始範例
\draw [...] plot (\x,{...})
我只更改了包含to的兩行程式碼,\addplot [...] (x,{...})
並\x
替換為 by ,x
而沒有觸及方括號內的內容[...]
。
\documentclass[10pt]{article}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm, scale = 1.2, transform shape]
\begin{axis}[
x=1.0cm,y=1.0cm,
axis lines=middle,
xmin=-1.129485186780251,
xmax=5.72744433329757,
ymin=-0.96997054991852,
ymax=5.616499082403562,
xtick={0.0},
ytick={0.0}, restrict y to domain=-0.96997054991852:5.616499082403562,]
\clip(-1.129485186780251,-0.76997054991852) rectangle (5.82744433329757,5.916499082403562);
\addplot[line width=0.8pt,smooth,samples=100,domain=-3.129485186780251:10.42744433329757] (x,{(x)-2.25E-50});
\addplot[line width=0.8pt,smooth,samples=100,domain=-3.129485186780251:10.42744433329757] (x,{0-0.07123888916965247*(x)^(4.0)+1.5096421986752417*(x)^(3.0)-9.579592272284225*(x)^(2.0)+23.93776936035761*(x)-17.586966972004486});
\draw (5.196549774492815,0.49322601353710884) node[anchor=north west] {$x$};
\draw (0.020629571335991016,5.575885328535428) node[anchor=north west] {$f(x)$};
\draw [line width=0.8pt,dash pattern=on 2pt off 2pt] (1.5,1.5)-- (1.5,0.);
\draw [line width=0.8pt,dash pattern=on 2pt off 2pt] (3.,0.)-- (3.,3.);
\draw [line width=0.8pt,dash pattern=on 2pt off 2pt] (4.5,4.5)-- (4.501485877435332,0.);
\draw (1.2137496293807978,0.021729881572188525) node[anchor=north west] {$x_0$};
\draw (2.700437452518166,0.021729881572188525) node[anchor=north west] {$x_1$};
\draw (4.228710949030006,0.021729881572188525) node[anchor=north west] {$x_2$};
\begin{scriptsize}
\draw [fill=black] (1.5,1.5) circle (1.2pt);
\draw [fill=black] (4.5,4.5) circle (1.2pt);
\draw [fill=black] (3.,3.) circle (1.2pt);
\end{scriptsize}
\end{axis}
\end{tikzpicture}
\end{document}
觀察你的圖表中間非常小。
去做
是時候進行一些字串操作了:
- 將每個小數四捨五入到小數點後兩位如果需要的話
- 替換
[10pt]{article}
為{standalone}
以使輸出影像更大。 - 注射
restrict y domain={...},
以避免Dimensions too large
。