Académique Documents
Professionnel Documents
Culture Documents
Antenne de Bretagne
Correction TP Agreg n 1
Ce corrige succint vous permettra de comparer les deux logiciels, en particulier en ce qui concerne
les graphiques.
Solution matlab
Il nous faut definir deux fonctions pour les methodes des rectangles et de Monte-Carlo :
function S=rect(f,n)
y=feval(f,1/n*[0:n-1]);
S=sum(y)/n;
function S=carlo(f,n)
y=feval(f,rand(n,1));
S=sum(y)/n;
aC=CoeffCarlo(1);bC=CoeffCarlo(2);
hold on
plot(lnn,aR*lnn+bR,b--,lnn,aC*lnn+bC,r--)
for n=nn
ErrRect=[ErrRect,abs(Sex-rect(exp,n))];
ErrCarlo=[ErrCarlo,abs(Sex-carlo(exp,n))]; title(Graphique logarithmique des erreurs
end
pour lintgration numrique)
xlabel(n)
clf
ylabel(|erreur|)
lnn=log(nn);
str1=[Mthode des rectangles
plot(lnn,log(ErrRect),b+
(pente=,num2str(aR),)];
,lnn,log(ErrCarlo),ro); str2=[Mthode de Monte-Carlo
(pente=,num2str(aC),)];
CoeffRect=polyfit(log(nn),log(ErrRect),1);
legend(str1,str2)
CoeffCarlo=polyfit(log(nn),log(ErrCarlo),1); grid on
aR=CoeffRect(1);bR=CoeffRect(2);
|erreur|
10
11
4.5
5.5
6.5
7
n
7.5
8.5
9.5
Solution scilab
function S=carlo(f,n)
y=f(rand(n,1));
S=sum(y)/n;
et le script principal :
getf rect.sci
getf carlo.sci
deff(y=g(x),y=exp(x));
Sex=exp(1)-1;
ErrRect=[];ErrCarlo=[];
nn=100:100:10000;
for n=nn
ErrRect=[ErrRect,abs(Sex-rect(g,n))];
ErrCarlo=[ErrCarlo,abs(Sex-carlo(g,n))];
end
plot2d([lnn,lnn],[aR*lnn+bR,aC*lnn+bC],[1,2]
,"001")
xclear()
lnn=log(nn);
[aR,bR,sig]=reglin(log(nn),log(ErrRect));
[aC,bC,sig]=reglin(log(nn),log(ErrCarlo));
-3.27
-3.90
-4.54
-5.82
+
+
-6.46
-7.09
-5.18
++
++
++
-7.73
-8.37
-9.01
++
++
++
+++
+++
+++
+++
++++
++++
++++
+++++
+++++
+++++
++++++
++++++
+++++++
+++++++
++++++++
+++
-9.65
4.61
5.07
5.53
5.99
6.45
6.91
+
Mthode des rectangles (pente=-0.9998374)
Mthode de Monte-Carlo (pente=-0.5050933)
7.37
7.83
8.29
8.75
9.21
CONCLUSIONS : Cette simulation est tr`es simple, mais represente le niveau de complexite
requis a
` lagreg. Il nest pas necessaire decrire un long programme ; il vaut mieux une petite
simulation bien choisie. Quant au logiciel, cest une question de go
ut...