# How one can draw cubic b-spline

I want to draw quadratic and cubic b-spline. The formula is

Unfortunately, the similar answer in the below link does not work: Plotting a recursive defined set of functions with conditional base function

TikZ comes with the tikzmath library, which allows one to define functions recursively. All one has to do is to feed this in. In my answer, which uses elements of Christian Feuersänger's nice answer (that works really great, BTW), I make use of two ways of declaring function: the one that Christian also used (though in a slightly different syntax) for the definiton of N_{i1}=\chi and another one using tikzmath. The second one allows one to define functions recursively.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{math} % tikzmath, see p. 640 of the pgfmanual.
\begin{document}
% based on https://tex.stackexchange.com/a/307032/121799
\def\tvalues{{0,1,3,4,6,9,10}}
\begin{tikzpicture}[evaluate={
function myN(\i, \k, \t) {
if \k == 1 then {
return chi(\t,\i);
} else {
return myN(\i, \k-1, \t)*(\t-\tvalues[\i])/(\tvalues[\i+\k-1]-\tvalues[\i])
+myN(\i+1, \k-1, \t)*(\tvalues[\i+\k]-\t)/(\tvalues[\i+\k]-\tvalues[\i+1]);
};
};
},
declare function={chi(\t,\i)=and(\t >= \tvalues[\i] ,\t <=\tvalues[\i+1]);}]
\begin{axis}[samples=101,
use fpu=false,
xlabel=$x$,ylabel=$y$,
ymax=2, ymin=0, xmin=0, xmax=10,
domain=0:10,
]
\end{axis}
\end{tikzpicture}
\end{document}


This also allows one to produce a plot that resembles your screen shot.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{math} % tikzmath, see p. 640 of the pgfmanual.
\begin{document}
% based on https://tex.stackexchange.com/a/307032/121799
\def\tvalues{{-1,0,1,3,4,6,9,10,11,12,13}}
\tikzset{evaluate={
function myN(\i, \k, \t) {
if \k == 1 then {
return chi(\t,\i);
} else {
return myN(\i, \k-1, \t)*(\t-\tvalues[\i])/(\tvalues[\i+\k-1]-\tvalues[\i])
+myN(\i+1, \k-1, \t)*(\tvalues[\i+\k]-\t)/(\tvalues[\i+\k]-\tvalues[\i+1]);
};
};
},
declare function={chi(\t,\i)=and(\t >= \tvalues[\i] ,\t <=\tvalues[\i+1]);}}

\begin{tikzpicture}
\begin{axis}[samples=101,
use fpu=false,mark=none,
xlabel=$x$,ylabel=$y$,
ymax=pi/2, ymin=0, xmin=0, xmax=10,
domain=0:10,
]
\addplot [mark=none] {myN(2,3,x)} node[pos=0.35,above] {$N_{23}$};
\addplot+ [mark=none] {myN(3,3,x)} node[pos=0.51,above] {$N_{33}$};
\addplot+ [mark=none] {myN(4,3,x)} node[pos=0.75,above] {$N_{43}$};
\end{axis}
\end{tikzpicture}
\end{document}


BTW, I think that your question is rather nice in principle. What prevented me from upvoting is the lack of an own attempt and an irritating statement on Chritian Feuersänger's nice answer. If you correct this, I'll consider upvoting, though.

• Many thanks, it works. Unfortunately this code run very slow. Do you know what the reason is? – Baruch Sep 18 '18 at 11:12
• @user108438 I do not precisely know, but these are 101 samples and a recursively defined function. Of course, you could define the cubic spline explicitly, which would make it a bit faster, but not by too much. – user121799 Sep 18 '18 at 11:37
• @user108438 I can't refrain from making a further comment. So far you have asked 10 questions here, to most of which you got answers that are IMHO very good. You did not bother to accept any of them. Here you just post a screen shot and I present a working code that does some recursive definition of functions, something that cannot be found in the pgfplots manual. All you do is to complain that the compilation time is too long. Really? How about you try to solve a problem or come up with an answer? The least thing you could do is to accept the answers to your previous questions. – user121799 Sep 18 '18 at 14:58