clear for id=winsid() close(scf(id)) end //exercice préliminaire function U=maSuite(n) if n<0 then U=null elseif n == 0 then U = 1 else U = maSuite(n - 1) + 1 end endfunction function U=fibonacci(n) if n<0 then U=null elseif n == 0 | n == 1 then U = 1 else U = fibonacci(n-1) + fibonacci(n-2) end endfunction function F=fctSuite(n, x) if n<0 then F=null elseif n == 0 then F=x else F=x.*fctSuite(n-1, x) end endfunction n=50 x=linspace(-1,1,n) title("Exercice préliminaire") plot(x, fctSuite(0, x), 'r') plot(x, fctSuite(1, x), 'g') plot(x, fctSuite(2, x), 'b') //Q1 function y=LegendreP(n, x) if n<0 then y=null elseif n==0 then y=ones(1, size(x,2)) elseif n==1 then y=x else y=(2-1/n).*x.*LegendreP(n-1,x)-(1-1/n).*LegendreP(n-2,x) end endfunction 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] //Q2 f2=sinh(2) disp(f2-sum(A^2)) //Q3 n=400 x=linspace(-1,1,n) scf() subplot(3, 1, 1) title("Les 7 premiers polynômes de Legendre") for i=0:7 plot(x, LegendreP(i, x)) end subplot(3, 1, 2) title("exp(-x) et son approximation") plot(x, exp(-x), "g") function y=norm_LegendreP(i) y=sqrt(2/(2*i+1)) endfunction function y=pif(a, n, x) y=0 for i=0:n y=y+a(i).*LegendreP(i, x)/norm_LegendreP(i) end endfunction function a=a_exp(k) a=A(k+1) endfunction plot(x, pif(a_exp, 6, x), ":") legend("exp(-x)", "approximation au rang 6") subplot(3, 1, 3) title("Erreur d''approximation au rang 6") plot(x, exp(-x)-pif(a_exp, 6,x)) //Q4 function a = a_abs(k) if k<0 then a=null elseif pmodulo(k,2)==1 then a=0 else a=sqrt(4*k+2)*integrate("x*LegendreP(k, x)", "x", 0, 1) end endfunction scf() //remarque: il serait plus efficace de "cacher" le résultat de pif(...) subplot(2,1,1) plot(x, pif(a_abs, 2, x),"r") plot(x, pif(a_abs, 4, x),"g") plot(x, pif(a_abs, 6, x),"b") legend(["degré 2", "degré 4", "degré 6"]) title("Approximations de |x| par des polynômes de Legendre") subplot(2,1,2) plot(x, abs(x) - pif(a_abs, 2, x),"r") plot(x, abs(x) - pif(a_abs, 4, x),"g") plot(x, abs(x) - pif(a_abs, 6, x),"b") legend(["degré 2", "degré 4", "degré 6"]) title("Erreur d''approximations de |x| par des polynômes de Legendre")