Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Lagrange Interpolation

Subject: Lagrange Interpolation

From: Akmal

Date: 7 Mar, 2012 03:08:11

Message: 1 of 3

When I execute this code it says that there is an invalid statement. I don't know what went wrong.
function [yi] = LagrangeInter(x,y,xi)
% Lagrange interpolation algorithm
% x,y - row-vectors of (n+1) data values (x,y)
% xi - a row-vector of values of x, where the polynomial y = Pn(x) is evaluated
% yi - a row-vector of values of y, evaluated with y = Pn(x)
 
n = length(x) - 1; % order of interpolation polynomial y = Pn(x)
ni = length(xi); % number of points where the interpolation is to be evaluated
L = ones(ni,n+1); % the matrix for Lagrange interpolating polynomials L_(n,j)(x)
                  % L has (n+1) columns for each point j = 1,2,...,n+1
                  % L has ni rows for each point of xi
for j = 1 : (n+1)
    for i = 1 : (n+1)
          if (i ~= j)
               L(:,j) = L(:,j).*(xi' - x(i))/(x(j)-x(i));
          end
     end
end
 
yi = y*L';

x = [ -1,-0.75,-0.5,-0.25,-0];
y = [ -14.58,-6.15,-1.82,-0.23,-0.00];
xInt = -1 : 0.01 : 0;
yInt = LagrangeInter(x,y,xInt);
plot(xInt,yInt,'g',x,y,'b*');

Subject: Lagrange Interpolation

From: Roger Stafford

Date: 8 Mar, 2012 06:32:25

Message: 2 of 3

"Akmal " <virtuoso_kraken@yahoo.com> wrote in message <jj6jer$m1r$1@newscl01ah.mathworks.com>...
> When I execute this code it says that there is an invalid statement. I don't know what went wrong.
> function [yi] = LagrangeInter(x,y,xi)
> % Lagrange interpolation algorithm
> % x,y - row-vectors of (n+1) data values (x,y)
> % xi - a row-vector of values of x, where the polynomial y = Pn(x) is evaluated
> % yi - a row-vector of values of y, evaluated with y = Pn(x)
>
> n = length(x) - 1; % order of interpolation polynomial y = Pn(x)
> ni = length(xi); % number of points where the interpolation is to be evaluated
> L = ones(ni,n+1); % the matrix for Lagrange interpolating polynomials L_(n,j)(x)
> % L has (n+1) columns for each point j = 1,2,...,n+1
> % L has ni rows for each point of xi
> for j = 1 : (n+1)
> for i = 1 : (n+1)
> if (i ~= j)
> L(:,j) = L(:,j).*(xi' - x(i))/(x(j)-x(i));
> end
> end
> end
> yi = y*L';
>
> x = [ -1,-0.75,-0.5,-0.25,-0];
> y = [ -14.58,-6.15,-1.82,-0.23,-0.00];
> xInt = -1 : 0.01 : 0;
> yInt = LagrangeInter(x,y,xInt);
> plot(xInt,yInt,'g',x,y,'b*');
- - - - - - - - - - -
  Akmal, I have not found any serious faults with your code that would produce a message that "there is an invalid statement". Also with your x, y, xInt trials it ran just fine on my system and the green curve ran nicely through all the blue points.

  I would suggest you first look for other m-files named "LagrangeInter". There might be one or more lurking around in your system left over as you developed the code and it or they could be executing instead of your latest version and causing the mischief.

  If that isn't it, you should figure out just which of your code lines is producing the error message and concentrate on that line of code to see why it seems to be misbehaving.

  If necessary you should try executing your code one line at a time with very short vectors as input trying to discover where the fault is. Check the 'L' matrix right after you exit from the for-loops to see if it is still the right size and has the correct values.

  If you pursue it in this step-by-step fashion I am sure you will eventually discover what is wrong. Good luck!

  PS - Also check that you have no variables named 'ones', 'length', or 'plot'.

Roger Stafford

Subject: Lagrange Interpolation

From: Akmal

Date: 8 Mar, 2012 15:48:17

Message: 3 of 3

Thank you for your reply. You were right. There were two files with the same name. I didn't realize it. Silly me. Thank you again.


"Roger Stafford" wrote in message <jj9jpp$nc1$1@newscl01ah.mathworks.com>...
> "Akmal " <virtuoso_kraken@yahoo.com> wrote in message <jj6jer$m1r$1@newscl01ah.mathworks.com>...
> > When I execute this code it says that there is an invalid statement. I don't know what went wrong.
> > function [yi] = LagrangeInter(x,y,xi)
> > % Lagrange interpolation algorithm
> > % x,y - row-vectors of (n+1) data values (x,y)
> > % xi - a row-vector of values of x, where the polynomial y = Pn(x) is evaluated
> > % yi - a row-vector of values of y, evaluated with y = Pn(x)
> >
> > n = length(x) - 1; % order of interpolation polynomial y = Pn(x)
> > ni = length(xi); % number of points where the interpolation is to be evaluated
> > L = ones(ni,n+1); % the matrix for Lagrange interpolating polynomials L_(n,j)(x)
> > % L has (n+1) columns for each point j = 1,2,...,n+1
> > % L has ni rows for each point of xi
> > for j = 1 : (n+1)
> > for i = 1 : (n+1)
> > if (i ~= j)
> > L(:,j) = L(:,j).*(xi' - x(i))/(x(j)-x(i));
> > end
> > end
> > end
> > yi = y*L';
> >
> > x = [ -1,-0.75,-0.5,-0.25,-0];
> > y = [ -14.58,-6.15,-1.82,-0.23,-0.00];
> > xInt = -1 : 0.01 : 0;
> > yInt = LagrangeInter(x,y,xInt);
> > plot(xInt,yInt,'g',x,y,'b*');
> - - - - - - - - - - -
> Akmal, I have not found any serious faults with your code that would produce a message that "there is an invalid statement". Also with your x, y, xInt trials it ran just fine on my system and the green curve ran nicely through all the blue points.
>
> I would suggest you first look for other m-files named "LagrangeInter". There might be one or more lurking around in your system left over as you developed the code and it or they could be executing instead of your latest version and causing the mischief.
>
> If that isn't it, you should figure out just which of your code lines is producing the error message and concentrate on that line of code to see why it seems to be misbehaving.
>
> If necessary you should try executing your code one line at a time with very short vectors as input trying to discover where the fault is. Check the 'L' matrix right after you exit from the for-loops to see if it is still the right size and has the correct values.
>
> If you pursue it in this step-by-step fashion I am sure you will eventually discover what is wrong. Good luck!
>
> PS - Also check that you have no variables named 'ones', 'length', or 'plot'.
>
> Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us