function prob_2_34 % % 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 options = odeset('JPattern',S); % Integrate and plot: t = 0:0.30:8; disp( 'Solving with ode15s...' ); tic; [t,v] = ode15s(@f,t,v0,options); toc disp( 'Solving with ode23...' ); tic; [t,v] = ode23(@f,t,v0,options); toc 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 8 0 5]) ylabel t, zlabel u, grid off %================================================ function dvdt = f(t,v) global c_h dvdt = c_h .* [0; diff(v)];