LAB - Lagrrange Interpolation
LAB - Lagrrange Interpolation
If you have a set of N points on a cartesian plane, there will always exist an N-1th order polynomial of the
form y = a_0 + a_1.x + a_2.x^2 + ... a_n-1.x^(n-1) which passes through all the points. Lagrange came up
with a neat approach to finding this polynomial, which is to construct a set of `basis' polynomials which
are zero at all the specified points except for one, then scale and add them to match all the control points.
LAGRANGEPOLY returns this polynomial, defined by the polynomial coefficients (a_0 .. a_n-1 above),
arranged in the same vector form used by Matlab builtins such as POLYVAL.
LAGRANGEPOLY optionally returns the x and y co-ordinates of all the extrema and points of inflection of
the polynomial too, since these are easily found from the polynomial form.
Lagrange interpolation has generally been replaced by spline fitting these days; see the SPLINE function.
Example
Find the polynomial going through 8 evenly-spaced points, then plot the curve, along with the
control points (red circles, to check the curve does indeed go through them) and the extrema
(blue dots).
Also, compare to SPLINE (dotted green line), which also passes through the points without such
extreme deviation (but is not in the form of a single polynomial).
X = [1 2 3 4 5 6 7 8];
Y = [0 1 0 1 0 1 0 1];
[P,R,S] = lagrangepoly(X,Y);
plot(xx,polyval(P,xx),X,Y,'or',R,S,'.b',xx,spline(X,Y,xx),'--g')
grid
axis([0.5 8.5 -5 5])
Lagrange Interpolation, .m code
function y=lagrange(x,pointx,pointy)
%
%LAGRANGE approx a point-defined function using the Lagrange polynomial
interpolation
%
% LAGRANGE(X,POINTX,POINTY) approx the function definited by the points:
% P1=(POINTX(1),POINTY(1)), P2=(POINTX(2),POINTY(2)), ...,
PN(POINTX(N),POINTY(N))
% and calculate it in each elements of X
%
% If POINTX and POINTY have different number of elements the function will
return the NaN value
%
%
n=size(pointx,2);
L=ones(n,size(x,2));
if (size(pointx,2)~=size(pointy,2))
fprintf(1,'\nERROR!\nPOINTX and POINTY must have the same number of
elements\n');
y=NaN;
else
for i=1:n
for j=1:n
if (i~=j)
L(i,:)=L(i,:).*(x-pointx(j))/(pointx(i)-pointx(j));
end
end
end
y=0;
for i=1:n
y=y+pointy(i)*L(i,:);
end
end
x = 0:10;
y = x.^2;
xx = linspace(0,10);
yy = lagrange(xx,x,y);
plot(x,y,'o',xx,yy,'.')
% pointx and pointy are two vectors of data values, x is a vector of points where you want to
interpolate.
Newton’s interpolation , Newton’s interpolation equally spaced nodes, .m code
n
Linear programming
Solve a simple linear program with linear inequalities, linear equalities, and bounds.
For this example, use these linear inequality constraints:
Example, use these linear inequality constraints:
A = [1 1
1 1/4
1 -1
-1/4 -1
-1 -1
-1 1];
b = [2 1 2 1 -1 2];
Use the linear equality constraint
.
Aeq = [1 1/4];
beq = 1/2;
Set these bounds:
lb = [-1,-0.5];
ub = [1.5,1.25];
Use the objective function
.
f = [-1 -1/3];
Solve the linear program.
x = linprog(f,A,b,Aeq,beq,lb,ub)
Optimal solution found.
x = 2×1
0.1875
1.2500