1> f = @(x) 1./(1 + x.^2); 2> x = linspace(-5,5); 3> plot(x, y) error: 'y' undefined near line 3 column 9 error: evaluating argument list element number 2 3> plot(x, f(x)) 4> nodic = -cos(pi/9*[0:9]) * 5 nodic = -5.00000 -4.69846 -3.83022 -2.50000 -0.86824 0.86824 2.50000 3.83022 4.69846 5.00000 5> p9c = polyfit(nodic, f(nodic), 9) p9c = Columns 1 through 9: -1.2698e-020 8.3649e-006 5.3091e-019 -5.8345e-004 -6.1653e-018 1.4634e-002 6.2987e-018 -1.5759e-001 5.8390e-017 Column 10: 6.8090e-001 6> hold on 7> yp9c = polyval(p9c, x); 8> plot(x, yp9c, 'r') 9> nodic = -cos(pi/10*[0:10]) * 5; 10> p10c = polyfit(nodic, f(nodic), 10); 11> yp10c = polyval(p10c, x); 12> plot(x, yp10c, 'g') 13> errore = max(abs(f(x)-yp10c)) errore = 0.13185 14> x=linspace(-5,5,1.e6); 15> yp10c = polyval(p10c, x); 16> errore = max(abs(f(x)-yp10c)) errore = 0.13220 17> % Esempio di un'interpolazione instabile: 17> % Quarteroni-Saleri, esempio 3.3, pag. 85 17> f=inline('sin(2*pi*x)') f = f(x) = sin(2*pi*x) 18> nodi=linspace(-1,1,22); 19> p21=polyfit(nodi,f(nodi),21); 20> x=-1 : 0.01 : 1; 21> length(x) ans = 201 22> % plottiamo la f (curva blu) e il polinomio interpolante (curva rossa); 22> % la curva rossa copre perfettamente la curva blue. 22> plot(x,f(x), x,polyval(p21,x),'r') 23> hold off 24> plot(x,f(x), x,polyval(p21,x),'r') 25> % numeri casuali normalmente distribuiti con la media 0 25> % e la deviazione standard 1 25> randn(1) ans = -0.32536 26> randn(1) ans = 0.56189 27> randn(1) ans = -0.47643 28> randn(1) ans = 0.16452 29> randn(1) ans = 0.084893 30> randn(1) ans = -1.1103 31> p21perturb = polyfit(nodi,f(nodi)+1.e-5*randn(1,22),21); 32> hold on 33> plot(x,polyval(p21perturb,x),'g') 34> p21perturb = polyfit(nodi,f(nodi)+1.e-4*randn(1,22),21); 35> plot(x,polyval(p21perturb,x),'k') 36> % costante di Lebesgue (stima) 36> 2^22/(exp(1)*21*(log(21)+0.57722)) ans = 2.0288e+004 37> quit