I am trying to plot a couple functions in Latex, including an absolute and e function, these are depicted in the following image;
However using the code below, the image that results is:
What is going wrong?
\documentclass{article}\usepackage{tikz}\usepackage{pgfplots}\begin{document}\begin{figure}[h]\centering\begin{tikzpicture}\begin{axis}[ axis lines = left, xlabel = $t$, ylabel = {$f(t)$},]%Below the red parabola is defined\addplot [ domain=-0.5:0.5, samples=100, color=red,]{sqrt(abs(x))};\addlegendentry{$\sqrt{|t|}$}%Here the blue parabloa is defined\addplot [ domain=-0.5:0.5, samples=100, color=blue, ] {abs(x)};\addlegendentry{$|t|$}%Here the orange parabloa is defined\addplot [ domain=-0.5:0.5, samples=200, color=orange, ] {e^(abs(x)-1)}; \addlegendentry{$e^{|t|}-1$} %Here the purple parabloa is defined\addplot [ domain=-0.5:0.5, samples=100, color=purple, ] {e^(abs(2*x)-1)};\addlegendentry{$e^{|2t|}-1$}\end{axis}\end{tikzpicture}\caption{M1} \label{fig:M1}\end{figure}\end{document}- 1Welcome to TeX.SX! Please make your code compilable (MWE).TeXnician– TeXnician2017-04-06 07:41:32 +00:00CommentedApr 6, 2017 at 7:41
- A couple of things:too few samples, giving you the blunt tip on
$\sqrt{|t|}$; Don't you meane^(abs(x))-1? That would be$\mathrm{e}^{|t|}-1$as indicated, and would make sense ase^0=1and you wantorange_curve(0)=0, soorange_curveshould be(e^x)-1(superfluous brackets for emphasis)Chris H– Chris H2017-04-06 08:05:09 +00:00CommentedApr 6, 2017 at 8:05 - Note also that with an even number of samples and a domain that is symmetric around 0, you will not get a sample exactly at x=0. So try 101 samples, instead of 100.Torbjørn T.– Torbjørn T.2017-04-06 09:12:19 +00:00CommentedApr 6, 2017 at 9:12
1 Answer1
At least your red plot can be improved by increasing the samples with:
\addplot [ domain=-0.5:0.5, samples=1000, color=red, smooth,]{sqrt(abs(x))};Butpgfplots is right plotting your functions: the purple and orange one can not go to y-value 0 (manually calculating results in 1/e for x=0).
Update: Correcting your typos:
%Here the orange parabloa is defined\addplot [ domain=-0.5:0.5, samples=201, color=orange, smooth ] {e^(abs(x))-1}; \addlegendentry{$e^{|t|}-1$} %Here the purple parabloa is defined\addplot [ domain=-0.5:0.5, samples=201, color=purple, smooth ] {e^(abs(2*x))-1};\addlegendentry{$e^{|2t|}-1$}- I think the OP has a typo in the formula definition -- it doesn't match the legend.Chris H– Chris H2017-04-06 08:03:14 +00:00CommentedApr 6, 2017 at 8:03
- 5000 is a bit overkill, try 201. (Maybe more for
sqrt(abs(x)).)Torbjørn T.– Torbjørn T.2017-04-06 09:15:42 +00:00CommentedApr 6, 2017 at 9:15 - @TorbjørnT. Thanks for pointing that out. That was my test document before I looked at the formulas and corrected them...TeXnician– TeXnician2017-04-06 09:17:56 +00:00CommentedApr 6, 2017 at 9:17
You mustlog in to answer this question.
Explore related questions
See similar questions with these tags.


