# Plot the square root function using TikZ

I want to draw the graph the function y=\sqrt{x} (and label it above the graph of the function) in an x-y plane. Then I need to put a filled dot on the point (4,0) (I want to label (4,0) beneath the dot, and I also I need to put a filled dot on an arbitrary point on the graph of the function and label (x,y) above the point. I need the graph for the following problem:

Find the point (x,y) on the graph of y=\sqrt{x} nearest the point (4,0).

I did the following, but for some reason I couldn't even get the graph of the square root function. Thanks!

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\draw[->] (-3,0) -- (7,0) node[right] {$x$};

\draw[->] (0,-3) -- (0,4) node[above] {$y$};

\draw[scale=0.5,domain=0:9,smooth,variable=\x,blue] plot ({\x},{\sqrt{\x}});

\end{tikzpicture}

\end{document}

• $x-y$ plane is a bad idea, the - will be rendered as a minus sign. Maybe $(x,y)$-plane? Aug 2, 2014 at 22:03

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
clip=false,
ymin=0,
xticklabels=\empty,
yticklabels=\empty,
legend pos=north west
]
\legend{$y=\sqrt{x}$}
\draw[fill] (axis cs:4,0) circle [radius=1.5pt] node[below right] {$(4,0)$};
\draw[fill] (axis cs:{4.5,sqrt(4.5)}) circle [radius=1.5pt] node[above left] {$(x,y)$};
\end{axis}
\end{tikzpicture}

\end{document}


• Thank you for your help. I need to highlight the point (4,0) on the x-axis. On the graph, you highlighted the point (4,2) Aug 2, 2014 at 17:26
• @Pat_Ho please see my updated answer. Aug 2, 2014 at 17:34

The cause of the error in your original code is the syntax you used to call the sqrt function:

({\x},{\sqrt{\x}})


should be replaced with

(\x,{sqrt(\x)})

• well spotted :) Nov 6, 2015 at 19:55

An easy-to-customize template with PSTricks.

\documentclass[pstricks,border=0pt,12pt,dvipsnames]{standalone}
\usepackage{amsmath}
\usepackage{pst-plot,pst-eucl}
\usepackage[nomessages]{fp}

\FPeval\XMin{0}
\FPeval\XMax{9}
\FPeval\YMin{0}
\FPeval\YMax{4}

\FPeval\XOL{0-1/2} % of DeltaX
\FPeval\XOR{1/2} % of DeltaX
\FPeval\YOB{0-1/2} % of DeltaY
\FPeval\YOT{1/2} % of DeltaY

\FPeval\DeltaX{1}
\FPeval\DeltaY{1}

\FPeval\AxisL{XMin+DeltaX*XOL}
\FPeval\AxisR{XMax+DeltaX*XOR}
\FPeval\AxisB{YMin+DeltaY*YOB}
\FPeval\AxisT{YMax+DeltaY*YOT}

\newlength\Width\Width=10cm
\newlength\Height\Height=8cm

\newlength\llx\llx=-5pt
\newlength\urx\urx=15pt
\newlength\lly\lly=-5pt
\newlength\ury\ury=15pt

\psset
{
llx=\llx,
lly=\lly,
urx=\urx,
ury=\ury,
labelFontSize=\scriptstyle,
xAxisLabel=$x$,
yAxisLabel=$y$,
algebraic,
plotpoints=120,
}

\def\f{sqrt(x)}

\begin{document}
\pslegend[rt]{%
\color{NavyBlue}\rule{12pt}{1pt} & \color{NavyBlue} $y=\sqrt x$
}
\begin{psgraph}
[
dx=\DeltaX,
dy=\DeltaY,
Dx=\DeltaX,
Dy=\DeltaY,
linecolor=gray,
tickcolor=gray,
ticksize=-3pt 3pt,
]{<->}(0,0)(\AxisL,\AxisB)(\AxisR,\AxisT){\dimexpr\Width-\urx+\llx}{!}%{\dimexpr\Height-\ury+\lly}
\psaxes
[
dx=\DeltaX,
dy=\DeltaY,
labels=none,
subticks=5,
tickwidth=.4pt,
subtickwidth=.2pt,
tickcolor=Red!30,
subtickcolor=ForestGreen!30,
xticksize=\YMin\space \YMax,
yticksize=\XMin\space \XMax,
subticksize=1,
](0,0)(\XMin,\YMin)(\XMax,\YMax)
\psplot[linecolor=NavyBlue]{0}{\XMax}{\f}
\pstGeonode[PointName={{(x,y)},{(4,2)},{(4,0)}},PosAngle=90]
(*2 {\f}){temp1}
(*4 {\f}){temp2}
(4,0){temp3}
\end{psgraph}
\end{document}


## Notes

Based on Herbert's comment below,

PointName={{(x,y)},{(4,2)},{(4,0)}}


is the correct syntax of my wrong syntax

PointName={(x{,}y),(4{,}2),(4{,}0)}

• The correct syntax is: \pstGeonode[PointName={{(x,y)},{(4,2)},{(4,0)}},PosAngle=90] otherwise TeX doesn't know which comma is part of the point name or a delimiter for the names.
– user2478
Aug 2, 2014 at 21:08
• ! Undefined control sequence. \c@lor@to@ps ->\PSTricks _Not_Configured_For_This_Format l.62 ...AxisR,\AxisT){\dimexpr\Width-\urx+\llx}{!}  Nov 9, 2022 at 10:59

And here's a simple approach with Metapost to extend the set of solutions.

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);

% define a unit size
u := 1cm;

% define the paths and point we need

% the y = sqrt(x) curve
path f;
f = (origin for x=0.1 step 0.1 until 6: .. (x,sqrt(x)) endfor) scaled u;

% the axes
path xx, yy;
xx = (0,-u/2+ypart llcorner f) -- (0,u/2+ypart urcorner f);
yy = (-u/2+xpart llcorner f,0) -- (u/2+xpart urcorner f,0);

% we need a point on a circle centred at (4,0) where it touches f
% so x=y^2  and (x-4)^2+y^2=r^2 where r^2 is minimal
% hence r^2 = x^2 - 7x + 16 and d(r^2)/dx = 2x-7
% so r^2 is minimal where x=7/2
% and our point is therefore (7/2,sqrt(7/2))

z1 = (4u,0);
z2 = (3.5u,sqrt(3.5)*u);

% we can add a circle to show this
path c;
c = fullcircle scaled 2 length (z2-z1) shifted z1;

% now draw everything in the right order
draw c withcolor .7 white;
draw f withcolor .67 red;
drawarrow xx withcolor .5 white;
drawarrow yy withcolor .5 white;

% and finally label the points
dotlabel.bot (btex $(4,0)$ etex,z1);
dotlabel.ulft(btex $(x,y)$ etex,z2);
% and the curve
label.rt(btex $y=\sqrt x$ etex, urcorner f);

endfig;
end.


## Notes

• As ever, the inline for-loop construct is very handy for defining function curves.

• If you define all the paths and points first and then draw them all together at the end, then it's a bit easier to get them drawn in the right order

• Once you've defined a path, you can use urcorner, llcorner, etc to refer to its bounding box. I've used this feature here to position the label for the function curve and to make axes that fit automatically.

\begin{tikzpicture}
\begin{axis} [xlabel=$x$, ylabel=$f(x)$, width=12cm]
\addplot [blue, line width = 1, smooth, domain=-2:2] {sqrt(x)};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[thick,major tick length=3mm,
axis lines=middle,
xmin=-1,xmax=10.5,ymin=-5.5,ymax=5.5,
xlabel={$x$},
ylabel={$y$},
axis line style={Triangle-Triangle},
tick label style={black, ultra thick},
major tick style = {thick, black},
xlabel style={at=(current axis.right of origin), anchor=west},
ylabel style={at=(current axis.above origin), anchor=south}
]