0% found this document useful (0 votes)
19K views

Programa Function de Lagrange

The lagrange function calculates the Lagrange polynomial interpolation of a set of x and f data points. It takes the x and f arrays as inputs and returns the coefficients c of the Lagrange polynomial. The document provides examples of using the lagrange function to calculate polynomial coefficients from different datasets and evaluating the polynomial at given x values.

Uploaded by

bleizher
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19K views

Programa Function de Lagrange

The lagrange function calculates the Lagrange polynomial interpolation of a set of x and f data points. It takes the x and f arrays as inputs and returns the coefficients c of the Lagrange polynomial. The document provides examples of using the lagrange function to calculate polynomial coefficients from different datasets and evaluating the polynomial at given x values.

Uploaded by

bleizher
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

programa Function de lagrange

function [c]=lagrange(x,f)
n1=length(x);
n=n1-1;
l=zeros(n1,n1);
for k=1:n+1,
v=1;
for j=1:n+1,
if k~=j,
v=conv(v,poly(x(j)))/(x(k)-x(j));
end
end
l(k,:)=v;
end
c=f*l;
******************************

>> x=[0.1 0.6 0.9 1.2];


f=[0.9950 0.8253 0.6216 0.3624];
[c]=lagrange(x,f)
c =
0.1056

-0.5935

0.0306

0.9978

0.0306

0.9978

f1=polyval(c,2)=-0.4700
c =
0.1056

-0.5935

c =
|
Error: Expression or statement is incomplete or incorrect.
>> x=[0.1 0.6 0.9 1.2];
>> f=[0.9950 0.8253 0.6216 0.3624];
>> [c]=lagrange(x,f)
c =
0.1056

-0.5935

>> f1=polyval(c,2)
f1 =
-0.4700
>> f1=polyval(c,pi)

0.0306

0.9978

f1 =
-1.4889
>> g1=cos(2)
g1 =
-0.4161
>> g2=cos(pi)
g2 =
-1
>> abs(f1-g1)
ans =
1.0728
>> abs(f2-g2)
Undefined function or variable 'f2'.
>>
>> x=[1.1 1.2 1.3 1.4];
>> f=[9.025013 11.02318 13.46374 16.44465];
>> [c]=lagrange(x,f)
c =
16.3262

-36.6546

39.4723

-11.7726

>> x=-4:0.05:2;
>> f=16.3262*x.^3-36.6546*x.^2+39.4723*x-11.7726;
>> plot(x,f)
>> grid
>>
>> x=[1.1 1.2 1.3 1.4];
>> f=[9.025013 11.02318 13.46374 16.44465];
>> function [c,d]=difdividida(x,y)
n=length(x);
d=zeros(n,n);
d(:,1)=y';
for j=2:n

for k=j:n
d(k,j)=(d(k,j-1)-d(k-1,j-1))/(x(k)-x(k-j+1));
end
end
c=d(n,n);
for k=(n-1):-1:1
c=conv(c,poly(x(k)));
m=length(c);
c(m)=c(m)+d(k,k);
end
function [c,d]=difdividida(x,y)
|
Error: Function definitions are not permitted in this
context.
>> [c,d]=difdividida(x,y)
Undefined function or variable 'y'.
>> x=[1.1 1.2 1.3 1.4];
>> y=[9.025013 11.02318 13.46374 16.44465];
>> [c,d]=difdividida(x,y)
c =
16.3262

-36.6546

39.4723

-11.7726

0
19.9817
24.4056
29.8091

0
0
22.1196
27.0175

0
0
0
16.3262

d =
9.0250
11.0232
13.4637
16.4446

>> y1=polyval(c,1.255)
y1 =
12.3045
>> y2=polyval(c,2.5)
y2 =
112.9135
>> x=0:0.5:3;

>> y3=polival(c,x);
Undefined function 'polival' for input arguments of type
'double'.
>> y3=polyval(c,x);
>> plot(x,y3)
>> x=[0 pi/6 pi/4 pi/2 pi 3pi/4 ];
x=[0 pi/6 pi/4 pi/2 pi 3pi/4 ];
|
Error: Unexpected MATLAB expression.
>> x=[0 pi/6 pi/4 pi/2 pi 3*pi/4 ];
>> y=x*sin(x);
Error using *
Inner matrix dimensions must agree.
>> y=xsin(x);
Undefined function 'xsin' for input arguments of type
'double'.
Did you mean:
>> y=[o

You might also like