如何使用 tikz 繪製雙曲函數

如何使用 tikz 繪製雙曲函數

我正在嘗試使用 tikz 繪製這個 Ewan webster 函數:

在此輸入影像描述

上面的圖像是在 python 中使用 numpy 函數產生的:

import numpy as np
import matplotlib.pyplot as plt
 
a = 0
b = 2
x = np.linspace(a,b,points)
y = np.exp(x)* np.sin(100*np.cosh(x)) 
plt.plot(x,y)

但我的劇情並沒有如預期進行。我的程式碼如下:

\documentclass[tikz]{standalone}
\usepackage{tikz}

\begin{document}    
\begin{tikzpicture}[domain=0:2]

  \draw[->] (-0.2,0) -- (3,0) node[right] {$x$};
  \draw[->] (0,-1.2) -- (0,4) node[above] {$f(x)$};

  \draw[color=red]    plot (\x,\x)             node[right] {$f(x) =x$};
  \draw[color=orange] plot (\x,{exp(\x)*sin(100*cosh(\x))}) node[right] {$f(x) = \frac{1}{20} \mathrm e^x$};
\end{tikzpicture}
\end{document}

答案1

我已經更正了你的程式碼。

您需要指定在正弦參數中使用弧度(與 一起r)。

我修改了 x 尺度和 y 尺度(因為指數成長很快)。

我添加了smooth關鍵點,因此曲線被平滑,而不是在點之間繪製直線。

我已將樣本增加到 500 點(預設為 25)。

但我必須將繪圖域限制為0:1.7而不是0:2因為除非那樣,TikZ 抱怨尺寸太大錯誤。

\documentclass[tikz]{standalone}

\begin{document}    
\begin{tikzpicture}[domain=0:1.70,samples=500,smooth,xscale=3,yscale=0.5]

  \draw[->] (-0.2,0) -- (2.5,0) node[right] {$x$};
  \draw[->] (0,-5) -- (0,5) node[above] {$f(x)$};

  \draw[color=orange] plot (\x,{exp(\x)*sin(100*cosh(\x) r)}) node[right] 
      {$f(x) = \mathrm e^x\sin\left(100\cosh\left(x\right)\right)$};
\end{tikzpicture}
\end{document}

在此輸入影像描述

為了尺寸太大錯誤,而不是使用 TikZ,我們可以使用pgf圖

這是第一次嘗試:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}

\begin{tikzpicture}
\begin{axis}[samples=500,domain=0:2]
\addplot[orange]plot (\x, {exp(\x)*sin(100*cosh(\x) r)});
\end{axis}
\end{tikzpicture}

\end{document}

在此輸入影像描述

如您所見,最終並不完美。

我們可以將繪製分成兩個域,後一個域使用更密集的取樣,使 x 在 1.2 和 2 之間。

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}

\begin{tikzpicture}
\begin{axis}
\addplot[orange,samples=500,domain=0:1.5]plot (\x, {exp(\x)*sin(100*cosh(\x) r)});
\addplot[orange,samples=1000,domain=1.5:2]plot (\x, {exp(\x)*sin(100*cosh(\x) r)});
\end{axis}
\end{tikzpicture}

\end{document}

我們獲得了更好的繪圖:

在此輸入影像描述

我們可以客製化簡報:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}

\begin{tikzpicture}
\begin{axis}[axis x line=middle,axis y line=left,xlabel=$x$,legend pos=north west]
\addplot[orange,samples=500,domain=0:1.5]plot (\x, {exp(\x)*sin(100*cosh(\x) r)});
\addplot[orange,samples=1000,domain=1.5:2]plot (\x, {exp(\x)*sin(100*cosh(\x) r)});
\legend{\textcolor{orange}{$\mathrm e^x\sin\left(100\cosh\left(x\right)\right)$}}
\end{axis}
\end{tikzpicture}

\end{document}

在此輸入影像描述

答案2

Asymptote 對數學圖有更多的控制。在這種情況下,樣本點的數量n=1000和連接類型可以Hermite很好地協同工作。

在此輸入影像描述

// http://asymptote.ualberta.ca/
import graph;
unitsize(5cm,4mm);
real a=0,b=2;
real y(real x) {return exp(x)*sin(100*cosh(x));}
path gr=graph(y,a,b,n=1000,Hermite);
draw(gr,magenta);
axes();

shipout(bbox(5mm,invisible));

相關內容