# Can not plot x^4, sqrt(x) or x^3 + 5x^2 using pgf/tikz

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?

-
Set the [smooth,samples=100, domain=-2:7], it worked. –  ferahfeza Dec 4 '13 at 10:43
thanks a lot, it did the job for x^3+2*x^2, but it still does not work for x^4 and sqrt(x)? –  Johan Klemmensen Dec 4 '13 at 10:51
Okay, I set the domain to 0:7, and it worked for sqrt(x). But still not for x^4? –  Johan Klemmensen Dec 4 '13 at 11:14
Why don't use {axis} environment for plotting functions? Maybe this help : tex.stackexchange.com/questions/54485/… –  ferahfeza Dec 4 '13 at 11:21

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}
\addlegendentry{$x^4$};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}
\addlegendentry{$\sqrt{x}$};
\end{axis}
\end{tikzpicture}
\end{document}


-

A solution with PSTricks.

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-plot}
\psset{algebraic}
\begin{document}
\begin{pspicture}(-2,-1)(2.5,10.5)
\psaxes{->}(0,0)(-2,-1)(2,10)[$x$,0][$y$,90]
\psplot[linecolor=red]{-1.5}{1.5}{x^4}
\psplot[linecolor=green]{0}{1.5}{sqrt(x)}
\psplot[linecolor=blue]{-1.25}{1.25}{x^3+5*x^2}
\end{pspicture}
\end{document}


-