2

I am trying to plot a couple functions in Latex, including an absolute and e function, these are depicted in the following image;enter image description here

However using the code below, the image that results is:enter image description here

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}
TeXnician's user avatar
TeXnician
34.3k9 gold badges69 silver badges135 bronze badges
askedApr 6, 2017 at 7:39
Joel's user avatar
5
  • 1
    Welcome to TeX.SX! Please make your code compilable (MWE).CommentedApr 6, 2017 at 7:41
  • @TeXnician Is it correct now?CommentedApr 6, 2017 at 7:49
  • Now it is compilable.CommentedApr 6, 2017 at 7:55
  • 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=1 and you wantorange_curve(0)=0, soorange_curve should be(e^x)-1 (superfluous brackets for emphasis)CommentedApr 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.CommentedApr 6, 2017 at 9:12

1 Answer1

3

At least your red plot can be improved by increasing the samples with:

plots

\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:

corrected plots

%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$}
answeredApr 6, 2017 at 8:00
TeXnician's user avatar
3
  • I think the OP has a typo in the formula definition -- it doesn't match the legend.CommentedApr 6, 2017 at 8:03
  • 5000 is a bit overkill, try 201. (Maybe more forsqrt(abs(x)).)CommentedApr 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...CommentedApr 6, 2017 at 9:17

You mustlog in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.