18.01.2015 Views

Appunti di Calcolo Numerico - Esercizi e Dispense - Università degli ...

Appunti di Calcolo Numerico - Esercizi e Dispense - Università degli ...

Appunti di Calcolo Numerico - Esercizi e Dispense - Università degli ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

12.9. Applicazioni <strong>di</strong> MATLAB® nel <strong>Calcolo</strong> <strong>Numerico</strong><br />

’i vettori x e y non hanno la stessa lunghezza’)<br />

else<br />

xval=xval ( : ) ;<br />

yval=zeros ( length ( xval ) , 1 ) ; % s i crea un v e t t o r e <strong>di</strong> t u t t i zero<br />

n=length ( x ) −1; % n grado del polinomio<br />

for i =1:n+1<br />

yval = yval + lagrange ( xval , x , i ) * y ( i ) ;<br />

end<br />

end<br />

function yval=interplagrange ( xval , x , y )<br />

% dati i v e t t o r i x e y da interpolare<br />

% la function fa l ’ interpolazione <strong>di</strong> Lagrange valutandola<br />

% in xval<br />

i f length ( x)~= length ( y )<br />

error (’MATLAB:lagrange’ ,’x e y non hanno la stessa lunghezza’)<br />

else<br />

y=y ( : )<br />

n=length ( x ) −1;% n grado del polinomio<br />

A = [ ] ;<br />

for i =1:n+1<br />

A=[A , lagrange ( xval , x , i ) ] %ogni colonna della matrice A ha i l<br />

% valore dell ’ i−simo polinomio <strong>di</strong><br />

% Lagrange c a l c o l a t o nei v a l o r i xval<br />

end<br />

yval=A* y ;<br />

end<br />

Eseguiamo l’esempio <strong>di</strong> prima per vedere cosa si ottiene:<br />

>> yy=interplagrange(xx,x,y);<br />

>> plot(x,y,’o’, xx,yy)<br />

Ora la function interplagrange sostituisce l’uso delle due function polyfit, polyval o<br />

interpmonom, polyval.<br />

Calcoliamo ora il polinomio <strong>di</strong> interpolazione me<strong>di</strong>ate le <strong>di</strong>fferenze <strong>di</strong>vise <strong>di</strong> Newton. Scriviamo due<br />

functions, la prima che scrive la tabella delle <strong>di</strong>fferenze <strong>di</strong>vise, la seconda che valuta il polinomio <strong>di</strong> interpolazione<br />

implementando l’algoritmo <strong>di</strong> Horner 2 in modo da minimizzare il numero delle operazioni da<br />

eseguire.<br />

function table= d i v d i f ( x , y )<br />

% function table=<strong>di</strong>v<strong>di</strong>f ( x , y )<br />

% x − a s c i s s e dei dati da interpolare<br />

% y − or<strong>di</strong>nate dei dati da interpolare<br />

% table − t a b e l l a d e l l e d i f f e r e n z e d i v i s e<br />

x=x ( : ) ;<br />

y=y ( : ) ;<br />

n=length ( x ) ;<br />

m=length ( x ) ;<br />

i f n~=m<br />

error (’MATLAB:<strong>di</strong>fferenze_<strong>di</strong>vise’ ,’errore sui dati’)<br />

else<br />

table=zeros (n , n ) ; % iniziamo la t a b e l l a come una matrice <strong>di</strong> z e r i<br />

table ( : , 1 ) = y ;<br />

for j =2:n<br />

2 William Horner (1786-1837) fu un matematico inglese, ricordato essenzialmente per il suo metodo sulle equazioni algebriche.<br />

Spieghiamo l’algoritmo solo per rendere comprensibile la function che scriviamo.<br />

199

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!