// TP 2 MT12 P25 /// /// /// /// /// /// /// /// PARTIE 1 : Récursivité ////////////////// clear function U=Suite(n) if n<1 then U = null elseif n == 1 then U=1 else U = Suite(n - 1) / 2 // Calcule U_n puis le divise par 2. end endfunction if exists('u') then disp('u') end // ------------ // Q1 //------------ function U= suite_1(n) if n<0 then U = null elseif n ==0 then U=1 else U = suite_1(n - 1) +1 // end endfunction // ------------ // Q2 //------------ function U= suite_Fibo(n) if n<0 then U = null elseif n ==0 | n ==1 then U=1 else U = suite_Fibo(n-1)+ suite_Fibo(n-2) end endfunction // ------------ // Q3 //------------ function U= f(n,x) if n<0 then U = null elseif n ==0 then U=x else U = f(n-1,x).*x end endfunction // ------------ // Q4 //------------ scf() n=50 x=linspace(-1,1,n) title("Suites de Fonctions") plot(x, f(0, x), 'r') plot(x, f(1, x), 'g') plot(x, f(2, x), 'b') /// /// /// /// /// /// /// /// PARTIE 2 Polynome de Legendre ////////////////// clear // ------------ // Q5 //------------ function y=LegendreP(n,x) if n<0 then y= null elseif n==0 then y=1; elseif n==1 then y=x; elseif n>1 then y=(1/n).*((2*(n-1)+1).*x.*LegendreP(n-1,x)-(n-1).*LegendreP(n-2,x)) end endfunction // ------------ // Q6 //------------ a_0=sqrt(2)*sinh(1); a_1=-sqrt(6)/exp(1); a_2=(exp(2)-7)/exp(1)*sqrt(5/2); a_3=(5*exp(2)-37)/exp(1)*sqrt(7/2); a_4=3*sqrt(2)*(18*exp(2)-133)/exp(1); a_5=(329*exp(2)-2431)/exp(1)*sqrt(11/2); a_6=(3655*exp(2)-27007)/exp(1)*sqrt(13/2); A=[a_0,a_1,a_2,a_3,a_4,a_5,a_6] /* int_[-1,1] exp(-2x) dx= (1/2) (exp(2)-exp(-2))=sinh(2) */ disp(sinh(2)-sum(A.^2)) // ------------ // Q7 //------------ x = linspace(-1,1,400); scf() subplot(3,1,1) co=['r','g','b','y','c','m','k'] for i=0:6 plot(x, LegendreP(i, x),co(i+1)) end title("Polynômes de Legendre") /* Alternative de plot plot(x, LegendreP(0, x),'r') plot(x, LegendreP(1, x),'g') plot(x, LegendreP(2, x),'b') plot(x, LegendreP(3, x),'y') plot(x, LegendreP(4, x),'c') plot(x, LegendreP(5, x),'m') plot(x, LegendreP(6, x),'k')*/ // Approximation // Norme des poly de Legendre function y=normL(k) y=sqrt(2/(2*k+1)); endfunction // Approx sur la famille des poly Legendre (sans boucle for) x = linspace(-1,1,400) LegendreP_f_6=[ones(1,length(x))*LegendreP(0,x)/normL(0);LegendreP(1,x)/normL(1); LegendreP(2,x)/normL(2);LegendreP(3,x)/normL(3) ; LegendreP(4,x)/normL(4);LegendreP(5,x)/normL(5);LegendreP(6,x)/normL(6)] pi_6f= A*LegendreP_f_6 y=pi_6f err_exp=exp(-x)-y // Alternative Approx sur la famille des poly Legendre avec une boucle for function y=approx_Le(A,k,x) B=ones(1,k+1); for i=0:k B(1,i+1)=(LegendreP(i, x))./normL(i); end y=sum(B.*A); endfunction //Vecteur des images de l'approx y=zeros(1,length(x)) for i=1:length(x) y(i)=approx_Le(A,6,x(i)); end /* Alternative approx de Legendre */ function y=approx_Le_Alt(A,k,x) B=ones(k+1,length(x)); for i=0:k B(i+1,:)=(LegendreP(i, x))./normL(i); end y= A*B endfunction // fonction d'erreur function z=err_exp(x,k) z=zeros(1,length(x)) for i=1:length(x) z(i)=exp(-x(i)) - approx_Le(A,6,x(i)) ; end endfunction // PLOT de l'approximation scf() subplot(3,1,2) plot(x,exp(-x),'ob') plot(x,y,'+g') title("Fonction exp(-x) et son approximation") subplot(3,1,3) z=err_exp(x,6); plot(x,z) title("Erreur approximation") // ------------ // Q8 -- Q12 sur la fonction valeur abs sur [-1,1] //------------ function y=coeff(k) if k<0 then y = null elseif k/2 ~= floor(k/2) then y=0 else y= (2/normL(k))*integrate('x*LegendreP(k, x)','x',0,1) end endfunction C=[coeff(0),coeff(2),coeff(4),coeff(6)] x = linspace(-1,1,400); // Approximation Legendre sans boucle for LegendreP_abs_6=[ones(1,length(x))*LegendreP(0,x)/normL(0) ;LegendreP(2,x)/normL(2); LegendreP(4,x)/normL(4); LegendreP(6,x)/normL(6)] LegendreP_abs_4= LegendreP_abs_6(1:3,:) LegendreP_abs_2= LegendreP_abs_6(1:2,:) y2=C(1,1:2)* LegendreP_abs_2 y4=C(1,1:3)* LegendreP_abs_4 y6=C* LegendreP_abs_6 Y=[y2; y4; y6] // Erreur d'approx err=repmat(abs(x),3,1)-Y // Alternative Approximation Legendre (boucle for)) D=zeros(length(x),4); for i=1:length(x) D(i,:)=[ C(1)*LegendreP(0,x(i))/normL(0), C(2)* LegendreP(2,x(i))/normL(2),C(3)* Legendre P(4,x(i))/normL(4), C(4)*LegendreP(6,x(i))/normL(6)]; end y2= sum(D(:,1:2),"c") ; // pi_2f(x)) y4= sum(D(:,1:3),"c") ; // pi_4f(x)) y6= sum(D,"c") ; // pi_6f(x) ) Y=[y2'; y4'; y6']; // Erreur d'approx err=zeros(length(x),3); for i=1:3 err(:,i)=abs(x)-Y(i,:); end scf() subplot(2,1,1) title("abs,Approximation k=2, k=4, k=6") plot(x,[abs(x); y2; y4; y6]) legend(['ordre2' ;'ordre4' ;'ordre6'],4) // position de la légende subplot(2,1,2) title("Erreurs Approximation k=2, k=4, k=6") plot(x,err(1,:),co(1)) plot(x,err(2,:),co(2)) plot(x,err(3,:),style=co(3)) legend(['ordre2' ;'ordre4' ;'ordre6'],4)