Está en la página 1de 3

f u n c t i o n I=doblesm ( f , c , d , a , b , n , m)

%e l orden de l a s d i f e r e n c i a l e s de l a i n t e g r a l e e s de l a forma dydx


%i n g r e s e m o s a n t e s e s t a s v a r i a b l e s en e l workspace
% en l o s l i m i t e s de c y d deve i n g r e s a r s e a s i : c=@( x ) x ; i g u a l para d .
%f e s una f u n c i o n i n t e g r a b l e deve s e r e s c r i t o de e s t a forma : e j e m p l o
%f=@( x , y ) ( x+y ) ∗ c o s ( x.^2+y ) ; para n=m=8
%c e s l i m i t e i n f e r i o r de y , puede s e r una f u n c i o n de x : @( x ) 0 ;
%d e s l i m i t e s u p e r i o r de y , puede s e r f u n c i o n de x t : @( x ) 1 ;
%a e s l i m i t e i n f e r i o r de x , deve s e r una c o n s t a n t e ejem a=0
%b e s l i m i t e s u p e r i o r de x , deve s e r una c o n s t a n t e ejem a=1
%n e s numero de p a r t i c i o n e s o i n t e r v a l o s de x , deve s e r par
%m e s numero de p a r t i c i o n e s o i n t e r v a l o s de y , deve s e r par
i f rem ( n ,2)~=0
e r r o r ( ’ i n g r e s e un v a l o r par para e l numero de p a r t i c i o n e s ’ )
end
i f rem (m,2)~=0
e r r o r ( ’ i n g r e s e un v a l o r par para e l numero de p a r t i c i o n e s ’ )
end
h=(b−a ) / n ;
J1 =0;
J2 =0;
J3 =0;
f o r i =0:n
x=a+i ∗h ;
HX=(d ( x)−c ( x ) ) / n ;
K1=f ( x , c ( x))+ f ( x , d ( x ) ) ;
K2=0;
K3=0;
f o r j =1:m−1
Y=c ( x)+ j ∗HX;
Q=f ( x ,Y ) ;
i f rem ( j ,2)==0
K2=K2+Q;
else
K3=K3+Q;
end
end
L=(K1+2∗K2+4∗K3) ∗HX/ 3 ;
i f i==0 | | i==n
J1=J1+L ;
e l s e i f rem ( i ,2)==0
J2=J2+L ;
else
J3=J3+L ;
end
end

1
end
d i s p ( ’ l a i n t e g r a l d o b l e por e l metodo de simpson e s : ’ )
J=h / 3 ∗ ( J1+2∗J2+4∗J3 )
end

%d e r i v a d a h a c i a a d e l a n t e
g=i n p u t ( ’ i n g r e s e tu f u n c i o n e . g a s i @( x ) c o s ( x ) : ’ , ’ s ’ ) ;
f=s t r 2 f u n c ( g ) ;
h=i n p u t ( ’ i n g r e s e l a amplitud a t r a b a j a r : ’ ) ; %cuando e l v a l o r e s mas p e q u e o s
d=i n p u t ( ’ e l orden de l a d e r i v a d a a c a l c u l a r : ’ ) ; %orden 1 , 2 o 3
x=i n p u t ( ’ un punto donde q u i e r e s aproximar l a d e r i v a d a : ’ ) ;
i f d==1
Fd3=(4∗ f ( x+h)− f ( x+2∗h)−3∗ f ( x ) ) / ( 2 ∗ h ) ;
d i s p ( ’ p r i m e r a d e r i v a d a es ’ )
f p r i n t f ( ’ adelante : %.10 f \n ’ , Fd3 )
e l s e i f d==2
Fd3=( f ( x+2∗h)−2∗ f ( x+h)+ f ( x ) ) / ( h ^ 2 ) ;
d i s p ( ’ segunda d e r i v a d a es ’ )
f p r i n t f ( ’ a d e l a n t e : %.10 f \n ’ , Fd3 )
e l s e i f d==3
Fd3=( f ( x+3∗h)−3∗ f ( x+2∗h)+3∗ f ( x+h)− f ( x ) ) / ( h ^ 3 ) ;
d i s p ( ’ t e r c e r a d e r i v a d a es ’ )
f p r i n t f ( ’ a d e l a n t e : %.10 f \n ’ , Fd3 )
else
d i s p ( ’ ∗ ∗ f o r m u l a no d i s p o n i b l e ∗ ∗ ’ )
end

% Romberg
f = i n p u t ( ’ i n g r e s e tu f u n c i o n e . g a s i @( x ) c o s ( x ) : ’ , ’ s ’ ) ;
a = input ( ’ i n g r e s a r l i m i t e i n f e r i o r , a : ’);
b = input ( ’ i n g r e s a r l i m i t e superior , b : ’);
n = i n p u t ( ’ i n g r e s a r e l n m e r o de s u b i n t e r v a l o s , n : ’);

h = b−a ;
r = z e r o s ( 2 , n +1);
r ( 1 , 1 ) = h ∗ ( f ( a)+ f ( b ) ) / 2 ;
f p r i n t f ( ’ \ nRomberg i n t e g r a t i o n t a b l e : \ n ’ ) ;
f p r i n t f ( ’ \ n %11.8 f \n\n ’ , r ( 1 , 1 ) ) ;
for i = 2:n
sum = 0 ;
f o r k = 1 : 2 ^ ( i −2)
sum = sum+f ( a+(k −0.5)∗ h ) ;

2
end
r ( 2 , 1 ) = ( r (1 ,1)+ h∗sum ) / 2 ;

for j = 2: i
l = 2^(2∗( j −1));
r ( 2 , j ) = r ( 2 , j −1)+( r ( 2 , j −1)−r ( 1 , j −1))/( l −1);
end

for k = 1: i
f p r i n t f ( ’ %11.8 f ’ , r ( 2 , k ) ) ;
end

f p r i n t f ( ’ \ n\n ’ ) ;
h = h/2;
for j = 1: i
r (1 , j ) = r (2 , j ) ;
end
end

%d e r i v a d a h a c i a a t r a s
g=i n p u t ( ’ i n g r e s e tu f u n c i o n e . g a s i @( x ) c o s ( x ) : ’ , ’ s ’ ) ;
f=s t r 2 f u n c ( g ) ;
h=i n p u t ( ’ i n g r e s e h : ’ ) ; %cuando e l v a l o r e s mas p e q u e o s e t i e n e mejor r e s u l t a
d=i n p u t ( ’ numero de d e r i v a d a s a c a l c u l a r : ’ ) ;
x=i n p u t ( ’ un punto donde q u i e r e s aproximar l a d e r i v a d a : ’ ) ;
i f d==1
Bd3=(−4∗ f ( x−h)+ f ( x−2∗h)+3∗ f ( x ) ) / ( 2 ∗ h ) ;
d i s p ( ’ p r i m e r a d e r i v a d a es ’ )
f p r i n t f ( ’ a t r a s : %.10 f \n ’ , Bd3 )
e l s e i f d==2
Bd3=( f ( x−2∗h)−2∗ f ( x−h)+ f ( x ) ) / ( h ^ 2 ) ;
d i s p ( ’ segunda d e r i v a d a es ’ )
f p r i n t f ( ’ a t r a s : %.10 f \n ’ , Bd3 )
e l s e i f d==3
Bd3=(− f ( x−3∗h)−3∗ f ( x−h)+3∗ f ( x−2∗h)+ f ( x ) ) / ( h ^ 3 ) ;
d i s p ( ’ t e r c e r a d e r i v a d a es ’ )
f p r i n t f ( ’ a t r a s : %.10 f \n ’ , Bd3 )
else
d i s p ( ’ ∗ ∗ f o r m u l a no d i s p o n i b l e ∗ ∗ ’ )
end

También podría gustarte