Está en la página 1de 3

function A=trapecio(f,a,b,n)

h=(b-a)/n;
s=0;
for k=1:n-1
s=s+f(a+k*h ) ;
end
A=(f(a)+f(b)+2*s)*h/2 ;

>> f=@(x) sin(x+2);


>> trapecio(f,0,4,8)
ans =-1.3475
Mtodo de la cuadratura
>> f=@(x) sin(x+2);
>> quad(f,0,4)
ans =-1.3763
function A=romberg(f,a,b,error)
r(1,1)=trapecio(f,a,b,1);
r(2,1)=trapecio(f,a,b,2);
r( 2,2) = (4*r(2,1)-r(1,1))/3;
k=2;
while abs(r(k,k)-r(k,k-1))>=error
k=k+1;
r(k,1) =trapecio(f,a,b,2^( k-1)) ;
for j=2:k
r (k,j) = (4^( j-1)* r (k,j-1)-r (k-1,j-1 ))/(4^( j-1)-1);
end
end
A=r;

>> f=@(x) log(x);


>> romberg(f,1,2,0.00001)
ans =
0.3466
0
0
0
0.3760 0.3858
0
0
0.3837 0.3863 0.3863
0
0.3856 0.3863 0.3863 0.3863
>> f=@(x) log(x);
>> quad(f,1,2)
ans =0.3863
>> f=@(x) log(1+x.^2);
>> romberg(f,-1,1,0.00001)
ans =
1.3863
0
0
0
0
0.6931 0.4621
0
0
0
0.5697 0.5286 0.5330
0
0
0.5383 0.5278 0.5278 0.5277
0
0.5305 0.5279 0.5279 0.5279 0.5279
>> f=@(x) log(1+x.^2);
>> quad(f,-1,1)
ans =
0.5279

function y=ejer1(x)
xx=[0 1 2 3 4];
yy=[0 7 2 5 0];
y=interp1(xx,yy,x,'spline');
>> f=@(x) ejer1(x);
>> romberg(f,0,4,0.0001)
ans =
0
0
0
0
0
4.0000 5.3333
0
0
0
14.0000 17.3333 18.1333
0
0
16.5000 17.3333 17.3333 17.3206
0
17.1250 17.3333 17.3333 17.3333 17.3334
>> f=@(x) ejer1(x);
>> quad(f,0,4)
ans =17.3333