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.

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.

share|improve this question
1  
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. –  Loop Space Nov 9 '11 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 '11 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 ;-) –  eudoxos Nov 9 '11 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?) –  eudoxos Nov 9 '11 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. –  Loop Space Nov 9 '11 at 12:08
show 1 more comment

1 Answer 1

up vote 4 down vote accepted

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}
share|improve this answer
add comment

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.