function U=molexample(J,tf) % MOLEXAMPLE Simple demonstration of method of lines on nonlinear % heat equation % u_t = 0.1 u_xx - 2 u^2, (x,t) in (0,1) x (0,tf), % with boundary conditions % u_x(0,t)=0, u(1,t)=0 % and initial condition % u(x,0) = sin(20 x^2). % Uses ode23s, with default error tolerance, to solve semidiscretized % problem. % % U = molexample(J,tf) % where: % U = approximate solution at time tf % J = number of subintervals % tf = final time % % Example: % >> J=100; x=linspace(0,1,J+1); U=molexample(J,1.0); % >> plot(x,U), xlabel x, ylabel U % ELB 4/13/05 % See also: MOLMANUF dx=1/J; x=0:dx:1-dx; U0=sin(20*x.^2); [t,UU]=ode23s(@molderivs,[0 tf],U0); % pass function *handle* in this case U=UU(end,:); % UU has soln at lots of times; pick last time U=[U 0]; % add right boundary condition function dUdt=molderivs(t,U); % U,dUdt are a column vectors; t not used J=length(U); dx=1/J; nu = 0.1/(dx*dx); dUdt=zeros(J,1); dUdt(1)=-2*nu*U(1)+2*nu*U(2)-2*U(1)^2; for k=2:J-1 dUdt(k)=nu*U(k-1)-2*nu*U(k)+nu*U(k+1)-2*U(k)^2; end dUdt(J)=nu*U(J-1)-2*nu*U(J)-2*U(J)^2;