% build a vector of ones n = 20; e1=ones(n,1); % construct spatial discretization matrix A A=spdiags([e1 -2*e1 e1],[-1 0 1],n,n); % periodic boundaries: A(1,n)=1; A(n,1)=1; % define the domain and time span dx = (1-(-1))/(n); x = (-1:dx:1-dx); tspan = [0 10]; % initial conditions (gaussian) u0 = exp(-x.^2); % solve ODE in time k = 1.2; [t,y]=ode45('rhs',tspan,u0,[],k,dx,A); % plot initial condition, solution after one time % step and the solution at end time figure(1) set(gca, 'FontSize', 20); plot(x,u0,'r*-',x,y(1,:),'g*-',x,y(end,:),'b*-'); legend('Initial condition', 'Solution at t = dt\_1', 'Solution at t = 10') xlabel('x'); ylabel('u'); % plot solutions as a function of time at x = 0 figure(2) set(gca, 'FontSize', 20); I = find(x == 0); plot(t,y(:,I),'b*-'); xlabel('t'); ylabel('u');