% Usage: [u x t] = advection(c,f,T,n,nt) or u = advection(c,f,T,n,nt) % Advection equation on [0,1] with first order differencing % % Input: % c - advection velocity % f - Matlab inline function f(x) for the initial condition % T - final time moment % n - number of subintervals (panels) % nt - number of time intervals % % Output: % u - computed solution % x - nodes of the spacial grid % t - time steps % % Examples: % [u x t] = advection(-1,@bump,1,100,100); here 'bump' is a user-defined function in M-file % [u x t] = advection(-1,@bump,1,100,80); % [u x t] = advection(-1,@bump,1,100,99); function [u x t] = advection(c,f,T,n,nt); x = (0:n)/n; t = (0:nt)*T/nt; u = zeros(n+1,nt+1); h = 1/n; tau = T/nt; cfl = c*tau/h; cfl1=cfl+1; for i=2:n u(i,1) = f((i-1)*h); end; u(1,1) = 0; u(n+1,1) = 0; for k=1:nt-1 for i=2:n u(i,k+1) = cfl1 * u(i,k) - cfl * u(i+1,k); end; u(1,k+1) = 0; u(n+1,k+1) = 0; end;