%% Polynomial Fit Examples
% Examples using data from textbook Example 4.2

format longg;

x=[3.35 3.40 3.50 3.60]'
f=[0.298507 0.294118 0.285714 0.277778]'
A=[ones(size(x)) x x.^2 x.^3]
b=A\f

display(['Function f(x)=1/x at x=3.44, f(x)=', num2str(1/3.44,'%1.16e')]);

display(['Cubic Interpolation at x=3.44, f(x)=', num2str(polyval(flipud(b),3.44),'%1.16e')]);

display(['Quadratic Interpolation at x=3.44, f(x)=', num2str(polyval(vander(x(1:3))\f(1:3),3.44),'%1.16e')]);

display(['Linear Interpolation at x=3.44, f(x)=', num2str(polyval(vander(x(2:3))\f(2:3),3.44),'%1.16e')]);

a=x(1)
b=x(2)
c=x(3)
%Ruin variable x... then let it =3.44, the point of interest
x=3.44

P1=((x-b)/(a-b))*f(1)+((x-a)/(b-a))*f(2)
P2=((x-b)*(x-c)/((a-b)*(a-c)))*f(1)+((x-a)*(x-c)/((b-a)*(b-c)))*f(2)+((x-a)*(x-b)/((c-a)*(c-b)))*f(3)