clc
clear all
close all

x = [0.5:0.05:0.8]'
y = [1.2 1 0.7 0.6 0.1 -0.2 -0.6]'

%% LAGRANGE POLINOMIAL

plot(x,y,'k*')
hold on;

x_to_estimate = [0.5:0.001:0.8]';

for k = 1:length(x_to_estimate)
     x_desired = x_to_estimate(k);
     p_value = 0;
     for i = 0:length(x)-1
          %% CALCULAR l_i
          l_i = 1;
          for j = 0:length(x)-1
               if j ~= i
                    l_i = l_i*(x_desired-x(j+1))/(x(i+1)-x(j+1));
               endif
          endfor
          p_value = p_value + l_i*y(i+1);
     endfor

     plot(x_desired,p_value,'r')
     hold on;

endfor

% UTILIZANDO FUNÇÕES OCTAVE

##p = polyfit(x,y,length(x)-1) % POLINÓMIO INTERPOLADOR DE MENOR GRAU
##
##plot(x,y,'k*')
##hold on;
##plot([0.5:0.001:0.8],polyval(p,[0.5:0.001:0.8]),'r')
##%xlim([0.3 1])





