function [x,u,ue]=myHeat3(N,dt,nstep) % Solve the heat equation u_t = u_xx + Q(x,t) % using centered differences in space and % forward differences in time. Assign Q(x,t) % and ICs and BCs as in the class notes. % setup mesh and initial conditions dx=1/N; x=linspace(-dx,1+dx,N+3); u=log(x+1); % taken nstep time steps t=0; j=2:N+2; r=dt/dx^2; for n=1:nstep Q=exp(-t)*((x(j)+1).^(-2)-log(x(j)+1)); u(j)=u(j)+r*(u(j+1)-2*u(j)+u(j-1))+dt*Q; t=t+dt; u(1)=u(3)-2*dx*exp(-t); u(N+3)=u(N+1)+2*dx*exp(-t)/2; end % Here is the exact solution ue=log(x+1).*exp(-t);