1 Solution matlab
Transcription
1 Solution matlab
ENS Cachan Antenne de Bretagne Correction TP Agreg n◦ 1 Ce corrigé succint vous permettra de comparer les deux logiciels, en particulier en ce qui concerne les graphiques. 1 Solution matlab Il nous faut définir deux fonctions pour les méthodes 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; Puis un script principal : Sex=exp(1)-1; ErrRect=[];ErrCarlo=[]; nn=100:100:10000; 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 l’’intgration 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); On obtient le résultat suivant : Graphique logarithmique des erreurs pour l’intégration numérique −3 Méthode des rectangles (pente=−0.99984) Méthode de Monte−Carlo (pente=−0.52871) −4 −5 |erreur| −6 −7 −8 −9 −10 −11 4.5 5 5.5 6 6.5 7 n 1 7.5 8 8.5 9 9.5 2 Solution scilab On définit de même les deux méthodes : function S=rect(f,n) y=f(1/n*[0:n-1]); S=sum(y)/n; 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)’); str1=[’Mthode des rectangles (pente=’,string(aR),’)’]; str2=[’Mthode de Monte-Carlo (pente=’,string(aC),’)’]; str=strcat([str1,’@’,str2]); plot2d([lnn’,lnn’],[log(ErrRect’),log(ErrCarlo’)] ,[-1,-9],"121",str); 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)); xtitle(’Graphique logarithmique des erreurs pour l’’intgration numrique’,’n’,’|erreur|’) On obtient le résultat suivant : Graphique logarithmique des erreurs pour l’intØgration numØrique |erreur| Ο -3.27 Ο Ο Ο -3.90 ΟΟ Ο -4.54 Ο + Ο + -5.82 + + -6.46 + + Ο + + -7.09 + + Ο Ο Ο ΟΟ Ο Ο Ο ΟΟ Ο Ο Ο Ο Ο Ο ΟΟ Ο Ο Ο Ο Ο Ο Ο Ο Ο Ο Ο Ο ΟΟ ΟΟ Ο Ο Ο ΟΟ Ο ΟΟ ΟΟ Ο Ο ΟΟΟΟ Ο Ο Ο Ο Ο Ο Ο Ο Ο Ο Ο Ο Ο Ο Ο Ο Ο Ο ΟΟ Ο Ο -5.18 Ο Ο Ο Ο + Ο Ο Ο Ο + ++ ++ Ο Ο ΟΟ Ο Ο ++ -7.73 -8.37 -9.01 Ο ++ ++ ++ +++ +++ +++ +++ Ο ++++ ++++ ++++ +++++ +++++ +++++ Ο ++++++ ++++++ +++++++ +++++++ ++++++++ +++ n Ο -9.65 4.61 5.07 5.53 5.99 6.45 6.91 + Ο MØthode des rectangles (pente=-0.9998374) MØthode de Monte-Carlo (pente=-0.5050933) 7.37 7.83 8.29 8.75 9.21 CONCLUSIONS : Cette simulation est très simple, mais représente le niveau de complexité requis à l’agreg. Il n’est pas nécessaire d’écrire un long programme ; il vaut mieux une petite simulation bien choisie. Quant au logiciel, c’est une question de goût... 2