1

enter image description here% I tried using cosinus because it shows what I need to show, but im really not getting anywhere at this point.

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}

\begin{figure}[h]
\begin{center}
\begin{tikzpicture}
\begin{axis}[
grid=both, 
grid style={line width=.1pt, draw=gray!40}, 
axis lines=left, 
ylabel=y, 
xlabel=x]
\addplot[domain=0:4.5, samples=100, color=red]{cos(\x) r};
\addlegendentry{\cos(x)}
\end{axis}
\end{tikzpicture}
\end{center}
\end{figure}

\end{document}
6
  • Welcome to TeX.SE. It's usually helpful to also include your question in the body, and not just the title. Then you can make the title more concise and specific to your question. It's also not clear what you want: do you want to plot cosine, but it's not working? That's different than not knowing its function, or copying with pgfplots. And have you tried cos(\x r)? – Teepeemm Feb 25 at 15:43
  • 1
    Do you have a picture of the function you tried to draw? Maybe we could recognize it or even guess. – SebGlav Feb 25 at 16:10
  • Off-topic: you use ancient pgfplots package! Upgrade it to recent version (1.17) ... – Zarko Feb 25 at 17:27
  • 2
    Could be a polynomial function of degree 5 (see for example bookdown.org/tpinto_home/Beyond-Linearity/…). Wikipedia has some explanation on the expressions used to draw this kind of graph: en.wikipedia.org/wiki/Polynomial#Graphs – Marijn Feb 26 at 10:14
  • @Zarko, just because OP is stating compat=1.3 that does not mean that he has installed that version. (When I answer to questions I always state the minimal required compat level which gives the same result as the version I used to create that image, which I state in a comment in the first line of my code. – Stefan Pinnow Feb 26 at 13:25
1

We also not know which function you like to have.

Edit:

  • it seems that you like to approximate function showed on added image
  • it is not clear, if you also like to have filled as is shown on image, so I limited myself only to curve of function
  • one possible way to draw this function is to estimate coordinates of all extremes of showed function, collect them as coordinates and than write function with option smooth (which use spline functions through estimated coordinates):
\documentclass[a4paper,10pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17} % <------

    \begin{document}
    \begin{figure}[h]
\centering
    \begin{tikzpicture}
\begin{axis}[
    axis lines=left,
    grid,
    ylabel=$y$,
    xlabel=$x$,
    xmin=-4, xmax=8,
    ymin=-6, ymax=6,
    legend pos=south west,
    no marks,
    smooth] % <----
\addplot +[very thick] coordinates
    { (-3,2) (-2,4) (0,-3) (3,2) (5,-3) (7,4.5) };
    \legend{$y(x)$}
\end{axis}
    \end{tikzpicture}
    \end{figure}
\end{document}

enter image description here

2
  • I am so sorry, I forgot to add the picture of the function which I need. – FriendlyGuy96 Feb 26 at 9:53
  • @FriendlyGuy96, see edited answer. Is now proposed solution what you after? – Zarko Feb 27 at 2:13
1

Very strange question indeed. Your code is wrong, you might prefer this one:

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}

\begin{figure}[h]
\begin{center}
\begin{tikzpicture}
\begin{axis}[
grid=both, 
grid style={line width=.1pt, draw=gray!40}, 
axis lines=left, 
ylabel=y, 
xlabel=x]
\addplot[domain=0:4.5, samples=100, color=red] {cos(\x r)};
\addlegendentry{$\cos(x)$}
\end{axis}
\end{tikzpicture}
\end{center}
\end{figure}

\end{document}

cos(x)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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