function prob_2_35 % % Written by: % -- % John L. Weatherwax 2005-05-07 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- global c_h % Spatial grid, initial values: N = 1000; h = 2*pi/N; x = h*(1:N); v0 = exp(-100*(x - 1) .^ 2); c_h = - (0.2 + sin(x - 1) .^ 2)' / h; % Sparsity pattern for the Jacobian: S = sparse(N,N); for m = 2:N S(m,m-1) = 1; S(m,m) = 1; end % WWX: for periodic BC's: S(1,1) = 1; S(1,N) = 1; options = odeset('JPattern',S); % Integrate and plot: %t = 0:0.30:8; % WWX: Run for longer to see the trend. t = 0:0.30:24; [t,v] = ode15s(@f,t,v0,options); pltspace = ceil(N/128); x = x(1:pltspace:end); v = v(:,1:pltspace:end); surf(x,t,v), %view(10,70), axis([0 2*pi 0 t(end) 0 5]) ylabel t, zlabel u, grid off %================================================ function dvdt = f(t,v) global c_h dvdt = c_h .* [0; diff(v)]; % WWX: For periodic BC's: dvdt(1) = c_h(1)*(v(1)-v(end));