1

Is it possible to plot with TikZ/pgfplots the function f(x)=sin(\pi \cdot x)?

If so, how?

I want ti to look like the function here: enter image description here

  • 5
    (1) This is clearly a "Do-It-For-Me" question in my opinion -- please show more effort. It is your 13th question. (2) Here's one of many related already existing questions tex.stackexchange.com/questions/12951 or tex.stackexchange.com/questions/16232. (3) I edited the title of the question so that it is maybe more useful for others. – Dr. Manuel Kuehner Apr 3 '17 at 14:14
  • I barely know any tikz commands, and I tried doing a bit of coding that didn't work, like `\\addplot [blue, line width = 1, smooth, domain=-3:3] {sin(rad(\pi \cdot x)}; and some other ones. – Gal Grünfeld Apr 3 '17 at 14:18
  • Update: thank you, it helped - I discovered that I needed to use the * character for multiplication, and accidentally discovered that pi is the syntax for the symbol of pi (and not \pi, and I tried so far). Please don't downvote the post. – Gal Grünfeld Apr 3 '17 at 14:19
  • 3
    Then provide a MWE and try 3.14 instead of \pi and * instead of \cdot. Don't mix LaTeX commands for type setting stuff with math commands for plotting stuff. Just have a look at the pgfplots manual. – Dr. Manuel Kuehner Apr 3 '17 at 14:20
6

enter image description here

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
  \begin{axis}%
    [grid=both,
     minor tick num=4,
     grid style={line width=.1pt, draw=gray!10},
     major grid style={line width=.2pt,draw=gray!50},
     axis lines=middle,
     enlargelimits={abs=0.2}
    ]
    \addplot[domain=-1:3,samples=50,smooth,red] {cos(deg(pi*x))};
  \end{axis}
\end{tikzpicture}
\end{document}
  • Thanks a lot, but I already found accidentally how to do it by the time you commented with a bit of help from the link that @Dr. Manuel Kuehner provided above. – Gal Grünfeld Apr 3 '17 at 14:21
2

One possible:

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}



% CORES DE GRÁFICOS
\usepackage{xcolor} % configurações de cor
\definecolor{padrao-grafico-grid}{RGB}{195,220,227}
\definecolor{padrao-grafico1}{RGB}{67,128,134}
\definecolor{padrao-grafico2}{HTML}{1ABC9C}
\definecolor{padrao-grafico3}{RGB}{243,156,18}
\definecolor{padrao-grafico4}{HTML}{c0392b}
\definecolor{padrao-grafico5}{HTML}{8e44ad}



% GRÁFICOS 
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{my style/.append style={axis x line=middle, axis y line=
        middle, xlabel={$x$}, ylabel={$y$}, axis equal }}





\begin{document}


\begin{tikzpicture}
\begin{axis}[my style, grid=both, grid style={padrao-grafico-grid}, width=10cm, xtick={-1,0,...,3}, ytick={-1,0,...,1},
xmin=-2, xmax=4, ymin=-1.2, ymax=1.2]

% gráficos de funções   
\addplot+[mark=none,smooth,color=padrao-grafico2] (\x,{cos(pi*deg(x))});


\end{axis}
\end{tikzpicture} 



\begin{tikzpicture}
% remover grid - tirar grid=both, grid style={padrao_grafico4!15}, minor tick num=4
\begin{axis}[my style, grid=both, grid style={padrao-grafico-grid}, width=10cm, xtick={-3,-2,...,3}, ytick={-3,-2,...,3},
xmin=-4, xmax=4, ymin=-4, ymax=4]

%gráfico com legenda
\addplot+[mark=none,smooth,color=padrao-grafico1] (\x,{(e)^\x});
\label{plot_one}
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{$y=e^x$}

% gráficos de funções   
\addplot+[mark=none,smooth,color=padrao-grafico2] (\x,{\x^2});
\addplot+[mark=none,smooth,color=padrao-grafico3] (\x,{\x});
\addplot+[thick,mark=none,smooth,color=padrao-grafico4] (\x,{2*\x});
\addplot+[ultra thick,mark=none,smooth,color=padrao-grafico5] (\x,{3*\x});

% pontos
\addplot[smooth,mark=x,color=padrao-grafico1] coordinates{(2,3)};
\addplot[smooth,mark=o,color=padrao-grafico2] coordinates{(2,2)};
\addplot[smooth,mark=*,color=padrao-grafico3] coordinates{(2,1)};

% assintota
\addplot[dashed, color=padrao-grafico4] coordinates{(-3,-4) (-3,4)};

% ponto com coordenada
\addplot[smooth,mark=*,color=padrao-grafico3] coordinates{(3,-3)};
\addplot[dashed,very thin,color=padrao-grafico4] coordinates { (3,0) (3,-3) (0,-3) };

\addplot[mark=*] coordinates {(3,0)} node[pin=-50:{$(3,0)$}]{} ;

\addplot[mark=*,color=padrao-grafico4] coordinates {(2,-2)} node[right] {$A$};

\end{axis}
\end{tikzpicture} 


\end{document}
  • You should increase the samples (200 is usually ok) to have a more relevant sin function. – marsupilam Apr 3 '17 at 14:31
1

enter image description here

\documentclass[border=4mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
     axis lines=middle,clip=false,
            xmin=-3,xmax=4,ymin=-2,ymax=2,
            ytick={-1,1},
            xtick={-2,-1,0,1,2},
            xticklabels={$-2$,$-1$,$0$,$1$,$2$},
            xticklabel style={black},
            xlabel=$x$,
            ylabel=$y$]
      \addplot[domain=-2:2,samples=200,orange]{sin(deg(pi*x))}
                                node[right,pos=0.9,font=\footnotesize]{$f(x)=\sin \pi x$};
      \addplot[domain=-2:2,samples=200,magenta]{cos(deg(pi*x))}
                                node[right,pos=1,font=\footnotesize]{$f(x)=\cos \pi x$};
    \end{axis}
  \end{tikzpicture}
\end{document}

Not the answer you're looking for? Browse other questions tagged or ask your own question.