Program
function[]=lang(X,Y,x0)
n=length(X)
p=0
x=poly(0,'x')
for i=1:n
l=1
for j=[1:i-1,i+1:n]
l=l*(x-X(j))/(X(i)-X(j))
end
p=p+l*Y(i)
end
disp(p,'langrages poly=')
printf('value of poly @ %.4f=%.4f',x0,horner(p,x0))
endfunction
Output:
X=[-1 1 4 6]
X =
-1. 1. 4. 6.
Y=[-4 8 -41 78]
Y =
-4. 8. -41. 78.
lang(X,Y,2)
langrages poly=
2 3
17.685714 +3.1952381x -15.685714x +2.8047619x
value of poly @ 2.0000=-16.2286
function[]=lang(X,Y,x0)
n=length(X)
p=0
x=poly(0,'x')
for i=1:n
l=1
for j=[1:i-1,i+1:n]
l=l*(x-X(j))/(X(i)-X(j))
end
p=p+l*Y(i)
end
disp(p,'langrages poly=')
printf('value of poly @ %.4f=%.4f',x0,horner(p,x0))
endfunction
Output:
X=[-1 1 4 6]
X =
-1. 1. 4. 6.
Y=[-4 8 -41 78]
Y =
-4. 8. -41. 78.
lang(X,Y,2)
langrages poly=
2 3
17.685714 +3.1952381x -15.685714x +2.8047619x
value of poly @ 2.0000=-16.2286
0 Comments