# B spline curve generation in Python

I am using the de boors algorithm to generate B splines in python. However I am getting spikes in the final figure. I do not understand why this happens. I am posting my code here for reference

def knot_values(n,k):
t = np.zeros((1,n+k+1))
for i in range(0,n+k+1):
if i<k:
t[i] = 0
if i>=k and i<=n:
t[i] = i-k+1
if i>n:
t[i] = n-k+2

return t

def basis_spline(u,t,i,k):
if k==1:
if u>=t[i] and u<=t[i+1]:
sol = 1
else:
sol = 0
else:
a = (u - t[i])*basis_spline(u,t,i,k-1)
b = t[i+k-1] - t[i]
c = (t[i+k] - u)*basis_spline(u,t,i+1,k-1)
d = t[i+k] - t[i+1]
if b == 0:
temp1 = 0
else:
temp1 = a/b

if d == 0:
temp2 = 0
else:
temp2 = c/d

sol = (temp1) + (temp2)

return sol

def curve_generator(n,k,ctrl_x,ctrl_y):

t = knot_values(n,k)
print(t)
u = np.arange(t[k-1],t[n+1],0.001);
length = u.shape;

x = np.zeros((1,length));
y = np.zeros((1,length));

for i in range(0,n+1):
for j in range(0,length):
x[j] = x[j] + basis_spline(u[j],t,i,k)*ctrl_x[i];
y[j] = y[j] + basis_spline(u[j],t,i,k)*ctrl_y[i];

return x,y

n = 6
k = 3
ctrl_x = np.array([0,3,6,9,10,12,15])
ctrl_y = np.array([0,4,2,3,7,8,5])

[x,y] = curve_generator(n,k,ctrl_x,ctrl_y)

n = x.shape
a = *n
b = *n
for i in range(0,n):
a[i] = x[i]
b[i] = y[i]

plt.plot(a,b)
plt.show() • Don't post the same question again to add new details. Instead, use the edit button when necessary. – Dan Hulme May 13 '18 at 10:02

if u>=t[i] and u<t[i+1]: