%% Annotated Matlab transcript -- 3/20 %% CS 323 - Doug DeCarlo % Example of a quadratic Lagrange interpolating polynomial x0 = 1; x1 = 2; x2 = 3; y0 = 1; y1 = 2.5; y2 = -1; x = 0:0.01:4; % plot L0(x), with the data -- see how L0(x0)=1, and L0(x1)=L0(x2)=0 plot(x,0,x0,y0,'.',x1,y1,'.',x2,y2,'.',x,(x-x1).*(x-x2)./((x0-x1).*(x0-x2))); % now plot the whole interpolating polynomials plot(x,0,x0,y0,'.',x1,y1,'.',x2,y2,'.',x,(x-x1).*(x-x2)./((x0-x1).*(x0-x2))*y0 + (x-x0).*(x-x2)./((x1-x0).*(x1-x2))*y1 + (x-x0).*(x-x1)./((x2-x0).*(x2-x1)) * y2); % we can change the data and see how the result changes y0 = -6; plot(x,0,x0,y0,'.',x1,y1,'.',x2,y2,'.',x,(x-x1).*(x-x2)./((x0-x1).*(x0-x2))*y0 + (x-x0).*(x-x2)./((x1-x0).*(x1-x2))*y1 + (x-x0).*(x-x1)./((x2-x0).*(x2-x1)) * y2); y1 = -10; plot(x,0,x0,y0,'.',x1,y1,'.',x2,y2,'.',x,(x-x1).*(x-x2)./((x0-x1).*(x0-x2))*y0 + (x-x0).*(x-x2)./((x1-x0).*(x1-x2))*y1 + (x-x0).*(x-x1)./((x2-x0).*(x2-x1)) * y2); % Now let's make a interpolating polynomial that ends up being a line % as the three points will be collinear y1 = (y0+y2)/2; plot(x,0,x0,y0,'.',x1,y1,'.',x2,y2,'.',x,(x-x1).*(x-x2)./((x0-x1).*(x0-x2))*y0 + (x-x0).*(x-x2)./((x1-x0).*(x1-x2))*y1 + (x-x0).*(x-x1)./((x2-x0).*(x2-x1)) * y2); % You can even get a constant polynomial as a result y0 = 2; y1 = 2; y2 = 2; plot(x,0,x0,y0,'.',x1,y1,'.',x2,y2,'.',x,(x-x1).*(x-x2)./((x0-x1).*(x0-x2))*y0 + (x-x0).*(x-x2)./((x1-x0).*(x1-x2))*y1 + (x-x0).*(x-x1)./((x2-x0).*(x2-x1)) * y2);