Error in a code of Won Young Yang's book

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

2 Comments

What error are you observing?
And how can we reproduce it? In other words, what values of a,xf,T,it0,bx0,bxf,M,N did you pass into it, so that we can run it? Please give code to make those variables with the values you want.

Sign in to comment.

 Accepted 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.

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!