Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
$x-y$ plane is a bad idea, the - will be rendered as a minus sign. Maybe $(x,y)$-plane? –  mrc Aug 2 at 22:03

3 Answers 3

up vote 9 down vote accepted

Use pgfplots:

\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
]
\addplot+[mark=none,samples=200,unbounded coords=jump] {sqrt(x)};
\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}

enter image description here

share|improve this answer
    
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) –  Pat_Ho Aug 2 at 17:26
    
@Pat_Ho please see my updated answer. –  Gonzalo Medina Aug 2 at 17:34

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}

enter image description here

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)}
share|improve this answer
    
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. –  Herbert Aug 2 at 21:08
    
@Herbert: OK. Thanks. –  In PSTricks we trust Aug 2 at 21:14

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

enter image description here

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.