%
%
% -----------------------------------------------------
%
%   Approximation  im  Wintersemester 2013/14
%   H.J. Oberle
%
%   Plot von B-Splines B_{m,0}(t)
%
% ----------------------------------------------------
%
function bspline(m)
%
close all
%
format long e
%
%  Knoten aequidistant:
%
T = [0:20];
L = length(T);
for k=1:L
    Y(k) = basis(m,T(k),T);
end
%
%  Auswertung des B-Splines B_{m,0}(t)
%
t = [-1:0.01:5];
L = length(t);
for k=1:L
    s(k) = basis(m,t(k),T);
end
%
%   Plot 
%
plot(t,s,'b','Linewidth',1.2)
set(gca,'Linewidth',1.0,'FontSize',12)
axis([-1 5 -0.1 1.1])
hold
plot(T,Y,'k.','Markersize',20)
xlabel('B-Spline','FontSize',14)
%
%print -depsc bspline_4
%
%
% --------------------------------------------------------------------------

function [b] = basis(m,t,T)
%
B(1) = 0;
if and(T(1)<=t,t<T(2))
    B(1) = 1;
end
%
for k=2:(m+1)
    k1 = k+1;
    B(k) = 0;
    if and(T(k)<=t,t<T(k1))
        B(k) = 1;
    end
    for l=(k-1):-1:1
        l1 = l+1;
        B(l) = B(l)*(t-T(l))/(T(k)-T(l)) + B(l1)*(T(k1)-t)/(T(k1)-T(l1));
        
    end
end
%
b = B(1);

%
% --------------------------------------------------------------------------


