Académique Documents
Professionnel Documents
Culture Documents
2022/2023
Module: outils de programmation Matlab
Master Geomatique envirenmentale
Traveaux pratiques:Matlab
• Prof: A.BOUDHAR
• Stud : S.OUSSAOUI
Basics of matlab
I. initiation matlab:
%clc =effacer la commande windows
%clear = supprimer les variables
%close all = fermer tout les figures
clc, clear, close all
1.scalire:
% Addition
6+34
ans = 40
% soustraction
6-34
ans = -28
% division
6/34
ans = 0.1765
% multiplication
6*34
ans = 204
% déclaration de variables
x=-2.34*178
x = -416.5200
% affaicher la valeur de X
x
x = -416.5200
% 5 à la puissance de 2
y=power(5,2)
1
y = 25
yy=(5.^2)
yy = 25
ans x y yy
2. vecteur:
% afficher de 1 jusqu'à 50
X=1:50
X = 1×50
1 2 3 4 5 6 7 8 9 10 11 12 13
Y = 1×50
1 3 5 7 9 11 13 15 17 19 21 23 25
ans = 23
Z = 1×14
7 9 11 13 15 17 19 21 23 25 27 29 31
% langueur de la série
length(Z)
ans = 14
ans = 1×2
1 14
2
% la somme de chaque colonne
X+Y
ans = 1×50
2 5 8 11 14 17 20 23 26 29 32 35 38
T = 1×3
3.0000 2.5000 -6.0000
U = 1×3
3.0000 2.5000 -6.0000
Z = 1×6
-1.0000 -1.2000 -1.4000 -1.6000 -1.8000 -2.0000
% I---> Langeur de Z
I= length (Z)
I = 6
w = 4×2
0 0
0 0
0 0
0 22
Y = 1×3
1 1 1
X = 1×3
2.0000 1.4142 -6.1000
% Concatination horizontale
U=[Y,X]
U = 1×6
1.0000 1.0000 1.0000 2.0000 1.4142 -6.1000
3
ans = 1×6
-1.0000 -1.2000 -1.4000 -3.2000 -2.5456 12.2000
V = 6×1
1.0000
1.0000
1.0000
2.0000
1.4142
-6.1000
% un vercteur vertical
W =[2;3;6]
W = 3×1
2
3
6
% la somme de Li*Ci
Z*V
ans = 2.8544
ans = 6×6
-1.0000 -1.2000 -1.4000 -1.6000 -1.8000 -2.0000
-1.0000 -1.2000 -1.4000 -1.6000 -1.8000 -2.0000
-1.0000 -1.2000 -1.4000 -1.6000 -1.8000 -2.0000
-2.0000 -2.4000 -2.8000 -3.2000 -3.6000 -4.0000
-1.4142 -1.6971 -1.9799 -2.2627 -2.5456 -2.8284
6.1000 7.3200 8.5400 9.7600 10.9800 12.2000
Y = 1×3
0.9093 0.9878 0.1822
% vérefication (vraie,faux)
Z=(Y>0.5)
I = 2
ans = 2.0792
4
% nombrer les valeur qui sont vrai
YY=find(Y>0)
YY = 1×3
1 2 3
YYY = 1×3
0.9093 0.9878 0.1822
Y(Y>0)
ans = 1×3
0.9093 0.9878 0.1822
C1 = 1×3
3 4 5
C2=2+(1:5)
C2 = 1×5
3 4 5 6 7
% question 1
% vecteur de 10 colonnes et 1 ligne avec 5 sur tout les variables
aa=5* ones(10,1)
aa = 10×1
5
5
5
5
5
5
5
5
5
5
% QUESTION 2
% la sommes des 50 premier nombre impaire
bb=sum(1:2:100)
bb = 2500
% question 3
%vecteur des racines carrées des entiers impaire de 1--> 9
cc=sqrt(1:2:9)
cc = 1×5
1.0000 1.7321 2.2361 2.6458 3.0000
% question 4
% les carrées des entiers de 1-->10
5
dd= (1:10).^2
dd = 1×10
1 4 9 16 25 36 49 64 81 100
ddd= power(1:10,2)
ddd = 1×10
1 4 9 16 25 36 49 64 81 100
%question 5
% inverse des entiers de 1--> 10
1./(1:10)
ans = 1×10
1.0000 0.5000 0.3333 0.2500 0.2000 0.1667 0.1429 0.1250
x= 10 % déclarer un variable x
x = 10
v = 10×1
0.4218
0.9157
0.7922
0.9595
0.6557
0.0357
0.8491
0.9340
0.6787
0.7577
z = 10×1
4.2176
9.1574
7.9221
9.5949
6.5574
0.3571
8.4913
9.3399
6.7874
7.5774
z1 = 10×2
0.4218 4.2176
0.9157 9.1574
0.7922 7.9221
0.9595 9.5949
0.6557 6.5574
0.0357 0.3571
0.8491 8.4913
0.9340 9.3399
0.6787 6.7874
6
0.7577 7.5774
z2 = 20×1
0.4218
0.9157
0.7922
0.9595
0.6557
0.0357
0.8491
0.9340
0.6787
0.7577
3. Matrices:
A = 3×8
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
% une matrice contient seulement les 1 sur le diagnoles avec 4 lignes et 4 colonnes
B=eye(4,4)
B = 4×4
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
C = 8×3
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
%les dimension de C
size(C)
ans = 1×2
8 3
7
ans = 0
ans = 3×3
0 0 0
0 0 0
0 0 0
ans = 16×1
1
0
0
0
0
1
0
0
0
0
A = 2×8
1 0 0 0 0 1 0 0
0 0 1 0 0 0 0 1
ans = 8×2
1 0
0 0
0 1
0 0
0 0
1 0
0 0
0 1
B = 1×8
1 0 1 0 0 1 0 1
ans = 4
8
C=sum(A(2,:))
C = 2
D = 2×3
3.0000 4.0000 -2.0000
1.0000 -1.5000 6.0000
ans = 2×1
-2
6
ans = 1×3
0 0 0
% 2emme ligne moins le premier ligne suivi par 3emme ligne moins le 2eme li
diff(D)
ans = 1×3
-2.0000 -5.5000 8.0000
ans = 1×8
1 0 1 0 0 1 0 1
ans = 0
ans = 1×3
1.4142 3.8891 5.6569
ans = 1×3
3 -6 -12
ans = 2×3
3 4 -2
3 -6 -12
9
% ligne 2--> ligne 1 + ligne 2 et ligne 3--> ligne 2f + ligne 3
cumsum(D)
ans = 2×3
3.0000 4.0000 -2.0000
4.0000 2.5000 4.0000
ans = 2×2
1 0
0 0
%boite à moutache
boxplot(B)
10
4. Chaînes de caractères et messages:
a =
"hello world ! "
hello world !
x =
'OUSSAOUI'
11
y=['votre nom est ',x]
y =
'votre nom est OUSSAOUI'
disp(y)
s =
'bonjour'
% Vérifier size(s).
size(s)
ans = 1×2
1 7
s =
'bonjourà tous'
x = 5
for i =1:10
x=4*x*(1-x)
end
x = -80
x = -25920
x = -2.6875e+09
x = -2.8890e+19
x = -3.3386e+39
x = -4.4586e+79
x = -7.9515e+159
x = -Inf
x = -Inf
x = -Inf
12
nb = 21
a = 1×10
0 0 0 0 0 0 0 0 0 0
if a==0
a(5)=34 %si a=1 changer la valeur de 5eme valeur en 34
end
a = 1×10
0 0 0 0 34 0 0 0 0 0
nbre = 432
if (nbre<0)
disp("votre numéro est négatif")
elseif (nbre==0)
disp("votre numéro est nul")
else
disp("votre numero est positive ")
end
z=peaks(25)
z = 25×25
0.0001 0.0002 0.0007 0.0017 0.0034 0.0051 0.0042 -0.0050
0.0002 0.0005 0.0016 0.0038 0.0074 0.0101 0.0045 -0.0244
0.0003 0.0011 0.0032 0.0077 0.0147 0.0188 0.0028 -0.0671
0.0005 0.0019 0.0058 0.0141 0.0272 0.0357 0.0074 -0.1224
0.0007 0.0028 0.0088 0.0227 0.0468 0.0698 0.0452 -0.1285
13
0.0006 0.0029 0.0103 0.0300 0.0703 0.1271 0.1532 0.0232
-0.0005 0.0002 0.0053 0.0252 0.0789 0.1851 0.3265 0.3923
-0.0033 -0.0081 -0.0143 -0.0119 0.0299 0.1667 0.4485 0.8358
-0.0088 -0.0244 -0.0565 -0.1032 -0.1301 -0.0427 0.3007 0.9838
-0.0166 -0.0489 -0.1223 -0.2545 -0.4232 -0.5096 -0.2806 0.4947
figure
mesh(z)
surf(z)
14
15
Université Sultan Moulay Slimane | FST Béni Mellal
Master Géomatique Envirennementale
Prof: Abdelghani Boudhar
1.Data Importation
clc,clear,close all
path='C:\Users\S.OUSSAOUI\Desktop\MASTER\Outil de programmation\'
path =
'C:\Users\S.OUSSAOUI\Desktop\MASTER\Outil de programmation\'
xlfile =
'Temperature.xlsx'
namefxl =
'C:\Users\S.OUSSAOUI\Desktop\MASTER\Outil de programmation\Temperature.xlsx'
DTable=readtable(namefxl)
1
Date IMESKER ARMED TACHDERT NELTNER OUKASM jour mois
1 01-Oct-2007
2 02-Oct-2007
3 03-Oct-2007
4 04-Oct-2007
5 05-Oct-2007
6 06-Oct-2007
7 07-Oct-2007
8 08-Oct-2007
9 09-Oct-2007
10 10-Oct-2007
11 11-Oct-2007
12 12-Oct-2007
13 13-Oct-2007
14 14-Oct-2007
DTable.Date
2
24-Nov-2007
25-Nov-2007
26-Nov-2007
27-Nov-2007
29-Nov-2007
23-Dec-2007
24-Dec-2007
27-Dec-2007
29-Dec-2007
04-Jan-2008
% calcule la moyenne
mean(DTable.IMESKER)
ans = 16.5269
ans = 26.6206
ans = 5.1323
TS1 = 244×1
23.4965
21.3763
15.2702
18.7171
20.5888
26.1367
24.3471
25.5508
23.9505
24.7203
TS2=DTable.ARMED
TS2 = 244×1
22.5600
18.6300
13.2100
15.6400
17.6600
23.3200
21.3800
22.1900
19.2500
19.6100
3
TS3=DTable.TACHDERT
TS3 = 244×1
19.0390
15.2107
10.0352
12.9345
17.4945
19.5091
18.7000
19.1023
16.3748
17.2416
TS4=DTable.NELTNER
TS4 = 244×1
9.4102
6.3155
5.6664
12.9612
12.5817
9.6827
10.6445
11.1224
8.0913
7.6064
TS5=DTable.OUKASM
TS5 = 244×1
11.8400
6.1670
6.3160
12.3600
13.1900
13.1900
14.1600
13.3200
7.9600
10.2500
%colonnes de DATE
Vtimes=DTable.Date
4
%la moyen de la station TS1
Moy=mean(TS1)
Moy = 16.5269
Ecarttype1 = 4.4785
Mediane1 = 16.2252
Range1 = 21.4883
Mode1 = 5.1323
Var1 = 20.0569
T = 1×6 table
Moy Ecarttype1 Mediane1 Range1 Mode1 Var1
2.Represenations graphiques
a. plot 2D:
% station 1 en fonction des dates
f1=figure
f1 =
Figure (1) with properties:
Number: 1
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [488 342 560 420]
Units: 'pixels'
5
p1=plot(Vtimes,TS1,"LineStyle","-.","LineWidth",2,'Color','g')% linweidth=epaisseur de courbe
p1 =
Line with properties:
Color: [0 1 0]
LineStyle: '-.'
LineWidth: 2
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1×244 datetime]
YData: [1×244 double]
ZData: [1×0 double]
f2=figure
f2 =
Figure (2) with properties:
Number: 2
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [488 342 560 420]
Units: 'pixels'
6
Show all properties
f3=figure
f3 =
Figure (3) with properties:
Number: 3
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [488 342 560 420]
Units: 'pixels'
7
histogram(TS1,"FaceColor",'c')
xlabel('Date',"BackgroundColor",'y',"FontSize",15)% titre des axes des x
ylabel('temperature',"BackgroundColor",'y',"FontSize",15)% titre des axes des y
title('la temperature de station imsker ',"FontSize",14)
8
histfit(TS1)
xlabel('Date',"BackgroundColor",'y',"FontSize",15)% titre des axes des x
ylabel('temperature',"BackgroundColor",'y',"FontSize",15)% titre des axes des y
title('la temperature de station imsker ',"FontSize",14)
9
boxplot(TS1,'Colors',"r",'BoxStyle',"outline","MedianStyle","target",'Orientation',"horizontal"
10
a=[3:2:13;1:2:11]
a = 2×6
3 5 7 9 11 13
1 3 5 7 9 11
% réaliser un graphe en 2D
plot(a(:),"LineWidth",4, 'color',[0.9290 0.6940 0.1250])
title('diagramme')
% la boite à moustache
boxplot(a(:))
title('diagramme')
11
%déclarer une figure
figure(1)
subplot(5,1,1) % met la premiere figure dans la premier ligne d'une matrice à 5 ligne et un seu
plot(Vtimes,TS1,'r')%plot des date en x et TS1 en y avec une couleur rouge
xlabel("la date") %déclarer le nom de la'axe des X
ylabel("°C")%déclarer le nom des Y
title("la temperature de station IMSKER")% titre de plot
legend('Tair IMESKER')%déclarer une légende
subplot(5,1,2)
plot(Vtimes,TS2,'g')
xlabel("la date")
ylabel("°C")
title("la temperature de station ARMED")
legend('Tair ARMED')
subplot(5,1,3)
plot(Vtimes,TS3,'c')
xlabel("la date")
ylabel(" °C")
title("la temperature de station TACHDERT")
legend('Tair TACHDDIRT')
subplot(5,1,4)
plot(Vtimes,TS4,'b')
xlabel("la date")
12
ylabel("°C")
title("la temperature de station NELTENER")
legend('Tair NELTENER')
subplot(5,1,5)
plot(Vtimes,TS5,'y')
xlabel("la date")
ylabel(" °C")
title("la temperature de station OUKASEM")
legend(['Tair OUKASEM'])
area(Vtimes,TS5,"FaceColor","g")
xlabel("la date")
ylabel(" °C")
title("la temperature de station OUKASEM")
legend(['Tair OUKASEM'])
b.histogramme:
subplot(5,1,1)
histogram(TS1,"FaceColor",'c')%réaliser un histogramme de la premiere station
xlabel("la date")
ylabel("°C")
title("la temperature de station IMSKER")
subplot(5,1,2)
histogram(TS2,"FaceColor",'g')%réaliser un histogramme de la 2eme station
xlabel("la date")
ylabel("°C")
title("la temperature de station ARMED")
subplot(5,1,3)
13
histogram(TS3,"FaceColor",'r')%réaliser un histogramme de la 3eme station
xlabel("la date")
ylabel(" °C")
title("la temperature de station TACHDERT")
subplot(5,1,4)
histogram(TS4,"FaceColor",'b')%réaliser un histogramme de la 4eme station
xlabel("la date")
ylabel("°C")
title("la temperature de station NELTENER")
subplot(5,1,5)
histogram(TS5,"FaceColor",'k')%réaliser un histogramme de la 5eme station
xlabel("la date")
ylabel(" °C")
title("la temperature de station OUKASEM")
c. boite à moustach:
figure(3)
DTair=DTable(:,2:6) %afficher le deuxieme couleur à 6 eme colonne
14
IMESKER ARMED TACHDERT NELTNER OUKASM
xlabel("Stations")
ylabel("Frequency")
title("la boite à moustach de chaque station")
15
%afficher la moyen de chaque station
mean(varT)
ans = 1×5
16.5269 13.5801 11.0286 5.0306 5.4034
16
Université Sultan Moulay Slimane | FST Béni Mellal
Master Géomatique Envirennementale
Prof: Abdelghani Boudhar
Statistique Bivariée
1. Lecture de données
clc,clear,close all
path='C:\Users\S.OUSSAOUI\Desktop\MASTER\Outil de programmation\'
path =
'C:\Users\S.OUSSAOUI\Desktop\MASTER\Outil de programmation\'
Warning: Function power has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potenti
Warning: Function power has the same name as a MATLAB builtin. We suggest you rename the function to avoid a
potential name conflict.
xlfile='Temperature.xlsx'
xlfile =
'Temperature.xlsx'
namefxl =
'C:\Users\S.OUSSAOUI\Desktop\MASTER\Outil de programmation\Temperature.xlsx'
DTable=readtable(namefxl)
1
Date IMESKER ARMED TACHDERT NELTNER OUKASM jour mois
2
Date IMESKER ARMED TACHDERT NELTNER OUKASM jour mois
3
Date IMESKER ARMED TACHDERT NELTNER OUKASM jour mois
Tair = 244×4
23.4965 22.5600 19.0390 9.4102
21.3763 18.6300 15.2107 6.3155
15.2702 13.2100 10.0352 5.6664
18.7171 15.6400 12.9345 12.9612
20.5888 17.6600 17.4945 12.5817
26.1367 23.3200 19.5091 9.6827
24.3471 21.3800 18.7000 10.6445
25.5508 22.1900 19.1023 11.1224
23.9505 19.2500 16.3748 8.0913
4
24.7203 19.6100 17.2416 7.6064
4.Statistique Bivariée
%Extraire des variables depuis la table xls
stname=DTable.Properties.VariableNames(2:6)
Tair = 244×4
23.4965 22.5600 19.0390 9.4102
21.3763 18.6300 15.2107 6.3155
15.2702 13.2100 10.0352 5.6664
18.7171 15.6400 12.9345 12.9612
20.5888 17.6600 17.4945 12.5817
26.1367 23.3200 19.5091 9.6827
24.3471 21.3800 18.7000 10.6445
25.5508 22.1900 19.1023 11.1224
23.9505 19.2500 16.3748 8.0913
24.7203 19.6100 17.2416 7.6064
altitude = 1×4
1404 1940 2343 3230
r = 0.9756
r2 = 2×2
1.0000 0.9756
0.9756 1.0000
[rho,pval] = corr(DTable.IMESKER,DTable.ARMED,'type','Pearson')
rho = 0.9756
pval = 2.4739e-161
[r,p] =corr(DTable{:,(2:5)}) % la correlation entre tous les station sous forme d'une matrice
r = 4×4
1.0000 0.9756 0.9422 0.7056
0.9756 1.0000 0.9715 0.7467
0.9422 0.9715 1.0000 0.8223
0.7056 0.7467 0.8223 1.0000
p = 4×4
1.0000 0.0000 0.0000 0.0000
5
0.0000 1.0000 0.0000 0.0000
0.0000 0.0000 1.0000 0.0000
0.0000 0.0000 0.0000 1.0000
n=1;
p = polyfit(DTable.ARMED,DTable.IMESKER,n)
p = 1×2
1.0406 2.3960
% Y=aX+b
a=p(1); %coefficient directeur
b=p(2); %’ordonnée à l’origine
[p,S2] =polyfit(DTable.ARMED,DTable.IMESKER,n); % S2, Strcuture to estimate Error fit
plot(DTable.ARMED,DTable.IMESKER,'rx',"LineWidth",1)
xlabel(stname{2}),ylabel(stname{1})%AJUSTEMENT
Imsker_Cal=polyval(p,DTable.ARMED);
% Ycal2=polyval(p2,cc);
% plot(DTable.ARMED,DTable.IMESKER,'bd', DTable.ARMED,Imsker_Cal,'-',cc,Ycal2,'r--')
plot(DTable.ARMED,DTable.IMESKER,'gx', DTable.ARMED,Imsker_Cal,'-r')
xlabel(stname{2}),ylabel(stname{1}) %afficher les noms des axes
title('Linear and Polynomial Fit of Tair Data')
grid on %activation du grid
legend ("Observed",strcat("Fit Order ",num2str(n)))
6
[Imsker_Cal,delta] = polyval(p,DTable.ARMED,S2);% to estimate Error fit
plot(DTable.Date,DTable.IMESKER,'-b',"LineWidth",1)
hold on %activer le travail dans meme figure
plot(DTable.Date,Imsker_Cal,'-.r',"LineWidth",1)
xlabel('Time'), ylabel(['Tair ',stname{1}, ' °C'])
title('Linear and Polynomial Fit of Tair Data')
grid on
legend ("Observed",strcat("Estimated from Poly Order ",num2str(n)))
hold off
7
clear, clc,close all
data= readtable('Temperature.xlsx'); %importation des données
var1=2+1;
var2=3+1;
figure
plot(data.Date,data.ARMED,'-r','LineWidth',1)
hold on
plot(data.Date,data.TACHDERT,'-b','LineWidth',2);
xlabel('TIME [Day]'), ylabel('Air Temperature [°C]');
%importer les nom de légend d'après l'entete de la table
legend(data.Properties.VariableNames{var1},data.Properties.VariableNames{var2})
title ('Air Temperature Variation')
grid on
hold off
8
[l,c]=find( isnan (data.TACHDERT))
l =
c =
nombre=length(l)
nombre = 0
indices=[l,c];
indices=indices'
indices =
[r1,p1]=corrcoef(data.ARMED,data.TACHDERT)
r1 = 2×2
1.0000 0.9715
0.9715 1.0000
p1 = 2×2
1.0000 0.0000
0.0000 1.0000
[r,p]= corrcoef(data.ARMED,data.TACHDERT,'Rows','complete')
9
r = 2×2
1.0000 0.9715
0.9715 1.0000
p = 2×2
1.0000 0.0000
0.0000 1.0000
dlacunes=[data.ARMED(l,1),data.TACHDERT(l,1)]
dlacunes =
10
plot(xobs,yobs,'r.')
hold on
plot(xobs,Ycal1,'b-')
plot(xobs,Ycal1+2*delta,'g--',xobs,Ycal1-2*delta,'g--')
title('Polynomial Fit of Data with 95% Prediction Interval')
legend('Data','Linear Fit','95% Prediction Interval')
grid on
xlabel(data.Properties.VariableNames{var1}),ylabel(data.Properties.VariableNames{var2});
hold off
11
dlacunes=[data.ARMED(l,1),data.TACHDERT(l,1)]
dlacunes =
TS2fill=polyval(p1,data.ARMED(l,1))
TS2fill =
gapfill=data.TACHDERT
gapfill = 244×1
19.0390
15.2107
10.0352
12.9345
17.4945
19.5091
18.7000
19.1023
16.3748
17.2416
gapfill(l,1)=TS2fill
gapfill = 244×1
12
19.0390
15.2107
10.0352
12.9345
17.4945
19.5091
18.7000
19.1023
16.3748
17.2416
figure
plot(data.Date,data.TACHDERT,'-c')
hold on
plot(data.Date(l,1),data.ARMED(l,1),'+b')
plot(data.Date(l,1),TS2fill,'.r')
plot(data.Date,gapfill,'-g')
legend('data gap','data Ref Stat','gapfill Point','gapfill')
% T=DTable.ARMED;
T=data.ARMED;
vtime=data.Date;
T(3:7)=nan;T(10:12)=nan;T(100:102)=nan;
nans = isnan(T); % indices to NaN values in T % (assumes there are no NaNs in vtime)
13
T2=T;%List with missing data Nan
nbrnan=sum(isnan(T))% Nombre de données manquante
nbrnan = 11
Dt = 244×2
22.5600 22.5600
18.6300 18.6300
NaN 19.2233
NaN 19.8167
NaN 20.4100
NaN 21.0033
NaN 21.5967
22.1900 22.1900
19.2500 19.2500
NaN 18.8575
nanPrPost = 1×2
11 0
figure
plot(vtime,T2,'*',vtime,T,':'); grid on
legend ('raw data','corrected data')
14
15