MATLAB: Error in a code of Won Young Yang’s book

error

Hi everybody I've got an error during running one Matlab code
would you please tell me what should I do to run it? Code is:
function [u,x,t] = heat_exp(a,xf,T,it0,bx0,bxf,M,N)
%solve a u_xx = u_t for 0 <= x <= xf, 0 <= t <= T
% Initial Condition: u(x,0) = it0(x)
% Boundary Condition: u(0,t) = bx0(t), u(xf,t) = bxf(t)
% M = # of subintervals along x axis
% N = # of subintervals along t axis
dx = xf/M; x = [0:M]*dx;
dt = T/N; t = [0:N]*dt;
for i = 1:M + 1, u(i,1) = it0(x(i)); end
for n = 1:N + 1, u([1 M + 1],n) = [bx0(t(n)); bxf(t(n))]; end
r = a*dt/dx/dx, r1 = 1 - 2*r;
for k = 1:N
for i = 2:M
u(i,k+1) = r*(u(i + 1,k) + u(i-1,k)) + r1*u(i,k);
end
end

Best Answer

You have
x = [0:M]*dx
so your x starts at 0.
You then have
it0(x)=sin (pi*x)
so you are trying to assign into it0(0) but 0 is not a positive integer. Just use
it0 = sin(pi*x);
However, your next line is
u(x,0) = it0(x)
with subscript 0, and 0 is not a positive integer.
Related Question