When I found that a simple x^(1.0/3.0)
does not yield a graph in PGFplots for negative values of x, I attempted to define my own function for CubeRoot using pgfmathdeclarefunction
as below. But, am not able to get them to work.
These are based on this example for a Gaussian distribution, and another one here.
This compiles as is, but has a problem if I set \DomainMin
to a negative value. The CubeRootB
function does not even compile, and I do not see what the problem with it is.
\documentclass{article}
\usepackage{tikz}
\newcommand*{\DomainMin}{0.1}
%\newcommand*{\DomainMin}{-2.0} % CubeRootA gives and error if DomainMin < 0
\tikzstyle{MyPlotStyle}=[domain=\DomainMin:2,samples=100,smooth]
\pgfmathdeclarefunction{gauss}{3}{% From links mentioned in the question
\pgfmathparse{1/(#3*sqrt(2*pi))*exp(-((#1-#2)^2)/(2*#3^2))}%
}
\pgfmathdeclarefunction{CubeRootA}{1}{%
\pgfmathparse{%
ifthenelse(equal(#1,abs(#1)),%
(#1)^(1.0/3.0),% #1 >= 0
-1.0*((abs(#1))^(1.0/3.0))% #1 is negative
)%
}%
}
\pgfmathdeclarefunction{CubeRootB}{1}{%
\pgfmathparse{%
\pgfmathifthenelse
{\pgfmathequal{#1}{\pgfmathabs{#1}}}%
{exp(ln(#1)/3.0)}% #1 >= 0
{-1.0*(exp(ln(abs(#1))/3.0))}% #1 is negative
}%
}
% Modified version of CubeRootB per Caramdir's suggestion
\pgfmathdeclarefunction{CubeRootC}{1}{%
\pgfmathparse{%
ifthenelse(
equal(#1,abs(#1)),%
exp(ln(#1)/3.0),% #1 >= 0
-1.0*(exp(ln(abs(#1))/3.0))% #1 is negative
)
}%
}
\begin{document}
\begin{tikzpicture}
\draw [help lines] (-2,-2) grid [step=0.5] (2,2);
\draw plot [MyPlotStyle] (\x,{gauss(\x,0,0.5)});
%
\draw plot [MyPlotStyle] (\x,{ CubeRootA(\x)});% Error if DomainMin < 0
%\draw plot [MyPlotStyle] (\x,{1.0+CubeRootB(\x)});% Does not compile
\draw plot [MyPlotStyle] (\x,{ CubeRootC(\x)});% Error if DomainMin < 0
\end{tikzpicture}
\end{document}