Graphics, Figures & Tables ⇒ Finding point of intersection of two graphs within TikZ-PGF
Finding point of intersection of two graphs within TikZ-PGF
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.
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
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}
Best regards and welcome to the board
Thorsten
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Finding point of intersection of two graphs within TikZ-PGF

Meanwhile, I have stumbled across the
Code: Select all
name path=
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);
I never cease to be amazed by TikZ-PGF.
Thank you once more, localghost.
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Finding point of intersection of two graphs within TikZ-PGF
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10