Soluzione per Es1
1 clear all 2 close all
3 % plot della funzione per individuare 4 % dove si trova lo zero
5 x=linspace(0,1,1000);
6 f=@(x) exp(−x).*sin(4*x−pi/2)+x;
7 figure(1)
8 plot(x,f(x)); grid;
9 % dal grafico si evince che un intervallo 10 % per la ricerca dello zero e' [0.25,0.35]
11 x0=0.25;
12 fp=@(x) 1+exp(−x).*(4*cos(4*x−pi/2)−sin(4*x−pi/2));
13 [x,iter]=Newton(f,fp,x0,1.e−6,20)
14 % sono necessarie solo 3 iterazioni, confermando l' ordine di
15 % convergenza quadratico del metodo di Newton
1
Solution for Ex 2. At rst write 1 clear all; close all;
2 xbar=linspace(−1,1,100)'; % Evaluation points 3 ybar=1./(1+25*xbar.^2); % Needed for tests 4 n=15;
5 xC=−cos((2*[1:n]−1)*pi/(2*n)); %Chebyshev points 6 xCL=−cos([0:n−1]*pi/(n−1)); %Chebyshev−Lobatto points 7 yC=1./(1+25*xC.^2); %Runge fct at the interp. pts 8 yCL=1./(1+25*xCL.^2); %Runge fct at the interp. pts 9 n = n−1;
10 coef = polyfit(xC,yC,n);
11 pC = polyval(coef,xbar);
12 coef = polyfit(xCL,yCL,n);
13 pCL = polyval(coef,xbar);
14 figure(1), plot(xbar,ybar,'g−−', xC,yC,'bo',...
15 xbar,pC,'r−'); grid;
16 legend('Original function','Chebyshev points',...
17 'Interpolation on Chebyshev points');
18 figure(2), plot(xbar,ybar,'g−−', xCL,yCL,'bo',...
19 xbar,pCL,'r−'); grid;
20 legend('Original function',...
21 'Chebyshev−Lobatto points',...
22 'Interpolation on Chebyshev−Lobatto points');
23 % calcolo degli errori 24 max(abs(pC−ybar)) 25 max(abs(pCL−ybar))
2
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 0
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Original function Chebyshev points
Interpolation on Chebyshev points
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Original function Chebyshev-Lobatto points
Interpolation on Chebyshev-Lobatto points
3