function [yi, ypi, P, D] = neville(x, y, xi) % NEVILLE Interpolation usin Neville!s "et#o$ % NEVILLE(,&,I) NEVILLE(,& ,I) interpolates to fin$ &I, t#e value of % t#e un$erlyin function & at t#e point I, usin Neville!s % "et#o$' an$ & ust e vectors of lent# N' % % [&I,&PI] = NEVILLE() also returns t#e interpolate$ $erivative % &PI' % % [&I,&PI,P,D] [&I,&PI,P,D ] = NEVILLE() also returns t#e polynoial tale % P an$ $erivative polynoial tale D calculate$ for t#e last I' % *oe +ennin -all ./00 % 1n Iterative "et#o$ of Nuerical Differentiation % D' 2' +unter n = lent#(x)3 for 4 = 05lent#(xi) x$ = []3 for i = 05n x$(i) = as(x(i) xi(4))3 en$ [x$s,i] = sort(x$)3 x = x(i)3 y = y(i)3 P = 6eros(n,n)3 P(5,0) = y(5)3 for i = 05n0 for 7 = 05(ni) P(7,i80) = ((xi(4)x(7))9P(780,i) ((xi(4)x(7)) 9P(780,i) 8 (x(78i)xi(4))9P(7,i)): (x(78i)xi(4) )9P(7,i)): (x(78i)x(7))3 en$ en$ yi(4) = P(0,n)3 D = 6eros(n,n)3 D(5,0) = y(5)3 for i = 05n0 D(i,.) = (D(i80,0)D(i,0)):(x(i8 (D(i80,0)D(i,0)):(x(i80)x(i))3 0)x(i))3 en$ for i = .5n0 for 7 = 05(ni) D(7,i80) = (P(780,i) 8 (xi(4)x(7))9D(780,i) (xi(4)x(7) )9D(780,i) P(7,i) 8 (x(78i) xi(4))9D(7,i)):(x(78i)x(7))3 en$ en$ ypi(4) = D(0,n)3 en$
A Neville's algorithm example in Matlab Neville's algorithm as given in the book is easy to implement in Matlab, if one realizes that there is a quick way to deal with the "0 vs. " inde!ing issue. hat is, arrays in other languages are frequently inde!ed from i#0 to i#n. his is what the book assumes. $ut since the Matlab language has the concept of a matri! so firmly imbedded, all indices start at i#. %o, to write &lgorithm . in Matlab, one trick is to add one to indices, but keep the "for" loops the same. hat is, the only line that needs to change is the main line( ;(i80,780) = ((xxx(i780))9;(i80,7) (xxx(i80))9;(i,7)):''' (x(i80)x(i780))3
&gain, note that the only change is that the indices in this line have one. he loops are still(
all been
increased by
for i = 05n for 7 = 05i
here is a better way to write &lgorithm .. Namely, to compute each new column of the Neville's algorithm table, so that the program doesn't have to store away the whole table as a matri!, but rather )ust a vector which is a column. his is what the code below does. here is a disadvantage**the program can not be modified to add nodes as needed, as suggested on page +. t should be saved as "nev.m"( function y = nev(xx,n,x,;) % Neville!s alorit# as a function (save as
Now, to run this on problem . in assignment - would look like( > x=0?@/50/50??/ x =
0?@/
0?A/
0?B/
0?C/
> ;=[0.0BA 0A0.B 0C?. .//. ..BA@. .@?B] ; =
0?/
0??/
0.0BA
0A0.B
0C?.
.//.
..BA@.
.@?B
> nev(0?@/,A,x,;) % 7ust a c#ec4 ans =
0.0BA
> nev(0?/,A,x,;) ans =
0B?B@?
> nev(.///,A,x,;) ans =
.A0BA@
>
$ut to actually plot out some polynomials, as in problem . on assignment -, you probably want to actually write a new program, for instance( % save in soet#in li4e
% plot a couple #un$re$ points in eac# case
% first plot t#e oriinal function yy=0':(08.A9xx'G.)3 plot(xx,yy)3 #ol$ on3 % t#is a4es t#e plots t#at folloF pile up
for n=[. @ 0B] x=05.:n503 % (c#ec4 t#is For4s for n=@, for instance) ;=0':(08.A9x'G.)3 % et t#e interpolation points for i=05./0 yy(i)=nev(xx(i),n,x,;)3 %interpolate en$3 plot(xx,yy)3 en$3 % inessential eellis#ents5 axis([0 0 . .])3 % experientation s#oFe$ t#is Fas nice plot(x,;,!o!)3 % s#oF t#e 0B interpolation pointslast values of x an$ ; title(!Polynoial interpolation of y=0:(08.A9xG.)t#e une exaple!)3 #ol$ off3
% so future plots start over