Math & ScienceDrawing function in tikzpicture

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
m3talac
Posts: 1
Joined: Sun Jan 10, 2016 6:31 am

Drawing function in tikzpicture

Post by m3talac »

I have to draw function with \plot but it says "Dimension too large", this is my code:

Code: Select all

\begin{tikzpicture}
\begin{axis}[grid=major, xmin=0, xmax=3, ymin=-1.5, ymax=1.5, xlabel=$t$, ylabel=$x(t), y(t)$, scale=0.3]
\plot[blue] plot[samples=100, smooth] expression{(1 + 2 * x)*exp(-2*x) * sin(280* 3.14 * x)};
\plot[red, dashed] plot[samples=100, smooth] expression{ (1+2*x) * exp(-2*x)};
\end{axis}
\end{tikzpicture}
Hope you can help me because i need it in few days, there are picture how it should look:
plot.jpg
plot.jpg (47.71 KiB) Viewed 32417 times
and this are functions:
formula.jpg
formula.jpg (15.87 KiB) Viewed 32417 times
Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Drawing function in tikzpicture

Post by Stefan Kottwitz »

Hi,

welcome to the forum!

I would use pgfplots for this. Have a look at the pgfplots gallery for examples.

To avoid that "Dimension too large" error, you can restrict the y value as I do here:

Code: Select all

\documentclass[border=10pt]{standalone} 
\usepackage{pgfplots}
\pgfplotsset{%
  every tick label/.append style = {font=\tiny},
  every axis label/.append style = {font=\scriptsize}
}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[grid=major, xmin=0, xmax=3, ymin=-1.1, ymax=1.1,
    xlabel=$t$, ylabel={$x(t), y(t)$},
    xtick = {0,0.5,...,3}, ytick = {-1,-0.5,...,1},
    scale=0.3, restrict y to domain=-1:1]
    \addplot[blue, samples=100, smooth, unbounded coords=discard]
      plot (\x, { 1 + 2 * \x)*exp(-2*\x) * sin(280* 3.14 * \x) });
    \addplot[red, dashed, samples=100, smooth]
      plot (\x, { (1+2*\x) * exp(-2*\x) } );
    \addplot[red, dashed, samples=100, smooth]
      plot (\x, { -(1+2*\x) * exp(-2*\x) } );
  \end{axis}
\end{tikzpicture}
\end{document}
plot.png
plot.png (15.7 KiB) Viewed 33943 times
Stefan
LaTeX.org admin
GKS
Posts: 2
Joined: Fri Jul 08, 2016 9:35 pm

Drawing function in tikzpicture

Post by GKS »

Hello,

I would like to mention, that there is a little mistake/typo in your code, Stefan_K. It should be

Code: Select all

sin(180* 3.14 * \x)
Then the Graph fits to the so called envelope.

For better convenience

Code: Select all

\documentclass[border=10pt]{standalone}
    \usepackage{pgfplots}
    \pgfplotsset{%
      every tick label/.append style = {font=\tiny},
      every axis label/.append style = {font=\scriptsize}
    }
    \begin{document}
    \begin{tikzpicture}
      \begin{axis}[grid=major, xmin=0, xmax=3, ymin=-1.1, ymax=1.1,
        xlabel=$t$, ylabel={$x(t), y(t)$},
        xtick = {0,0.5,...,3}, ytick = {-1,-0.5,...,1},
        scale=0.3, restrict y to domain=-1:1]
        \addplot[blue, samples=100, smooth, unbounded coords=discard]
          plot (\x, { 1 + 2 * \x)*exp(-2*\x) * sin(180* 3.14 * \x) });
        \addplot[red, dashed, samples=100, smooth]
          plot (\x, { (1+2*\x) * exp(-2*\x) } );
        \addplot[red, dashed, samples=100, smooth]
          plot (\x, { -(1+2*\x) * exp(-2*\x) } );
      \end{axis}
    \end{tikzpicture}
    \end{document}
Greeetings GKS
Attachments
corrected plot
corrected plot
plotversion2.png (28.6 KiB) Viewed 33294 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Re: Drawing function in tikzpicture

Post by Stefan Kottwitz »

Hi GKS,

thank you for the correction!

And welcome to the forum,

Stefan
LaTeX.org admin
Post Reply