# Intersection of two tkz-fct funktions

Is it possible to mark the intersection point of two tkz-fct functions and print it's coordinates?

For example in this case:

\documentclass{article}

\usepackage{tkz-fct}
\begin{document}
\begin{tikzpicture}
\tkzInit[ymax=15,xmax=4,ystep=5]
\tkzAxeXY
\tkzFct[color=red,domain=0:4]{2*x**2+5}
\tkzFct[color=blue,domain=0:4]{-x**3+15}
\end{tikzpicture}
\end{document}


-
When you say "get", do you mean that you want the numeric coordinates, or do you just want to mark the intersection? – Jake Apr 30 '12 at 21:20
Related: Coordinates of intersections – Jake Apr 30 '12 at 21:24
@Jake I want both. – student Apr 30 '12 at 21:29

It is possible if you use TikZ directly to plot and combine it with the intersections library.

\documentclass{article}

\usepackage{tkz-fct}
\usetikzlibrary{intersections}

\tikzset{
name plot/.style={every path/.style={name path global=#1}}
}

\begin{document}
\begin{tikzpicture}
\tkzInit[ymax=15,xmax=4,ystep=5]
\tkzAxeXY
%
\tkzFct[name plot=A,color=red,domain=0:4]{2*x**2+5}
%
\tkzFct[name plot=B,color=blue,domain=0:4]{-x**3+15}
%
\draw [name intersections={of=A and B, by=AxB}] (AxB) circle (2pt);
\end{tikzpicture}
\end{document}


With the following line you can read out the absolute coordinates of the intersection point as it is placed in the resulting picture. With some calculations you can get the coordinate system values I guess …

\draw [name intersections={of=A and B, by=AxB}] let \p1=(AxB) in
(AxB) circle (1pt) node [right] {\x1,\y1};


File list

 *File List*
article.cls    2007/10/19 v1.4h Standard LaTeX document class
size10.clo    2007/10/19 v1.4h Standard LaTeX file (size option)
tkz-fct.sty    2011/06/01 1.16 c function
tkz-base.sty    2011/06/01 1.16 c tkz-base
etex.sty    1998/03/26 v2.0 eTeX basic definition package (PEB)
tikz.sty    2010/10/13 v2.10 (rcs-revision 1.76)
pgf.sty    2008/01/15 v2.10 (rcs-revision 1.12)
pgfrcs.sty    2010/10/25 v2.10 (rcs-revision 1.24)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
pgfrcs.code.tex
pgfcore.sty    2010/04/11 v2.10 (rcs-revision 1.7)
graphicx.sty    1999/02/16 v1.0f Enhanced LaTeX Graphics (DPC,SPQR)
keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
graphics.sty    2009/02/05 v1.0o Standard LaTeX Graphics (DPC,SPQR)
trig.sty    1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg    2010/04/23 v1.9 graphics configuration of TeX Live
pdftex.def    2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/message (HO)
ltxcmds.sty    2011/04/18 v1.20 LaTeX kernel commands for general use (HO)
pgfsys.sty    2010/06/30 v2.10 (rcs-revision 1.37)
pgfsys.code.tex
pgfsyssoftpath.code.tex    2008/07/18  (rcs-revision 1.7)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
pgfcore.code.tex
pgfcomp-version-0-65.sty    2007/07/03 v2.10 (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v2.10 (rcs-revision 1.1)
pgffor.sty    2010/03/23 v2.10 (rcs-revision 1.18)
pgfkeys.sty
pgfkeys.code.tex
pgffor.code.tex
tikz.code.tex
numprint.sty    2008/02/17 v1.38 Print numbers (HH)
array.sty    2008/09/09 v2.4c Tabular extension package (FMi)
fp.sty    1995/04/02
defpattern.sty    1994/10/12
fp-basic.sty    1996/05/13
fp-snap.sty    1995/04/05
fp-exp.sty    1995/04/03
fp-trigo.sty    1995/04/14
fp-pas.sty    1994/08/29
fp-random.sty    1995/02/23
fp-eqn.sty    1995/04/03
fp-upn.sty    1996/10/21
fp-eval.sty    1995/04/03
tkz-tools-utilities.tex
tkz-tools-arith.tex
tkz-tools-base.tex
tkz-tools-misc.tex
tkz-tools-math.tex
tkz-obj-points.tex
tkz-obj-segments.tex
tkz-obj-marks.tex
supp-pdf.mkii
pdftexcmds.sty    2011/04/22 v0.16 Utilities of pdfTeX for LuaTeX (HO)
ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO)
ifpdf.sty    2011/01/30 v2.3 Provides the ifpdf switch (HO)
epstopdf-base.sty    2010/02/09 v2.5 Base part for package epstopdf
grfext.sty    2010/08/19 v1.1 Managing graphics extensions (HO)
kvdefinekeys.sty    2011/04/07 v1.3 Defining keys (HO)
kvoptions.sty    2010/12/23 v3.10 Keyval support for LaTeX options (HO)
kvsetkeys.sty    2011/04/07 v1.13 Key value parser (HO)
etexcmds.sty    2011/02/16 v1.5 Prefix for e-TeX command names (HO)
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live
***********

-
@Altermundus, wouldn’t it be possible to apply name path to \tkzFct? I tried it but it doesn’t work – I guess the options of \tkzFct are send to plot and not to \draw or something like that … – Tobi Apr 30 '12 at 21:30
You can use every path/.style={name path global=A} in the options of the \tkzFct. – Jake Apr 30 '12 at 21:35
@Jake: Thanks :-) – Tobi Apr 30 '12 at 21:39
I made a change to tkf-fct to use name path directly – Alain Matthes Apr 30 '12 at 22:06
@jake : fine solution ! because I forgot global with name path globaland it's why I remove a scope but perhaps I can remove it without problem. – Alain Matthes Apr 30 '12 at 22:26

No there is nothing specific in tkz-fct. You need to use TikZ to do that. With simple functions like in your example, you can use fp.

I think when you have complex functions , it's better to get the intersection with a real mathematics soft.

Actually tkz-fct works only with gnuplot and perhaps it's possible to use tables created by gnuplot to find an approximation of the intersection.

Now perhaps with lua, it's possible to find a fine method.

Update

But I find a solution ! I modified my package but I don't know if there are bad effects:

I used id for 'name path' and I modified \def\tkz@fct

\documentclass{article}
\usepackage{tkz-fct}
\usetikzlibrary{%
arrows,
calc,intersections
}

\makeatletter
\def\tkz@fct[#1]#2{%
\pgfkeys{/tkzfct/.cd,
domain      = \tkz@dmin:\tkz@dmax,
samples     = 200,
fp          = true,
id          = tkzfct}
\pgfqkeys{/tkzfct}{#1}%
\iftkz@init@NO%
\FPdiv{\tkz@x@delta}{\tkz@init@xorigine}{\tkz@init@xstep}%
\FPdiv{\tkz@y@delta}{\tkz@init@yorigine}{\tkz@init@ystep}%
\else
\FPset{\tkz@x@delta}{0}%
\FPset{\tkz@y@delta}{0}%
\fi%
% stockage
\FPdiv\tkz@ba{\tkz@min}{\tkz@init@xstep}%
\FPdiv\tkz@bb{\tkz@max}{\tkz@init@xstep}%
\def\x{(x*\tkz@init@xstep)}%
\expandafter\edef\csname tkzFctgnu\@alph\tkz@tkzf@fct \endcsname{#2}%
\expandafter\edef\csname tkzFctgnuLast\endcsname{#2}
\clip (\tkz@xa,\tkz@ya) rectangle (\tkz@xb,\tkz@yb);
\draw[name path =\tkz@fct@id,xshift  = -\tkz@x@delta cm,yshift  = -\tkz@y@delta cm,/tkzfct/.cd,#1 ]%
plot[id=\tkz@fct@id,domain= \tkz@ba:\tkz@bb,samples    = \tkz@fct@samples]%
function{(#2)/\tkz@init@ystep};%
%\end{scope}
\let\tkz@tmp@xstep\tkz@init@xstep
\def\tkz@init@xstep{1}
\iftkz@fp%
\ReplaceSubStrings{\tkz@tempa}{#2}{log}{ln}
\ReplaceSubStrings{\tkz@tempb}{\tkz@tempa}{**}{^}
\ReplaceSubStrings{\tkz@tempa}{\tkz@tempb}{\x}{x}
\ReplaceSubStrings{\tkz@tempb}{\tkz@tempa}{asin}{arcsin}
\ReplaceSubStrings{\tkz@tempa}{\tkz@tempb}{acos}{arccos}
\ReplaceSubStrings{\tkz@tempb}{\tkz@tempa}{atan}{arctan}
\expandafter\edef\csname tkzFct\@alph\tkz@tkzf@fct\endcsname{\tkz@tempb}%
\expandafter\edef\csname tkzFctLast\endcsname{\tkz@tempb}%
\fi
\let\tkz@init@xstep\tkz@tmp@xstep
\catcode\:=\tkzTWOPTCode\relax
}%

\makeatother

\begin{document}
\begin{tikzpicture}
\tkzInit[ymax=15,xmax=4,ystep=5]
\tkzAxeXY
\tkzFct[id=f,color=red,domain=0:4]{2*x**2+5}
\tkzFct[id=g,color=blue,domain=0:4]{-x**3+15}
\fill [red, opacity=0.5, name intersections={of=f and g}]
(intersection-1) circle (2pt) node {};
\end{tikzpicture}
\end{document}


todo

With some calculations you can get the coordinate system values like Jakes writes. I need to create a specific macro because sometimes the system is translate and the units can change with xstep and ystep.

Here a solution before a new macro

\begin{document}
\begin{tikzpicture}
\tkzInit[ymax=15,xmax=4,ystep=5]
\tkzAxeXY
\tkzFct[id=f,color=red,domain=0:4]{2*x**2+5}
\tkzFct[id=g,color=blue,domain=0:4]{-x**3+15}
\makeatletter
\draw [name intersections={of=f and g, by=fxg}] let \p1=(fxg) in
(fxg) circle (1pt) \pgfextra{\pgfmathsetmacro\xc{\x1/28.45}\pgfmathsetmacro\yc{\y1/28.45*\tkz@init@ystep}} node [right,align=left] {a=\xc\\ b=\yc};
\end{tikzpicture}


-
Actually Jake's solution is the more useful with name plot/.style={every path/.style={name path global=#1}} because you can avoid to add a big piece of code. Another possibility is to modify directly the package but it's preferable to wait a new version. – Alain Matthes Apr 30 '12 at 22:24
Looks great. It worked for me in some examples and in others not. But I didn't figure out yet where the problem is. – student May 1 '12 at 20:03
And with Jake's idea, do you have the same problems ? every path/.style={name path global=A}` – Alain Matthes May 1 '12 at 20:19
Perhaps the problem is that I have too old tikz and tkz versions. When I have more time I will update them and try it again. – student May 1 '12 at 20:26