# How can I plot a coloured labeled graph of a heavily recursive function?

I am trying to write some personal notes based on the notes for a course I took a long time ago. In these notes the professor had the following graphs:

These graphs are beautiful and made with scalable vector graphics. Moreover, they are representing basis functions for splines, which are a very recursive structure unless you go and implement the dynamic programming algorithm, which seems overkill for plotting a static image.

Any suggestion as to which packages or tools I can use to make this?

I do not really know why the title of the question carries "heavily recursive" but you can draw all of them easily with the standard TeX packages such as MetaPost, PSTricks and TikZ. Here is a TikZy version of your graphs.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[font=\sffamily,transform shape,line cap=round]
\begin{scope}[scale=2]
\draw[help lines] (-1,-1) grid (7,2);
\draw[thick] (0,-1) -- (0,2) node[below left] {2};
\draw[thick] (-1,0) -- (7,0) foreach \X in {1,...,7}
{ (\X,0) node [below left] {\X}};
\foreach \Y [count=\X starting from 0] in {red,orange,green!60!black,cyan,brown!50!black,magenta}
{\draw[line width=1mm,\Y] (\X,1)
--node[black,above]{$\scriptstyle\mathsf{N}_{\mathsf{\X},\mathsf{1}}(u)$} (\X+1,1);}
\end{scope}
%
\begin{scope}[yshift=-5cm,scale=1.8]
\foreach \Y [count=\X starting from 0] in {brown!50!black,red,orange,green!60!black,cyan}
{\draw[very thick,\Y] (\X,0) -- ++ (1,1) -- ++ (1,-1); }
\draw[dashed,gray] foreach \X in {-1,5}
{(\X,0) -- ++ (1,1) -- ++ (1,-1)};
\draw[thick] (-0.1,0) -- (6.1,0) foreach \X in {0,...,6}
{ (\X,0) node [below] {\X}};
\end{scope}
%
\begin{scope}[yshift=-10cm,scale=1.8,declare
function={gauss(\x)=exp(-\x*\x/0.25);}]
\foreach \Y [count=\X starting from 0] in
{red,brown!50!black,green!60!black,orange,cyan,gray!50}
{\draw[very thick,\Y] plot[variable=\x,domain=-1.2:1.2,smooth]
({0.5+\x+0.5*\X+ifthenelse(\X>2,2.5,0)},{gauss(\x)}); }
\draw[thick] (-1,0) -- (7,0) foreach \X in {0,...,3}
{ ({-0.5+0.5*\X+ifthenelse(\X==3,0.5,0)},0) node [below]
{$\scriptstyle\mathsf{u}_{\mathsf{\X}}$}}
foreach \Y [count=\X starting from 0] in {m,m+k-2,m+k-1,m+k}
{ ({5+0.5*\X-ifthenelse(\X==0,0.5,0)},0) node [anchor=north west,rotate=-45]
{$\scriptstyle\mathsf{u}_{\mathsf{\Y}}$}};
\end{scope}
\end{tikzpicture}
\end{document}


• It;s heavily recursive because high order splines are defined recursively from lower ones, but they also need a customly defined set of knot values, so it's quite annoying to code. An unoptimazed C++ implementation is about 50 lines. – Makogan Dec 22 '19 at 18:41
• your example works for the easiest case which is splines of order 1, how would you go about plotting a recursive function? let;s say the fibonacci sequence (not to ask for splines because I know it'd be a pain) – Makogan Dec 22 '19 at 18:43
• @Makogan TikZ is able to do recursions, and the Fibonacci numbers are actually used to demonstrate this in section 59 Math Library of pgfmanual v3.1.5. Also, aren't Bezier curves splines, i.e. the cubic Bezier curves that are implemented in all of these programs, aren't they cubic splines? They get used in the plot[smooth] coordinates {...} syntax, which provides us with a smooth interpolation of some coordinates. – Schrödinger's cat Dec 22 '19 at 18:47