Graphics, Figures & TablesFinding point of intersection of two graphs within TikZ-PGF

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
chandra
Posts: 3
Joined: Sun Mar 27, 2011 4:06 pm

Finding point of intersection of two graphs within TikZ-PGF

Post by chandra »

Perhaps I am asking too much, but TikZ-PGF such a good package that I thought I should ask anyway.

I am using gnuplot to plot two Gaussian functions and need to find their point of intersection to shade the region enclosed by both curves.

Is there a way to do this in TikZ-PGF or should I resort to an external numerical solution and simply pass the co-ordinates of intersection to TikZ-PGF?

This is a proof of concept question and I am happy to look up the PGF manual if given a pointer or two.

Many thanks.
Last edited by chandra on Sun Mar 27, 2011 6:25 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Finding point of intersection of two graphs within TikZ-PGF

Post by localghost »

Have a look at the example below which only represents a very basic structure. It uses two layers to shift the filled area into the background so that the actual functions appear on top.

Code: Select all

\documentclass{minimal}
\usepackage{tikz}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,foreground}

\begin{document}
  \begin{tikzpicture}[y=5cm,domain=-5:5,samples=500]
    \begin{pgfonlayer}{foreground}
      \draw plot function {(1/(1*sqrt(2*pi)))*exp(-0.5*((x-0)/1)**2)};  % sigma=1, mu=0
      \draw plot function {(1/(1*sqrt(2*pi)))*exp(-0.5*((x-1)/1)**2)};  % sigma=1, mu=1
    \end{pgfonlayer}
    \begin{pgfonlayer}{background}
      \clip plot function {(1/(1*sqrt(2*pi)))*exp(-0.5*((x-0)/1)**2)} -- cycle;
      \fill[gray!50] plot function {(1/(1*sqrt(2*pi)))*exp(-0.5*((x-1)/1)**2)};
    \end{pgfonlayer}
  \end{tikzpicture}
\end{document}
The pgf/tikZ manual has details about the involved commands. A complete solution is left to the interested reader. I didn't look in detail, but perhaps the pgfplots package can do the same.


Best regards and welcome to the board
Thorsten
chandra
Posts: 3
Joined: Sun Mar 27, 2011 4:06 pm

Finding point of intersection of two graphs within TikZ-PGF

Post by chandra »

Thank you for your welcome and your solution, which works :-)

Meanwhile, I have stumbled across the

Code: Select all

name path=
option under the intersections library.

The operational lines from my file are:

Code: Select all

%
% Draw the two Gaussians using gnuplot
% Note that pi is known as is exponentiation via **
%
% sigma = 0.3; mu = 4.6 for blue; constant multiplier = 1.4
\draw[color=blue, name path=blueG] plot[id=blueref] function {1.4/(0.3*sqrt(2*pi))*exp(-((x-4.6)**2)/(2*0.3**2))};
% sigma = 0.4; mu = 5.8 for yellow; constant multiplier = 1.5
\draw[color=yellow!40!orange!60, name path=yellowG] plot[id=yellowref] function {1.5/(0.4*sqrt(2*pi))*exp(-((x-5.8)**2)/(2*0.4**2))};
%
% Find point of Intersection and its abscissa
%
\fill [name intersections={of=blueG and yellowG, by={a}}] (a) circle (2pt);
and this works too!

I never cease to be amazed by TikZ-PGF.

Thank you once more, localghost.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Finding point of intersection of two graphs within TikZ-PGF

Post by localghost »

Then please mark the topic (not the last post) accordingly as written in Section 3 of the Board Rules (to be read before posting) and as you have already been asked for.
Post Reply