51 lines
1.1 KiB
Mathematica
51 lines
1.1 KiB
Mathematica
|
function [x,t,c] = UW_scheme(xf,N,T,K,U,c0)
|
||
|
% ---- Numerical solution of the linear advection equation in a periodic domain ----
|
||
|
% c_t+ U * c_x = 0 with domain [0,xf]
|
||
|
% given the initial condition c0.
|
||
|
% -----------------------------------------------
|
||
|
% Sintax:
|
||
|
% [x,t,c] = UW_scheme(xf,N,T,K,U,c0)
|
||
|
%
|
||
|
% Input:
|
||
|
% xf end of our domain
|
||
|
% N number of space intervals
|
||
|
% T max time
|
||
|
% K number of time intervals
|
||
|
% U convection velocity
|
||
|
% c0 initial condition
|
||
|
%
|
||
|
% Output:
|
||
|
% x vector of spatial nodes
|
||
|
% t vector of time nodes
|
||
|
% c numerical solution
|
||
|
|
||
|
% Space and time intervals size
|
||
|
dx=xf/N;
|
||
|
dt=T/K;
|
||
|
% initialization of x and t vectors (nodes)
|
||
|
x=linspace(0,xf,N+1)';
|
||
|
t=linspace(0,T,K+1)';
|
||
|
|
||
|
% Solution matrix
|
||
|
c=zeros(N+1,K+1);
|
||
|
|
||
|
% Initial conditions
|
||
|
c(:,1) = c0(x);
|
||
|
|
||
|
|
||
|
% Creating our matrix
|
||
|
e = ones(N+1,1);
|
||
|
B = spdiags([-e,e],[-1,0],N+1,N+1);
|
||
|
I = speye(N+1);
|
||
|
|
||
|
%"printing" courant number and numerical viscosity
|
||
|
C0=(U*dt/dx)
|
||
|
numerical_viscosity= U*dx*(1-C0)/2
|
||
|
|
||
|
%final metrix to compute the solution
|
||
|
A = I - C0*B;
|
||
|
|
||
|
%finding the solution
|
||
|
for k=1:K
|
||
|
c(1:end,k+1) = A*c(1:end,k);
|
||
|
end
|