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}
  • 1
    Welcome to TeX.SX! Please make your code compilable (MWE). – TeXnician Apr 6 '17 at 7:41
  • @TeXnician Is it correct now? – Joel Apr 6 '17 at 7:49
  • Now it is compilable. – TeXnician Apr 6 '17 at 7:55
  • A couple of things:too few samples, giving you the blunt tip on $\sqrt{|t|}$; Don't you mean e^(abs(x))-1? That would be $\mathrm{e}^{|t|}-1$ as indicated, and would make sense as e^0=1 and you want orange_curve(0)=0, so orange_curve should be (e^x)-1 (superfluous brackets for emphasis) – Chris H Apr 6 '17 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. Apr 6 '17 at 9:12
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))};

But pgfplots 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$}
|improve this answer|||||
  • I think the OP has a typo in the formula definition -- it doesn't match the legend. – Chris H Apr 6 '17 at 8:03
  • 5000 is a bit overkill, try 201. (Maybe more for sqrt(abs(x)).) – Torbjørn T. Apr 6 '17 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 Apr 6 '17 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.