function [x,u,ue]=myHeat2(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 x=linspace(0,1,N+1); u=log(x+1); % taken nstep time steps t=0; j=2:N; dx=1/N; r=dt/dx^2; for n=1:nstep u(j)=u(j)+r*(u(j+1)-2*u(j)+u(j-1))... +dt*exp(-t)*((x(j)+1).^(-2)-log(x(j)+1)); t=t+dt; u(1)=0; u(N+1)=log(2)*exp(-t); end % Here is the exact solution ue=log(x+1).*exp(-t);