Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Interpolacja
clear;
global n = 50;
x = sort(rand(1,n).*10);
y = rand(1,n).*20-10;
function f = nwtn(x,known_x,a)
global n;
f = 0;
for i=1:n
elem = a(i);
for j=1:i-1
elem = elem * (x-known_x(j));
endfor
f = f + elem;
endfor
endfunction
for i=1:n
a = y;
for j=2:n
for k=n:-1:j
a(k)=(a(k)-a(k-1))/(x(k)-x(k-j+1));
endfor
endfor
endfor
xx = -1:0.01:11;
i = 0;
for x_=xx
yy(++i) = nwtn(x_,x,a);
endfor
plot(x,y,"x","markersize", 20,xx,yy);
axis([0, 11, -11, 11]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment