9

Here is my code:

\documentclass{article}
\usepackage{tikz} 
\begin{document}
\begin{tikzpicture}[x=1cm,y=1cm]
\draw[->,color=black] (-0.5,0) -- (3.5,0);
\draw[->,color=black] (0,-.5) -- (0,5);
\draw[very thick, smooth,samples=100,domain=0:3] plot(\x,{1/(\x-1)^(2/3)});
\end{tikzpicture}
\end{document}

which gives

! Package PGF Math Error: I cannot calculate the logarithm of `-1.0' (in '{1/(0
-1)^(2/3)}').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.7 ...s=100,domain=0:3] plot(\x,{1/(\x-1)^(2/3)})
                                                  ;
? 

Edit
I want something similar to the graph below (the shaded area isn't necessary):
enter image description here

3
  • 4
    As PGF math uses TeX’s mathematical engine (which isn’t made for such calculations) it substitutes certain functions by others (e.g. a^b = exp(b*ln(a))) or approximations. In this case it will fail trying ln(x-1) for x between 0 and 1. Would you be willing to use gnuplot for the calculations of these functions? Apr 20, 2013 at 6:32
  • @Qrrbrbirlbel yes, I would. But I want the axes format like my own code.
    – Sisabe
    Apr 20, 2013 at 6:38
  • 3
    You can also split it at x = 1 in its symmetrical components: \draw[domain=1.1:3] plot(\x,{1/(\x-1)^(2/3)});\draw[domain=0:.9] plot(\x,{1/(-(\x-1))^(2/3)}); Though, for plots, I’d recommend using pgfplots. Apr 20, 2013 at 6:38

4 Answers 4

8

With tkz-fct (tikz+ gnuplot + some macros of my package) you can get exactly your figure. I agree that the better tool to draw graphs of functions is pgfplots if you want something based on tikz. But with some very complex functions, you need to use pgfplots + gnuplot or something like this ...(pgfplots can use guplot). For the problem in x=1, Herbert's idea is fine, first ^2and then 1./3. but with something like ^(3/5) you need to split your domain. Last explanation : with gnuplot the power is get by ** and you need to write 1./3 to divide with real numbers.

\documentclass{article}
\usepackage{tkz-fct} 
\begin{document}
\begin{tikzpicture}
 % macro from tkz-fct
 \tkzInit[xmin=-.5,xmax=3.5,ymin=-.5,ymax=5]
 \tkzDrawXY[noticks]
 % part with gnuplot
 \tkzFct[domain = 0:3]{(1/(x-1)**2)**(1./3)}
 \tkzVLine[style      = dashed,
           line width = .6pt]{1}
 \tkzDrawArea[color=lightgray, domain =0:0.8,draw=black]% no need to rewrite the function
 % the macro uses the last function.
 \tkzDrawArea[color=lightgray, domain =1.3:3,draw=black]
 % \tkzDefPointByFct(.8) no need here but useful if you want a point of the curve
 % part with tikz
 \node[left] at (0,1) {1};
 \node[below] at (1,-0.5) {1};
 \node[below] at (3,0) {3};
\end{tikzpicture}
\end{document}

enter image description here

4
  • When I run your code, the graph isn't in the output. I have only the numbers and the axes.
    – Sisabe
    Apr 20, 2013 at 13:48
  • I'm still awaiting for your answer to my comment above!
    – Sisabe
    Apr 21, 2013 at 6:15
  • 1
    You need to install gnuplot and then you need to authorize the engine to access gnuplot with a correct path and with --shell-escape or to enable write18. There are some questions on the site about these problems. (the main problem is on OSX to install gnuplot) Apr 21, 2013 at 6:23
  • @AlainMatthes When I use your package tkz-fct, I alway lost the curves. Please help me. Dec 14, 2015 at 7:39
7

It needed to rewrite function as y = 1/(x-1)^2^(1/3) to plot it like..:

\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[smooth,xmin=0,xmax=3,ymin=0,ymax=2.4,axis x line=bottom,axis y line=left]
            \addplot[thick,red,domain=0:3] {1/(x-1)^2^(1/3)};
            \addplot[thin,dotted] coordinates {(1,0) (1,2.4)};
        \end{axis}
    \end{tikzpicture}
\end{document}

...this:

enter image description here

3
  • @m0nhawk could you please see my update above? Why is your output not similar to the graph I've attached?
    – Sisabe
    Apr 20, 2013 at 6:52
  • @Sisabe: Like this? Asymptote can be easily added to plot.
    – m0nhawk
    Apr 20, 2013 at 7:11
  • @m0nhawk: Two questions: 1- why the numbering of my y axis is 1, 1.5, 2, 2.5? 2- why did you put 1/3 in y = 1/|x-1|^(1/3) instead of 2/3?
    – Sisabe
    Apr 20, 2013 at 7:14
7

run with xelatex or pdflatex --shell-escape

\documentclass{article}
%\usepackage[pdf]{pstricks}% for `pdflatex --shell-escape`
\usepackage{pst-plot} 
\begin{document}
\begin{pspicture}(-1,-1)(3.5,3.5)
\psaxes{->}(0,0)(3,3)[$x$,-90][$y$,0]
\psplot[algebraic,plotpoints=1000,yMaxValue=3,
        linewidth=1.5pt,linecolor=red]{0}{3}{1/((x-1)^2)^(1/3)}
\psline[linestyle=dashed](1,0)(1,3)
\end{pspicture} 
\hspace{5mm}
\begin{pspicture}(-1,-1)(3.5,3.5)
\pscustom[fillstyle=solid,fillcolor=blue!20,linestyle=none]{
  \psplot[algebraic,plotpoints=1000,yMaxValue=3]{0}{0.8}{1/((x-1)^2)^(1/3)}
  \psplot[algebraic,plotpoints=1000,yMaxValue=3]{1.2}{3}{1/((x-1)^2)^(1/3)}
  \psline(*3 {1/((x-1)^2)^(1/3)})(3,0)(0,0)}
\psline[linewidth=0.36,linecolor=white](1,0)(1,3)
\psaxes{->}(0,0)(3,3)[$x$,-90][$y$,0]
\psplot[algebraic,plotpoints=1000,yMaxValue=3,
        linewidth=1.5pt,linecolor=red]{0}{3}{1/((x-1)^2)^(1/3)}
\psline[linestyle=dashed](1,0)(1,3)
\end{pspicture}
\end{document}

enter image description hereenter image description here

5

For filling under the curve you can draw twice with different fill color.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
\begin{axis}[xmax=3.5,xmin=0,ymin=0,ymax=7,
xtick={1,3},ytick=1,
axis x line=bottom,
axis y line=left,
width=4cm,height=5cm]
\addplot+[draw=black,fill=gray,domain=0:3,no marks,samples=50] {(1/(x-1)^2)^(1/3)} \closedcycle;
\addplot+[draw=black,fill=white,domain=0.8:1.4,no marks,samples=50] {(1/(x-1)^2)^(1/3)} \closedcycle;
\draw (axis cs:1,0) -- (axis cs:1,7);
\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 .