TU Wien:Computernumerik VU (Schranz-Kirlinger)/Übungen SS16/Beispiel 37

Aus VoWi
Zur Navigation springen Zur Suche springen

Lösung[Bearbeiten | Quelltext bearbeiten]

Matlab Code

function Bsp7

x = [0 1 3];
y = [1 3 2];
x0 = 2;

fprintf('Ergebnis: %f\n',neville(x,y,x0));

end

function y0 = neville(x,y,x0)
n = length(x);
y0 = [];

for k = 1:length(x0)
    xd = [];
    for i = 1:n
        xd(i) = abs(x(i) - x0(k));
    end
    
    [~,i] = sort(xd);
    
    x = x(i);
    y = y(i);
    
    P = zeros(n,n);
    P(:,1) = y(:);
    
    for i = 1:n-1
        for j = 1:(n-i)
            P(j,i+1) = ((x0(k)-x(j))*P(j+1,i) + (x(j+i)-x0(k))*P(j,i))/(x(j+i)-x(j));
        end
    end
    
    y0 = cat(1, y0, P(1,n));
    
end

end