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 Method

Subject: Lagrange Method

From: Akmal

Date: 4 Mar, 2012 13:07:15

Message: 1 of 2

I'm writing a code for polynomial interpolation by Lagrange method. But I can't execute my program. I don't know where I went wrong. Any input would be much appreciated.


% Setting up the Lagrangian polynomial

z=sym('z');
d = input('enter f(x)= \n')
f=inline(d);
x0=input('inter x0 = \n')
x=x0
L0=(z-xdata(2))/(xdata(1)-xdata(2));
L1=(z-xdata(1))/(xdata(2)-xdata(1));
fl=L0*ydata(1)+L1*ydata(2);
fxdesired=subs(fl,z,xdesired);
fprev=fxdesired;

datapoints=3;
p=1;
for i=1:n
    if d(i) <= datapoints
        xdata(p)=x(i);
        ydata(p)=y(i);
        p=p+1;
    end
end

% Calculating coefficients of Newton's Divided difference polynomial
% Setting up the Lagrangian polynomial
z=sym('z');
L0=((z-xdata(2))*(z-xdata(3)))/((xdata(1)-xdata(2))*(xdata(1)-xdata(3)));
L1=((z-xdata(1))*(z-xdata(3)))/((xdata(2)-xdata(1))*(xdata(2)-xdata(3)));
L2=((z-xdata(1))*(z-xdata(2)))/((xdata(3)-xdata(1))*(xdata(3)-xdata(2)));
fq=L0*ydata(1)+L1*ydata(2)+L2*ydata(3);
fxdesired=subs(fq,z,xdesired);

fnew=fxdesired;
ea=abs((fnew-fprev)/fnew*100);
if ea >= 5
    sd=0;
else
    sd=floor(2-log10(abs(ea)/0.5));
end

Subject: Lagrange Method

From: Matt J

Date: 4 Mar, 2012 19:05:14

Message: 2 of 2

"Akmal " <virtuoso_kraken@yahoo.com> wrote in message <jivpe3$a2l$1@newscl01ah.mathworks.com>...
> I'm writing a code for polynomial interpolation by Lagrange method. But I can't execute my program. I don't know where I went wrong. Any input would be much appreciated.
==============

Try the DBSTOP command

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