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. PRIMI PASSI IN MATLAB®<br />

for<br />

end<br />

end<br />

end<br />

k =2: j<br />

table ( j , k)= ( table ( j , k−1) − table ( j −1,k−1) ) / . . .<br />

( x ( j ) − x ( j−k+1) ) ;<br />

La <strong>di</strong>agonale principale della matrice table ha i coefficienti a 0 , a 1 , ... a n del polinomio <strong>di</strong> interpolazione.<br />

Il polinomio è dunque:<br />

p(x) = a 0 + a 1 (x − x 0 ) + a 2 (x − x 0 )(x − x 1 ) + ... + a n (x − x 0 )(x − x 1 )···(x − x n−1 )<br />

Per valutarlo in un punto x eseguiamo i seguenti passaggi, applicando, in tal modo, l’algoritmo <strong>di</strong> Horner:<br />

p = a n<br />

p = p(x − x n−1 ) + a n−1<br />

= a n (x − x n−1 ) + a n−1<br />

p = p(x − x n−2 ) + a n−2<br />

.<br />

= a n (x − x n−1 )(x − x n−2 ) + a n−1 (x − x n−2 ) + a n−2<br />

p = p(x − x 0 ) + a 0<br />

= a n (x − x n−1 )(x − x n−2 )...(x − x 1 )(x − x 0 ) + ... + a 1 (x − x 1 )(x − x 0 ) + a 0<br />

= a 0 + a 1 (x − x 0 )(x − x 1 ) + ... + a n (x − x 0 )(x − x 1 )...(x − x n−2 )(x − x n−1 )<br />

La function da scrivere è dunque:<br />

function yval= i n t e r p d i v d i f ( xval , x , table )<br />

% function yval= i n t e r p d i v d i f ( xval , x , table )<br />

% x − a s c i s s e 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 <strong>di</strong>vise ,<br />

% ottenuta dalla function d i v d i f ;<br />

% servono i v a l o r i della <strong>di</strong>agonale principale<br />

% xval − s c a l a r e o v e t t o r e in cui c a l c o l a r e i l polinomio<br />

% i n t e r p o l a t o r e<br />

% yval − valore ( o v a l o r i ) del polinomio i n t e r p o l a t o r e<br />

% valutato in xval<br />

% nel calcolo del polinomio i n t e r p o l a t o r e s i applica<br />

% l ’ algoritmo <strong>di</strong> Horner<br />

xval=xval ( : ) ;<br />

x=x ( : ) ;<br />

n=length ( x ) −1;<br />

yval=table (n+1 ,n+1)* ones ( length ( xval ) , 1 ) ;<br />

for j =n: −1:1<br />

yval=yval . * ( xval−x ( j ) ) + table ( j , j ) ;<br />

end<br />

Le functions appena descritte possono essere usate in maniera del tutto equivalenti per risolvere il problema<br />

dell’interpolazione. Ci sono però dei casi in cui i risultati ottenuti dalla polyfit e dalla interpmonom<br />

non sono corretti in quanto la matrice <strong>di</strong> Vandermonde ad esse legate è malcon<strong>di</strong>zionata.<br />

Abbiamo già descritto gli effetti del malcon<strong>di</strong>zionamento nell’interpolazione. Quando applichiamo la<br />

function polyfit ai dati <strong>di</strong> quel problema, si ha il seguente messaggio <strong>di</strong> avvertimento (un warning):<br />

>> poli=polyfit(x,y,5)<br />

Warning: Polynomial is badly con<strong>di</strong>tioned. Add points with <strong>di</strong>stinct X<br />

values, reduce the degree of the polynomial, or try<br />

200

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

Saved successfully!

Ooh no, something went wrong!