Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % MAIN LAB_4 MN
- x0=[1,2,4,6,8,10]
- y0=[6,6,4,2,0,0]
- plot(x0,y0,"g*")
- x = -1:0.1:10
- hold on;
- plot(x0, y0, "g*");
- %aproksymacja wielomianem 2 stopnia
- w2 = polyfit(x0, y0, 2);
- f2 = polyval(w2, x);
- plot(x, f2, "r");
- %aproksymacja wielomianem 3 stopnia
- w3 = polyfit(x0, y0, 3);
- f3 = polyval(w3, x);
- plot(x, f3, "b");
- y = f_lagrange(x0, y0, x);
- plot(x, y, 'k')
- hold off;
- % FUNKCJA LAGRANGE
- function y = f_lagrange(x0, y0, x)
- n = length(x0);
- y=0;
- for ii=1: n
- li=1;
- for jj=1: n
- if ii~=jj
- li = li .* (x - x0(jj))./(x0(ii) - x0(jj));
- endif
- endfor
- y = y + y0(ii) .* li;
- endfor
- endfunction
- % FUNKCJA NEWTON
- function y = f_newton(x0, y0, x)
- n = length(x0)-1;
- D = ones(n+1, n+1);
- for i=1: n+1
- D(i, 1) = y0(i);
- endfor
- for i=2: n+1
- for j=2: i
- D(i, j) = [D(i, j-1) - D(i-1, j-1)] ./ [x0(i) - x0(i-j+1)];
- endfor
- endfor
- y = D(1, 1);
- w=1;
- for i=2: n+1
- w = w .* (x - x0(i-1));
- y = y + D(i, i) * w;
- endfor
- endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement