function [U,x]=HeatImplicit_N(a,b,nx,nt,alpha,tmax)
%HeatBTCS:Solve 1-D equation with BTCS Scheme
% %input a,b end points of the spece domain default a=0,b=1 % nx number of spatial steps default nx=20 % nt number of time steps default nt=10 % alpha diffusion coefficient default alpha=0.01 % tmax maximum time for the simulation default tmax=0.5 % %output T matix of solution: T(:)=T(x) at t=t(m) if nargin<1 a=0;b=1; end if nargin<3,nx=20;end if nargin<4,nt=20;end if nargin<5,alpha=0.01;end if nargin<6,tmax=3;end % %compute the step sizes clc close all dx=(b-a)/(nx-1); dt=tmax/(nt-1); x=[0:nx-1]*dx; U=zeros(nx,1); %memory allocation for the solution d=zeros(nx,1); U0=f(x); %setup initial conditions Uold=U0'; % %setup the coefficient matix r=(alpha*dt)/(2*dx^2); a=-r; b=1+2*r; c=-r; a=a*ones(nx,1); b=b*ones(nx,1); A=spdiags([a b a],-1:1,nx,nx); A(1,1)=1;A(1,2)=0;A(nx,nx)=1;A(nx,nx-1)=-1; d=r; e=1-2*r; f=r d=d*ones(nx,1); e=e*ones(nx,1); B=spdiags([d e d],-1:1,nx,nx); B(1,1)=0;B(1,2)=0;B(nx,nx)=0;B(nx,nx-1)=0; C=zeros(nx,1); %setup the load vector for tn=1:nt C(nx)=dx*sin(tn*dt); g=B*Uold + C; Unew=cgs(A,g); plot(x,U0,'b:',x,Unew,'r.-') legend('initial value', 'solution') title(sprintf('time t=%0.2f',(tn-1)*dt)) Uold=Unew; U=Unew; drawnow
end function y=f(x) %initial condition function y=x.*(1-x.^2).^2;