5

I constructed the curve of the mentioned function but it is not displayed correctly.

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
   \begin{axis}[
       xlabel=$x$,
       ylabel=$y$,
       domain=0:0.6,
       samples=100,
       axis lines=middle,
       xmin=0, xmax=0.6,
       ymin=-0.4, ymax=0.2,
       enlargelimits=true,
       xtick={0,0.1,0.2,0.3,0.4,0.5,0.6},
       ytick={-0.1,0,0.1,0.2},
       grid=both,
       width=10cm,
       height=7cm,
       ]
       \addplot[blue, ultra thick] {x*e^(1/x) - e^((1-x^2)/x) - x - 1};
       \addlegendentry{\( (\mathscr{C}_h) \)}
      \end{axis}
 \end{tikzpicture}
\end{document}

That's what I want

enter image description here

Dots
  • 169
  • 5
  • 4
    What do you mean, exactly by "it is not displayed correctly." ? – jlab Mar 28 '24 at 10:57
  • @jlab When compiling my code, I have a skewed appearance of the curve.

    I tried to zoom by reducing xmax and ymax but it's the same.

    – Dots Mar 28 '24 at 11:20
  • Yes I'll add it – Dots Mar 28 '24 at 11:37
  • Try to avoid using ^ in calculations. For example e^x would be calculated as exp{x* log(e)) where log(e) will be calculated using the numerical value of e. Similarly x^2 will be translated as x*x if you are lucky, and exp(2*log(x)) otherwise. – John Kormylo Mar 28 '24 at 17:07
  • Your domain start at exactly 0, where the function is undefined (and behaves badly near it). – Rmano Mar 28 '24 at 18:53

1 Answers1

8

You get a similar plot if you plot it on a similar domain:

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
   \begin{axis}[
       xlabel=$x$,
       ylabel=$y$,
       domain=0.55:4,
       samples=100,
       axis lines=middle,
%       xmin=0, xmax=4,
%       ymin=-0.4, ymax=0.2,
       enlargelimits=true,
%       xtick={0,0.1,0.2,0.3,0.4,0.5,0.6},
%       ytick={-0.1,0,0.1,0.2},
       grid=both,
       width=10cm,
       height=7cm,
       ]
       \addplot[blue, ultra thick] {x*e^(1/x) - e^((1-x^2)/x) - x - 1};
%       \addlegendentry{\( (\mathscr{C}_h) \)}
      \end{axis}
 \end{tikzpicture}
\end{document}

enter image description here

(I would avoid setting the min/max and ticks manually, pgfplots is usually pretty good at it)