2

I am trying to plot a function, which involves taking the logarithm of another function, over the domain (0,1).

The graph is not smooth as it nears 0, as you can see in the image below, which it produces. In fact, it is not well-behaved quite a long way away from 0. But it appears well around 1.

How can I produce a graph that appears smooth over the whole domain?

Code v1

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
    [xmin=0,xmax=1,xtick={0,1},ymin=0,ymax=0.12,ytick={0}]
    \addplot[domain=0:1,samples=100]{(1/2)*((1 - x)/x)*((ln((1 + x)/(1-x))/x) - 2)};
\end{axis}
\end{tikzpicture}
\end{document}

Output v1

graph

UPDATE:

Rearranging the expression to make the arguments in ln() simpler seems to help ln((1+x)/(1-x)) = ln(1+x)-ln(1-x). I also specified \pgfplotsset{compat=1.18} as specified by KersouMan, but that did not help me much, as you can see. I also added restrict y to domain={0:0.12} to get rid of some big spikes. Finally, I thought I would give it a break and remove values close to 0 from the domain, instead using domain=0:1, but I do not want to do that, really I want pgfplots to graph this smoothly to (0,0).

Any more ideas?

Code v2:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}
    [xmin=0,xmax=1,xtick={0,1},ymin=0,ymax=0.12,ytick={0}]
    \addplot[domain=0.03:1,samples=100,restrict y to domain={0:0.12}]{(1/2)*((1 - x)/x)*( (ln(1 + x) - ln(1 - x))/x - 2)};
\end{axis}
\end{tikzpicture}
\end{document}

Output v2

enter image description here

Specific replies:

Re: KersouMan - Unfortunately, that did not help me, I wonder how it helped you.

Re: Jasper Habicht - It is a smooth function that rises from y=0 when x=0 and tends back to y=0 when x=1. Yes, I also believe the issue is that pgfplots is having issues near 0 due to the function getting complicated.

4
  • 2
    Could you maybe provide an image how you want the plot to look like or double check whether the formula for it is right? It seems that the curve becomes quite complicated near zero which you can see if you increase the value for samples. I am unsure whether this curve is really what you're expecting. May 25 at 20:22
  • 1
    Wouldn't know why, but adding a \pgfplotsset{compat=1.18} and increasing the number of samples gave me a correct result
    – KersouMan
    May 26 at 6:45
  • Maybe the compatibility setting did not work because your pgfplots package is not up to date (unlikely since requiring a version ahead of what you have installed throws an error). Checking different versions, I have a correct result starting with compat=1.12.
    – KersouMan
    May 26 at 11:00
  • Thanks for the suggestion. I had updated the package, but it did not help. I also tried with all versions between compat=1.12 and compat=1.18. For my purposes taking an approximate function for the domain near 0, as suggested by Juan, is good enough. May 26 at 12:12

1 Answer 1

3

A workaround: use the Taylor polynomial for the logarithmic part:

\documentclass[border=1.618]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=1,xtick={0,1},ymin=0,ymax=0.12,ytick={0}]
  \addplot[domain=0.4:1,samples=100] {(1/2)*((1 - x)/x)*( (ln(1 + x) - ln(1 - x))/x - 2)};
  \addplot[red,domain=0.0001:0.4,samples=41]{(1 - x)*(x/3 + x^3/5 + x^5/7 + x^7/9)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .