Wrong x-coordinate of intersection in tikz

Don't quite know what happens, but using perpendicular coordinates makes this a bit easier:

\fill[red] (D|-origin) circle [radius=2pt];

(D|-origin) means the x-coordinate of D and the y-coordinate of origin.


Just for comparison, here is a version in Metapost, where I have made MP do all the calculations for me, including finding the positive value of the function using the solve macro. To get the tangent line I just picked a likely point along the curve and used the feature that lets you extract the "direction" of the path at a particular point. For any given pair variable in MP, xpart and ypart let you extract the x and y coordinates.

enter image description here

This is wrapped up in luamplib so you will need to compile with lualatex.

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    numeric u;
    u = 1cm;

    % axes and ticks
    path xx, yy, ec_upper, ec_lower;
    xx = (left--right) scaled 4.8u;
    yy = xx rotated 90;

    drawoptions(withcolor 1/4 white);
    drawarrow xx; label.rt ("$x$", point 1 of xx);
    drawarrow yy; label.top("$y$", point 1 of yy);
    for i=-4 step 2 until 4:
        if i <> 0:
            draw (left--right) scaled 3 shifted (0, i*u); label.lft("$" & decimal i & "$", (-3, i*u));
            draw (down--up) scaled 3 shifted (i*u, 0); label.bot("$" & decimal i & "$", (i*u, -3));
        fi
    endfor
    drawoptions();

    % define the function, find a good value for min_x
    vardef f(expr x) = sqrt(x**3 - 3x + 5) enddef;
    vardef fpos(expr x) = (x**3 - 3x + 5) < 0 enddef;
    numeric minx, maxx, s;
    minx = solve.fpos(-3, 0); % see pp.176-177 of the MetafontBook
    maxx = 3; s = 1/32;

    % define and draw the upper and lower parts of the path
    ec_upper = ((minx, 0) for x=minx+s step s until maxx+eps: -- (x, f(x)) endfor) scaled u;
    ec_lower = reverse ec_upper reflectedabout(left, right);

    drawoptions(withcolor 2/3 blue);
    draw ec_lower .. ec_upper;
    label.urt("$E$", point infinity of ec_upper);
    drawoptions();

    % find the tangent at P and the intersection with the upper part of the curve

    path line; pair P, PP; numeric p; 
    p = 34; % I experimented to find this value...

    P = point p of ec_upper;

    line = (left -- 6 right) scaled u 
        rotated angle direction p of ec_upper  % "direction t of path" gives tangent of path at time t
        shifted P;

    PP = line intersectionpoint reverse ec_upper; % reverse so we start at the other end

    % draw the orthogonal markers using "xpart" and "ypart" 
    draw (0, ypart PP) -- PP -- (xpart PP, 0) dashed withdots scaled 1/2;
    filldraw fullcircle scaled dotlabeldiam shifted (xpart PP, 0) withcolor 2/3 red;

    draw line withcolor 1/2 red;
    dotlabel.ulft("$P$", P);
    dotlabel.lrt("$P*P$", PP);

endfig;
\end{mplibcode}
\end{document}

Tags:

Tikz Pgf