You are on page 1of 2

# N_KAID 18/02/2016

# Step 1: Explicit 1D convection equation


# Python 2.7.11 |Anaconda 2.4.1 (64-bit)
import numpy as np
# librairie numerique
import matplotlib.pyplot as pl # librairie graphique
nx = 5
nt = 5
dt = 0.1
c = 1.00
dx = 0.1
x=np.linspace(0.,0.5 ,nx+1) # laxe des x
up1=np.zeros(nx+1)
# declarer un vecteur U(n+1) de 6 noeuds
# initilaliser a zero
up1[0]=1. ; up1[5]=0. # Condition aux limites U(0)=1.0 et U(5)=0.0
pl.plot(x,up1,'-b') # Afficher la fonction init de U(x,t)
for it in range(1,nt): # boucle dans le temps
un = up1.copy()
for i in range(1,nx-1):
# boucle dans l'espace
up1[i] = un[i] - c*dt/dx*(un[i] - un[i-1])
# end for
pl.plot(x,up1,'-r') # Afficher la fonction de U(x,t) a chaque iteration
# end for
Condition initial (1ere Itration)
un
1.0
0.0
0.0
0.0
0.0
0.0

up1
1.0
0.0
0.0
0.0
0.0
0.0

x
0.0
0.1
0.2
0.3
0.4
0.5

2me Itration
un
1.0
0.0
0.0
0.0
0.0
0.0

up1
1.0
1.0
0.0
0.0
0.0
0.0

x
0.0
0.1
0.2
0.3
0.4
0.5

un
1.0
1.0
0.0
0.0
0.0
0.0

up1
1.0
1.0
1.0
0.0
0.0
0.0

x
0.0
0.1
0.2
0.3
0.4
0.5

3me Itration

4me Itration
un
1.0
1.0
1.0
0.0
0.0
0.0

up1
1.0
1.0
1.0
1.0
0.0
0.0

x
0.0
0.1
0.2
0.3
0.4
0.5

MATLAB CODE
nx = 5;
nt = 5;
dt = 0.1;
c = 1.00;
dx = 0.1;
x=linspace(0.0,0.5 ,nx+1) ;
up1=zeros(nx+1) ;
% declarer un vecteur U(n+1) de 6 noeuds
% initilaliser a zero
up1(1)=1.0 ;
up1(6)=0.0 ;
% Condition aux limites U(0)=1.0 et U(5)=0.0
hold on
plot(x,up1,'-b') % Tracer la fonction init de U(x,t)
%% boucle dans le temps
for it =1:nt
un = up1 ;
% boucle dans l'espace
for i = 2:nx
up1(i) = un(i) - c*dt/dx*(un(i) - un(i-1))
end
% Tracer la fonction de U(x,t) a chaque iteration
hold on
plot(x,up1,'-r')
end

You might also like