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
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
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.
samples
. I am unsure whether this curve is really what you're expecting.\pgfplotsset{compat=1.18}
and increasing the number ofsamples
gave me a correct resultpgfplots
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 withcompat=1.12
.compat=1.12
andcompat=1.18
. For my purposes taking an approximate function for the domain near 0, as suggested by Juan, is good enough.