# finding points on path with given x-coordinate (marking f(x) points for given x's)

A y=f(x) function is given as path; for a set of x-coordinates, I want to place node (x,f(x)) onto the path, and connect it with (x,0) with vertical line.

I have not got very far myself:

\tikz{
\def\fSpec{ (-2,-2.2) .. controls (-1,2.5) and (.5,-1.7) .. (2,1.6) }
\def\xxx{{-1.9,-1.3,-.8,-.2,.4,.9,1.5,2.0}}
\draw[thin] \fSpec;
\foreach\x in \xxx{
%% how to find f(\x) here?
}
}


I've know about the intersections library, but it seems like overkill; I would like to learn something more elegant.

I cannot give parametrically along the path length, because there will be multiple functions with different shapes and the points need to have the same x-coordinates for all of them.

• If you can't specify the functions exactly in the form y = f(x) then the intersections library is most definitely not overkill. Otherwise you are trying to solve p(t) = x and then substitute back in for y = q(t). The first of these involves solving a cubic which, although possible, is not pretty. The routine used by the intersection library for finding intersections is, I think, really quite an elegant solution to this problem. Nov 9, 2011 at 11:04
• @eudoxos: Is your curved path just something you drew "by hand"? In that case, you might be better off just finding a third-order polynomial that looks appropriate for your purpose, and then using that in your drawing.
– Jake
Nov 9, 2011 at 11:18
• @Jake: Thanks for the tip, I had it as back-up solution. I have to delve into how to evaluate expressions with PGF then ;-) Nov 9, 2011 at 11:51
• @AndrewStacey: can you convert the comment to answer so that I can accept it? (Or is there a way I can do it myself?) Nov 9, 2011 at 11:53
• Do you need anything beyond what Jake has said? I really do recommend the intersections library for this if "by eye" isn't accurate enough. Nov 9, 2011 at 12:08

Andrew Stacey said it in the comments: The only alternative to the intersections library is to provide the curve as a function. Here's one that I fitted by eye: Your curve is shown in light gray. If you don't need to recreate the curve very closely, you could of course make do with a simpler function.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\tikz{
\def\fSpec{ (-2,-2.2) .. controls (-1,2.5) and (.5,-1.7) .. (2,1.6) }
\def\xxx{-1.9,-1.3,-.8,-.2,.4,.9,1.5,2.0}
\draw [gray!50] \fSpec;
\foreach \x in \xxx{
\draw (\x,0) -- ({\x},{0.25+1.3*(\x+0.15)^3/5-(\x+0.15)^2/5-\x/10});
}
\draw [red] plot [domain=-2:2] ({\x},{0.25+1.3*(\x+0.15)^3/5-(\x+0.15)^2/5-(\x)/10});
}

\end{document}