# Applying the method of lines to a partial differential equation and using Runge-kutta method

By method of lines I converted the PDE

u_t=u_{xx}, with the initial and boundary conditions

u(0,t)=u(0.1,t)
u(0,x)=sin(2*pi*x)


to a system of ODEs, as follows

function ut=pde1(t,u)
%
% Problem parameters
% global ncall
xl=0.0;
xr=1.0;
d=10;
a=10;
%
% PDE
n=length(u);
h=((xr-xl)/(n-1));
for i=1:n
if(i==1)     ut(i)=0.0;
elseif(i==n) ut(i)=0;
else         ut(i)=d*(u(i+1)-2.0*u(i)+u(i-1))/h^2;
end
end
ut=ut';
%


Now I am solving the ODEs ut by Runge-kutta method as follows:

%Initial condition
n=500;
for i=1:n
u0(i)=sin((2*pi)*(i-1)/(n-1));
end
t0=0.0;
tf=0.1;
tout=linspace(t0,tf,n);
y=zeros(n,length(tout));
y(:,1)=u0';
h=1/n;

for i = 1 : length(tout)
t=tout(i);
k1 = pde_1(t,y(:,i));
k2 = pde_1(t+h/2, y(:,i)+h*k1/2);
k3 = pde_1(t+h/2, y(:,i)+h*k2/2);
k4 = pde_1(t+h, y(:,i)+h*k3);
y(:,i+1) = y(:,i) + h*(k1 + 2*k2 + 2*k3 + k4)/6;

end

y
plot(tout,y(:,1))


but the outputs are NAN! any help?

• Welcome to the Mathematics StackExchange! What language are you programming in? Also, you might get a better answer from StackOverflow; while the Runge-kutta is a numerical algorithm, your focus appears to be on the implementation rather than the mathematical properties, its correctness, or proving theorems with the method. – Larry B. Jan 3 at 21:43
• Your code deviates in some relevant points from your equations. Is it really u(0,t)=u(0.1,t) or did you want periodic boundaries u(0,t)=u(1,t) or zero boundaries u(0,t)=u(1,t)=0? The last one you enforced with your implementation. Do you get a useful result if you decrease the factor d or the time step h? – LutzL Jan 4 at 0:55