Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
34 lines (29 sloc) 884 Bytes
% x = liste der x werte bspw: [0.6 0.7 0.8]
% y = lister der y werte bsp: [0.8136 0.9967 1.1944]
% z = gesuchter x wert
% bsp aufruf:
% Lagrange([0.6 0.7 0.8], [0.8136 0.9967 1.1944], 0.66)
% Lagrange([-1 1 3], [0.5 2 8], 2)
% Falls man die exakte Kurve kennt (funktion(für punkt) für exakt einsetzten):
% exakt = 2^2;
% fprintf('Absoluter Fehler %.12f\n', abs(exakt - Lagrange([-1 1 3], [0.5 2 8], 2)));
% fprintf('Relativer Fehler %.12f\n', abs(exakt - Lagrange([-1 1 3], [0.5 2 8], 2)) / exakt);
function p = Lagrange(x, y, z)
x = x';
y = y';
n = length(x);
p = 0;
for i = 1 : n
p = p + li(z, x, i) * y(i);
fprintf('%f * %f += %f \n', y(i), li(z, x, i), p);
end
end
function p = li (z, x, i)
n = length(x);
p = 1;
for j = 1 : n
if j ~= i
p = p * (z - x(j)) / (x(i) - x(j));
end
end
end
You can’t perform that action at this time.