[Tex/LaTex] Can not plot x^4, sqrt(x) or x^3 + 5x^2 using pgf/tikz

errorsplottikz-pgf

I have recently started using LateX, and I am beginning to use pgf/tikz to create plots. But I have encountered some problems with the functions stated above. I can plot the function f(x)=x^3, but when I try to add 5*x^2 to the function, it gives an error when compiled. The same happens, when i try to plot x^4 and sqrt(x). An exampe if this:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgf,tikz}           
\usetikzlibrary{arrows}
\usepackage{tikz}
\usepackage [hmargin=2cm,vmargin=3cm]{geometry}
\begin{document}
\definecolor{ttqqqq}{rgb}{0.2,0,0}
\definecolor{wqwqwq}{rgb}{0.38,0.38,0.38}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.5923130274930888cm,y=1.0cm]
\draw[->,color=black] (-2.09,0) -- (7.96,0);
\foreach \x in {-2,-1,1,2,3,4,5,6,7}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[->,color=black] (0,-4.76) -- (0,9.28);
\foreach \y in {-4,-2,2,4,6,8}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
\clip(-2.09,-4.76) rectangle (7.96,9.28);
\draw[smooth,samples=100, domain=-2:8] plot(\x,{(\x)^(3)+2*(\x)^2});
\end{tikzpicture}
\end{document}

When compiled it gives an error, but when I put a minus in front if (\x)^3, so the function is -(\x)^3+2*(\x}^2, it plots the function perfectly fine. Is there anyone who has an explanation for this?

Best Answer

As ferahfeza mentioned, you can use the axis environment to plot functions. Here is an example:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}
  \addplot {x^4};
  \addlegendentry{$x^4$};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}
  \addplot {sqrt(x)};
  \addlegendentry{$\sqrt{x}$};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here