Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Millares Valenzuela
Instituto Interuniversitario del Sistema Tierra en Andaluca
Universidad de Granada
Curso de Introduccion a
MATLABTM
Breve seleccion de instrucciones
February 15, 2015
Contents
1
1
1
3
9
17
18
19
19
20
21
21
List of Figures
1.1
1.2
1.3
1.4
1.5
vii
Chapter 1
1.2 Funciones
MatlabTM incluye una pletora de funciones como, por ejemplo, sqrt(x), log(x),
sin(x), bessel j(n, x), exp(x), que calculan, respectivamente, la raz cuadrada, el log1
d i s p ( ESTIMACION
)
gE = 0 . 5 7 7 ;
% gamma de E u l e r
N = i n p u t ( I n t r o d u z c a e l n u mero de o l a s : ) ;
H13 = i n p u t ( I n t r o d u z c a l a a l t u r a de o l a s i g n i f i c a n t e (m ) : ) ;
HmaxN = H13 ( s q r t ( 0 . 5 l o g (N) ) + gE / s q r t (8 l o g (N ) ) ) ;
f p r i n t f ( La a l t u r a m a xima e s t i m a d a e s %f m\n , HmaxN )
abc
A33 = d e f ,
gh j
(1.1)
(1.2)
(1.3)
En general una matriz Dnm define una matriz de n filas por m columnas. En
MatlabTM los vectores se definen con corchetes de la siguiente forma1 :
A = [a, b, c
(1.4)
d, e, f
(1.5)
g, h, j] .
(1.6)
esas operaciones. As, por ejemplo, si A = [16.3 9585], max(A) produce como
salida 6.30, min(A) -958 y mean(A) -236.4250. Si existe algun elemento marcado
como NaN puede usarse la funcion nanmean() que realiza la media de todos los
elementos excepto los NaN. Estas funciones son aplicables a matrices, no solo a
vectores, y se les puede especificar la dimension sobre la que se realiza la operacion.
Las funciones max() y min() devuelven opcionalmente los ndices de los elementos
maximo(s) y mnimo(s), respectivamente.
Para definir de forma automatica vectores hay disponibles varias funciones de
MatlabTM .
linspace: Con a=linspace(x1, x2, N) se define un vector a de N elementos equiespaciados linealmente entre los valores x1 y x2.
zeros: Con a=zeros(N, M) se define una matriz de N filas por M columnas cuyos
elementos son todos ceros.
ones: Con a=ones(N, M) es igual que con zeros(), pero rellenando la matriz con
unos.
eye: a=eye(N) define una matriz identidad de N N.
rand: a=rand(N,M) define una matriz N M de numero aleatorios uniformemente distribuidos en el intervalo (0, 1).
Una forma rapida de definir matrices y que es similar (pero no igual, ojo con los
redondeos) a la funcion linspace() es haciendo uso del operador rango : (dos puntos).
Por ejemplo, A = 50 : 100 define un vector de 51 elementos, cuyos elementos van
de 50 a 100 a saltos de 1. Por defecto usa un salto de 1. Si queremos especificar
otro valor debemos especificarlo de la siguiente manera: A = 50 : 2 : 100, define un
vector de 26 elementos cuyos elementos son 50, 52, . . . 98, 100. Del mismo modo,
A = 0 : 0.1 : 100 es un vector de 1001 elementos entre 0 y 100 cuyo espaciado entre
ellos es 0.1.
Una vez definida la matriz o el vector es posible acceder a sus elementos. Por
ejemplo, para la matriz D siguiente
D = [1, 1, 0.1
(1.7)
3, 0, 0.65
(1.8)
9, 12, 1.54] ,
(1.9)
Igual que con numeros, pueden realizarse operaciones aritmeticas con matrices.
Para sumar y restar matrices se emplean los operadores + y , respectivamente2 .
Para que la suma/resta este bien definida (matematicamente y en MatlabTM ) las matrices tienen que tener identico numero de filas y columnas, de otro modo MatlabTM
dara un mensaje de error. Por ejemplo, no podremos sumar la matriz D33 a la matriz al vector
a13 = [3 60.02] ,
(1.10)
(1.11)
1, 1, 0
(1.12)
2, 2, 0] ,
(1.13)
siendo el resultado
[2, 2, 1.1
(1.14)
2, 1, 0.65
(1.15)
(1.16)
(1.17)
2, 1, 0.75
(1.18)
8, 2, 1.1] .
(1.19)
[1.8, 2.2, 1
(1.20)
4.3, 1.7, 3
(1.21)
(1.22)
(1.23)
3, 0, 0
(1.24)
18, 24, 0] .
(1.25)
Del mismo modo se hara con el operador exponente, si queremos elevar a una
potencia los elementos de una matriz. Por ejemplo, si R = T.2
R = [1, 1, 0.01
(1.26)
9, 0, 0
(1.27)
324, 576, 0] .
(1.28)
Notese que, por definicion, la suma/resta de matrices ya se realiza elemento a elemento, as que
no es necesario poner un punto delante de la operacion suma/resta.
(1.29)
3, 3, 0.3
(1.30)
(1.31)
Example 1.2. Este ejemplo ha sido tomado de [Gerald, C.F. y P.O. Wheatley (2000),
Analisis Numerico con Aplicaciones] y se corresponde con el problema de determinacion del mapa de temperaturas de equilibrio en una placa metalica bidimensional
de dimensiones 2 m 2 m. Los contornos, tal y como se indica en la Fig. 1.1, se
hallan cada uno a una determinada temperatura mantenida constante (imagnese en
contacto con un termostato a la temperatura indicada). Se pretende determinar las
temperaturas en el interior de la placa, discretizando su interior en 9 nodos, denominados uk , con k = 1 . . . 9 (Fig. 1.1). Las temperaturas en los nodos vienen dadas
por la ecuacion de difusion, y que en equilibrio se reduce a la ecuacion de Laplace.
Aplicada a este sistema y debidamente discretizada mediante diferencias finitas centradas da lugar al Sistema de ecuaciones lineales siguiente:
4u1 + u2
= 50
+ u4
u1 4u2 + u3
= 50
+ u5
u2 4u3
4u4 + u5
u1
+ u7
+ u4 4u5 + u6
u2
= 150
+ u6
=0
+ u8
+ u5 4u6
u3
u4
u5
u6
=0
+ u9 = 100
4u7 + u8
= 50
+ u7 4u8
= 50
(1.32)
+ u8 4u9 = 150
% V e r i f i c a m o s que l a m a t r i z A s e a nos i n g u l a r , i . e . d e t ( A ) = 0
i f ( abs ( d e t (A)) >1 e 7)
T=A\b ;
% o bien
% s o l = i n v ( A) b
end
% Para r e p r e s e n t a r e l r e s u l t a d o s como en l a p l a c a . . .
% ( i n c l u y e CCs )
sol = zeros (5 , 5 ) ;
s o l ( 1 , 2 : 4 ) = Tu ;
s o l ( end , 2 : 4 ) = Td ;
s o l ( 2 : 4 , 1) = Tl ;
s o l ( 2 : 4 , end ) = Tr ;
sol (2 , 2:4) = [T(1) , T(2) , T ( 3 ) ] ;
sol (3 , 2:4) = [T(4) , T(5) , T ( 6 ) ] ;
sol (4 , 2:4) = [T(7) , T(8) , T ( 9 ) ] ;
% Aunque s e v e r a en e l a p a r t a d o s i g u i e n t e con m a s d e t a l l e . . .
% u s a r e m o s l a f u n c i o n p c o l o r p a r a v i s u a l i z a r e l r e s u l t a d o
x = 2:2:2 length ( sol ( : , 1 ) ) ;
y = 2:2:2 length ( sol ( 1 , : ) ) ;
pcolor ( x , y , s o l ) ; shading i n t e r p
xlabel ( x )
ylabel ( y )
zlabel ( u (x , y ) )
t i t l e ( D i s t r i b u c i o n de t e m p e r a t u r a s u ( x , y ) )
colorbar
1.4 Visualizacion
Fig. 1.2 Solucion interpolada del sistema de ecuaciones lineales Eq. 1.32 segun las condiciones
de contorno dadas en el ejemplo (Fig. 1.2).
1.4 Visualizacion
MatlabTM integra no solo herramientas de calculo y analisis, sino tambien de visualizacion. Algunas de las funciones mas empleadas para visualizar resultados son
plot(), plotyy(), subplot(), surf() y otras. El usuario curioso puede aprender el uso de
estas funciones consultando la referencia de las mismas en la Ayuda de MatlabTM .
Example 1.3. Algunos ejemplos del uso de estas funciones pueden encontrarse en
las Figuras 1.3 y 1.4.
Listing 1.3 Ejemplos de funciones de visualizacion.
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % PROGRAMA: e j e m p l o 3
%
: E j e m p l o s de f u n c i o n e s de
% % DESCRIPCION
%
%%
v i s u a l i z a c i o n
%
10
% % USO: [ ] = e j e m p l o 3 ( )
%
% % ENTRADAS : Ninguna
%
% % SALIDAS : Ninguna
%
% % FECHA: 3 0 / 0 3 / 2 0 1 2
%
% % s e e a l s o L i n e S p e c , m e s h g r i d , s u r f , mesh , p c o l o r %
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ ] = ejemplo3 ( )
close all
clear all
clc
%
x
f
g
figure
plot (x , f , b )
h o l d on
plot (x , g , r )
xlabel ( x )
hold o f f
xlabel ( x )
ylabel ( f (x) y g(x) )
t i t l e ( E j e m p l o con p l o t )
legend ( f ( x )= s i n ( x ) , g ( x )= cos ( x ) s i n ( x 2 ) )
% ( 2 ) mismo p l o t con o p c i o n e s a v a n z a d a s
figure
p l o t ( x , f , L i n e S t y l e , , L i n e W i d t h , 2 , C o l o r , [ 0 0 0 ] ) ;
h o l d on
p l o t ( x , g , L i n e S t y l e , , L i n e W i d t h , 2 , C o l o r , [ 0 0 0 ] ) ;
xlabel ( x )
hold o f f
s e t ( gca , F o n t S i z e , 2 4 , FontName , A r i a l ) ;
xlabel ( x , FontSize ,30);
ylabel ( f ( x ) y g ( x ) , FontSize ,30);
t i t l e ( E j e m p l o con p l o t , F o n t S i z e , 2 4 ) ;
legend ( f ( x )= s i n ( x ) , g ( x )= cos ( x ) s i n ( x 2 ) , F o n t S i z e , 2 0 ) ;
% ( 3 ) s u b p l o t . Dos f u n c i o n e s en d o s p a n e l e s de misma f i g u r a
figure
t i t l e ( E j e m p l o con s u b p l o t )
subplot (121)
plot (x , f , b )
xlabel ( x )
ylabel ( f (x) )
legend ( f ( x )= s i n ( x ) )
subplot (122)
plot (x , g , r )
xlabel ( x )
y l a b e l ( g ( x )= cos ( x ) s i n ( x 2 ) )
legend ( g ( x )= cos ( x ) s i n ( x 2 ) )
1.4 Visualizacion
11
grid
xlabel ( x )
l e g e n d ( f ( x ) = s i n ( x ) , g ( x ) = 1 0 s i n ( 1 0 x ) e{x} )
% ( 5 ) p l o t 3 , p l o t en 3D ( p a r a m e t r i c a s ) .
figure
t = 0: pi /30:20 pi ;
p l o t 3 (10 s i n ( t ) . / t . 0 . 1 , c o s ( t ) . / t . 0 . 1 , t , m. )
t i t l e ( E j e m p l o con p l o t 3 )
g r i d on
axis square
xlabel ( sin ( t ) )
ylabel ( cos ( t ) )
zlabel ( t )
% (6) Surf
figure
[ x , y ] = meshgrid ( 4 : 0 . 1 : 4 , 4 : 0 . 1 : 4 ) ; % c r e a una m a l l a
f = besselj (0 ,( x .2+ y . 2 ) ) ;
subplot (321)
surf (x , y , f )
xlabel ( x )
ylabel ( y )
z l a b e l ( f ( x , y )= J 0 ( x 2+ y 2 ) )
subplot (322)
surf ( x , y , f ) ; shading i n t e r p
xlabel ( x )
ylabel ( y )
z l a b e l ( f ( x , y )= J 0 ( x 2+ y 2 ) )
subplot (323)
surfc (x , y , f )
xlabel ( x )
ylabel ( y )
z l a b e l ( f ( x , y )= J 0 ( x 2+ y 2 ) )
subplot (324)
mesh ( f ) % p i n t a l o s n d i c e s
xlabel ( x )
ylabel ( y )
z l a b e l ( f ( x , y )= J 0 ( x 2+ y 2 ) )
subplot (3 ,2 ,[5 6])
pcolor ( x , y , f ) ; shading i n t e r p ;
xlabel ( x )
ylabel ( y )
z l a b e l ( f ( x , y )= J 0 ( x 2+ y 2 ) )
colorbar
Example 1.4. Otros ejemplos de visualizacion de MatlabTM puede extraerse de funciones sencillas y conocidas en el a mbito de la Ingeniera Martima (vease Fig. 1.5).
Estos son:
1. Asignando valores apropiados a y , puede representarse la funcion de densidad de probabilidad (pdf) Gaussiana, que es p() =
e
2
(eta )2
22
2. Un caso tpico y recurrente en Teora Lineal es el uso de la relacion de dispersion ( 2 = gk tanh (kh)) para determinar la longitud de onda dada una frecuencia: Dados y h, puede determinarse k de forma sencilla graficamente o,
12
Fig. 1.3 Ejemplo de representacion con la funcion plot. Tal y como se indica, se estan representando las funciones f (x) = sin(x) y g(x) = cos(x) sin(x2 ).
Fig. 1.4 Ejemplo de representacion con la funcion surfc. En este caso se representa la funcion de
Bessel de primera especie de orden cero J0 con argumento x2 + y2 .
mediante el uso de la funcion fzeros de MatlabTM , numericamente. El lector curioso puede completar la grafica de la Fig. 1.5 dibujando las asntotas para kh
grande y pequeno.
3. Dada una altura de ola significante HS , puede representarse la funcion den2
2.005 H 2
HS
, y la funcion
2.005 H 2
HS
1e
.
4. Del mismo modo que en el apartado anterior, dado el periodo medio Tz , puede
representarse la funcion densidad de probabilidad y de distribucion de periodos
3
4
0.675 T
4
0.675 T
Tz
Tz
de Bretschneider: fB (T ) = 2.7 TT 4 e
y FB (T ) = 1 e
.
z
5. A partir de los dos items anteriores, la funcion de distribucion conjunta si T y H
estan descorrelacionadas sera f (H, T ) = fR (H) fB (T ).
1.4 Visualizacion
13
.
4 2 [1+(1+ 2 )1/2 ]
H 20 x
cos (t k0 x)
2e
y u(x,t) =
gh3
8 3 + 15 2 6 , donde =
9. Circulacion estuarina: u ( ) = 48A
x
z
z/h.
10. Sobrelevacion asociada a viento w = aCD w|w| en una plataforma continental
p
de profundidad constante h0 y longitud L: (x)
1 + 2 Lx 1. Considere =
h0 =
nwx L
,
(gh20 /2)
aCD 103 .
Listing 1.4 Uso de las funciones de visualizacion de MatlabTM para representar curvas y figuras.
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % PROGRAMA: e j e m p l o 4
%
: Visualiza funciones conocidas
% % DESCRIPCION
%
% % USO: [ ] = e j e m p l o 4 ( )
%
% % ENTRADAS : V a r i a s m e d i a n t e i n p u t
%
% % SALIDAS : Ninguna
%
% % FECHA: 3 0 / 0 3 / 2 0 1 2
%
% % s e e a l s o normpdf , x l i m , e c d f , f z e r o , a b s
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function []= ejemplo4
close all
clear all
clc
figure
% ( 1 ) F u n c i o n de d e n s i d a d de p r o b a b i l i d a d G a u s s i a n a
DENSIDAD GAUSSIANA )
d i s p ( FUNCION
mu = i n p u t ( I n t r o d u z c a e l v a l o r medio \mu en m: ) ;
s i g m a = i n p u t ( I n t r o d u z c a l a d e s v i a c i o n t p i c a \ s i g m a en m: ) ;
d e t a = i n p u t ( I n t r o d u z c a e l e s p a c i a d o \ D e l t a \ e t a en m: ) ;
e t a = [ mu5s i g m a : d e t a : mu+5 s i g m a ] ;
% m
p e t a = 1 / ( s i g m a s q r t (2 p i ) ) exp ( ( e t a mu ) . 2 / ( 2 s i g m a 2 ) ) ;
subplot (431)
plot ( eta , peta , k )
x l i m ( [ mu5s i g m a mu+5 s i g m a ] )
x l a b e l ( \ e t a (m) )
y l a b e l ( p (\ e t a ) )
t i t l e ( pdf Gaussiana )
% sum ( p e t a ) d e t a =1
14
h o l d on
p l o t ( e t a , normpdf ( e t a , mu , s i g m a ) , r o )
f p r i n t f ( F u n c i o n d e n s i d a d g a u s s i a n a d i b u j a d a . . . \ n\n )
% ( 2 ) R e l a c i o n de d i s p e r s i o n
DE DISPERSION
d i s p ( RELACION
)
T = i n p u t ( I n t r o d u z c a e l p e r i o d o de l a onda T en s : ) ;
w = 2 p i / T ;
% 1/ s
h = i n p u t ( I n t r o d u z c a l a p r o f u n d i d a d en m: ) ;
g = 9.82;
% m/ s 2
% R e l a c i o n de d i s p e r s i o n : w 2 = gk t a n h ( kh ) , o hw 2 / ( gkh )= t a n h ( kh ) .
x = [ 0 : 0 . 0 0 1 : 1 0 ] ; % x l o d e f i n o como kh
f 1 = hw 2 . / ( gx ) ;
f 2 = tanh ( x ) ;
subplot (432)
p l o t ( x , f1 , x , f 2 )
xlim ( [ 0 4 ] )
ylim ( [ 0 2 ] )
xlabel ( x )
l e g e n d ( h\omega 2 / ( gkh ) , t a n h ( kh ) )
t i t l e ( D e t e r m i n a c i o n g r a f i c a de k )
kh = f z e r o (@( x ) hw 2 / gxtanh ( x ) , hw 2 / g ) ;
k = kh / h ;
f p r i n t f ( E l v a l o r de k e s %f 1 /m\n , k )
lambda = 2 p i / k ;
f p r i n t f ( Y l a l o n g i t u d de onda c o r r e s p o n d i e n t e e s %f m\n , lambda )
% E l v a l o r e x a c t o p u e d e d e t e r m i n a r s e en s i m b o l i c o
f p r i n t f ( L o n g i t u d de onda d e t e r m i n a d a . . . \ n\n )
% ( 3 ) F u n c i o n de d e n s i d a d y de d i s t r i b u c i o n de R a y l e i g h
DE ALTURAS DE RAYLEIGH )
d i s p ( FUNCIONES DE DENSIDAD Y DISTRIBUCION
HS = i n p u t ( I n t r o d u z c a l a a l t u r a de o l a s i g n i f i c a n t e en m: ) ;
H = [0:0.05:5];
% m
HS = 1 . 5 ;
% m
fR = 4 . 0 1 H / ( HS 2 ) . exp ( 2 . 0 0 5 (H / HS ) . 2 ) ;
FR = 1exp ( 2 . 0 0 5 (H / HS ) . 2 ) ;
subplot (433)
p l o t (H, fR )
x l a b e l ( H (m) )
y l a b e l ( f R (H) )
t i t l e ( p d f de R a y l e i g h )
subplot (434)
p l o t (H, FR )
x l a b e l ( H (m) )
y l a b e l ( F R (H) )
t i t l e ( c d f de R a y l e i g h )
f p r i n t f ( F u n c i o n e s de R a y l e i g h r e p r e s e n t a d a s . . . \ n\n )
% ( 4 ) F u n c i o n de d e n s i d a d y d i s t r i b u c i o n de p e r i o d o s de B r e t s c h n e i d e r
DE PERIODOS DE BRETSCHNEIDER )
d i s p ( FUNCIONES DE DENSIDAD Y DISTRIBUCION
Tz = i n p u t ( I n t r o d u z c a e l p e r i o d o medio ( s ) : ) ;
T = [0:0.05:20];
% s
fB = 2 . 7 T . 3 / ( Tz 4 ) . exp ( 0 . 6 7 5 (T / Tz ) . 4 ) ;
FB = 1exp ( 0 . 6 7 5 (T / Tz ) . 4 ) ;
subplot (435)
p l o t ( T , fB )
x l a b e l ( T ( s ) )
ylabel ( f B (T) )
t i t l e ( p d f de B r e t s c h n e i d e r )
subplot (436)
p l o t ( T , FB )
1.4 Visualizacion
15
x l a b e l ( T ( s ) )
ylabel ( F B (T) )
t i t l e ( c d f de B r e t s c h n e i d e r )
f p r i n t f ( F u n c i o n e s de B r e t s c h n e i d e r r e p r e s e n t a d a s . . . \ n\n )
% ( 5 ) F u n c i o n de d e n s i d a d c o n j u n t a de H y T d e s c o r r e l a c i o n a d a s
CONJUNTAS )
d i s p ( FUNCIONES DE DENSIDAD Y DISTRIBUCION
f p r i n t f ( Usando l o s d a t o s a n t e r i o r e s . . . \ n\n )
fC = z e r o s ( l e n g t h ( fB ) , l e n g t h ( fR ) ) ;
f o r n = 1 : l e n g t h ( fR )
fC ( : , n ) = fR ( n ) fB ;
end
subplot (437)
% s u r f c (H, T , fC ) ; s h a d i n g i n t e r p
c o n t o u r (H, T , fC )
% s u r f (H, T , fC ) ; s h a d i n g i n t e r p
% p c o l o r (H, T , fC ) ; s h a d i n g i n t e r p
x l a b e l ( H (m) )
y l a b e l ( T ( s ) )
colorbar
t i t l e ( pdf conjunta d e s c o r r . )
f p r i n t f ( F u n c i o n de d i s t r i b u c i o n c o n j u n t a HT d e s c o r r e l a c i o n a d a r e p r e s e n t a d a . . . \ n\n )
% ( 6 ) F u n c i o n de d e n s i d a d c o n j u n t a H y T e s p e c t r a l
DE HT CONJUNTAS )
d i s p ( FUNCIONES DE DENSIDAD Y DISTRIBUCION
m0 = i n p u t ( I n t r o d u z c a l a a l t u r a de o l a s i g n i f i c a n t e e s p e c t r a l (m ) : ) ;
nu = i n p u t ( I n t r o d u z c a l a a n c h u r a e s p e c t r a l 0<nu <1: ) ;
Tz = i n p u t ( I n t r o d u z c a e l p e r i o d o medio ( s ) : ) ;
T = [0:0.05:20];
% s
H = [0:0.05:12];
% m
Ha = H / s q r t ( m0 ) ;
% adim
Ta = T / Tz ;
% adim
CL = 1 / ( 4 nu s q r t (2 p i ) ( 1 + ( 1 + nu 2 ) ( 0 . 5 ) ) ) ;
fHT = z e r o s ( l e n g t h ( Ta ) , l e n g t h ( Ha ) ) ;
f o r t = 1 : l e n g t h ( Ta )
f o r h = 1 : l e n g t h ( Ha )
fHT ( t , h ) = CL ( ( Ha ( h ) / Ta ( t ) ) 2 ) exp ( (Ha ( h ) 2 ) / 8 ( 1 + nu ( 2)(1 Ta ( t ) ( 1 ) ) 2 ) ) ;
end
end
subplot (438)
%p c o l o r (H, T , fHT ) ; s h a d i n g i n t e r p
% s u r f (H, T , fHT ) ; s h a d i n g i n t e r p
c o n t o u r (H, T , fHT )
x l a b e l ( H (m) )
y l a b e l ( T ( s ) )
colorbar
t i t l e ( cdf conjunta corr . )
f p r i n t f ( F u n c i o n de d i s t r i b u c i o n c o n j u n t a HT r e p r e s e n t a d a . . . \ n\n )
% ( 7 ) E l e v a c i o n en un e s t u a r i o c o n v e r g e n t e s i n f r i c c i o n
EN UN ESTUARIO CONVERGENTE SIN FRICCION
d i s p ( ELEVACION
)
L = i n p u t ( I n t r o d u z c a l a l o n g i t u d d e l e s t u a r i o (m ) : ) ; % p . e j . L = 100000
x =[0:L/1000:L ] ;
% m
Tom = i n p u t ( I n t r o d u z c a e l p e r i o d o de l a onda de marea ( s ) : ) ; % p . e j . T = 1 2 . 4 5 3 6 0 0
w = 2 p i / Tom ; % 1 / s
h = i n p u t ( I n t r o d u z c a l a p r o f u n d i d a d media (m ) : ) ; % p . e j . h = 10
a = i n p u t ( I n t r o d u z c a l a a m p l i t u d en l a d e s e m b o c a d u r a (m ) : ) ; % p . e j . a = 1 . 1
t 0 = i n p u t ( I n t r o d u z c a e l i n s t a n t e de s a l i d a ( s ) : ) ; % p . e j . t 0 = 13600
g = 9.82;
% m/ s2
b e t a = w / s q r t ( gh ) ;
e t a = b e s s e l j ( 0 , b e t a x ) / b e s s e l j ( 0 , b e t a L) c o s (w t 0 ) ;
16
subplot (439)
plot ( x /1000 , eta )
x l a b e l ( x ( km ) )
y l a b e l ( \ e t a (m) )
t i t l e ( E s t . conv . s i n f r i c c . )
f p r i n t f ( E l e v a c i o n en e s t u a r i o c o n v e r g e n t e s i n f r i c c i o n r e p r e s e n t a d a . . . \ n\n )
d i s p ( PROPAGACION
DEBIL
)
L = i n p u t ( I n t r o d u z c a l a l o n g i t u d d e l e s t u a r i o (m ) : ) ; % p . e j . L = 100000
x =[0:L/1000:L ] ;
A = i n p u t ( I n t r o d u z c a e l v a l o r d e l c o e f i c i e n t e de f r i c c i o n ( ) : ) ; % p . e j . A = 0 . 0 0 0 1
Tom = i n p u t ( I n t r o d u z c a e l p e r i o d o de l a onda de marea ( s ) : ) ; % p . e j . T = 1 2 . 4 5 3 6 0 0
w = 2 p i / Tom ; % 1 / s
a = i n p u t ( I n t r o d u z c a l a a m p l i t u d en l a d e s e m b o c a d u r a (m ) : ) ; % p . e j . a = 1 . 1
t 0 = i n p u t ( I n t r o d u z c a e l i n s t a n t e de s a l i d a ( s ) : ) ; % p . e j . t 0 = 13600
h = i n p u t ( I n t r o d u z c a l a p r o f u n d i d a d media (m ) : ) ; % p . e j . h = 10
kh = f z e r o (@( x ) hw 2 / gxtanh ( x ) , hw 2 / g ) ;
k0 = kh / h ;
g = 9.82;
% m/ s2
e t a = aexp ( 0.5Ak0x / w) . c o s (w t 0 k0x ) ;
u = e t a s q r t ( gh ) / h ;
subplot (4 ,3 ,10)
plot ( x /1000 , eta , x /1000 , u )
x l a b e l ( x ( km ) )
y l a b e l ( \ e t a (m) y u (m/ s ) )
t i t l e ( E l e v . y c o r r . con f r i c c . )
f p r i n t f ( E l e v a c i o n y c o r r i e n t e s en e s t u a r i o con f r i c c i o n r e p r e s e n t a d a s . . . \ n\n )
% ( 9 ) C i r c u l a c i o n e s t u a r i n a
ESTUARINA )
d i s p ( CIRCULACION
h = i n p u t ( I n t r o d u z c a l a p r o f u n d i d a d media (m ) : ) ;
z = 0: h /20: h ;
% m
r h o = i n p u t ( S a l i n i d a d en d e s e m b o c a d u r a ( p s u ) : ) ;
% rho = 35;
% E s t i m a c i o n e s en p s u
Drho = r h o ;
% C o n s i d e r o p r o p t o b e t a , c o n t r a c c i o n s a l i n a
Dx = i n p u t ( I n t r o d u z c a l a l o n g i t u d d e l e s t u a r i o (m ) : ) ;
Az = i n p u t ( I n t r o d u z c a e l c o e f i c i e n t e de v i s c o s i d a d t u r b u l e n t a ( m2 / s ) : ) ; % p . e j . Az = 0 . 0 1 ;
g = 9.82;
% m/ s2
u r = (g ( Drho / Dx) h 3 ) / ( 4 8 r h o Az)( 8( z / h ) . 3 + 1 5 ( z / h ) . 2 6 ( z / h ) ) ;
subplot (4 ,3 ,11)
p l o t ( ur , z , k ) ; h o l d on
q u i v e r ( z e r o s ( 1 , l e n g t h ( z ) ) , z , ur , z e r o s ( 1 , l e n g t h ( u r ) ) , 0 . 0 5 )
x l a b e l ( z (m) )
y l a b e l ( u {\ r h o }(m/ s ) )
ylim ( [ 0 h ] )
t i t l e ( Circ . e s t u a r i n a )
f p r i n t f ( C i r c u l a c i o n i n d u c i d a p o r e l g r a d i e n t e de d e n s i d a d c a l c u l a d a . . . \ n\n )
% ( 1 0 ) S o b r e l e v a c i o n v i e n t o
POR VIENTO )
d i s p ( SOBRELEVACION
F = i n p u t ( I n t r o d u z c a e l F e t c h (m ) : ) ; % p . e j . F = 10000
r h o a = i n p u t ( I n t r o d u z c a l a d e n s i d a d d e l a i r e ( kg / m3 ) : ) ; % p . e j . r h o a = 1 . 2
r h o = i n p u t ( I n t r o d u z c a l a d e n s i d a d d e l a g u a ( kg / m3 ) : ) ; % p . e j . r h o a = 1000
CD = i n p u t ( I n t r o d u z c a e l c o e f i c i e n t e de a r r a s t r e en s u p e r f i c i e ( adim ) : ) ; % p . e j . CD = 2 . 5 e3
n = i n p u t ( I n t r o d u z c a e l f a c t o r de f r i c c i o n en f o n d o ( adim ) : ) ; % p . e j . CD = 2 . 5 e3
wind = i n p u t ( I n t r o d u z c a l a v e l o c i d a d d e l v i e n t o (m/ s ) : ) ; % p . e j . wind = 15
h = i n p u t ( I n t r o d u z c a l a p r o f u n d i d a d a l b o r d e de l a p l a t a f o r m a (m ) : ) ; % p . e j . h0 = 150
x = 0:F/1000:F ;
% m
g = 9.82;
% m/ s
17
h0 = 1 5 0 ;
% m
tauw = r h o a CDwindabs ( wind ) ;
gamma = ntauwF / ( 0 . 5 r h o gh 2 ) ;
e t a = h0 ( s q r t (1+2gammax / F ) 1 ) ;
subplot (4 ,3 ,12)
plot ( x /1000 , eta , k )
x l a b e l ( x ( km ) )
y l a b e l ( \ e t a (m) )
t i t l e ( Sobrelev . viento )
f p r i n t f ( S o b r e e l e v a c i o n p o r v i e n t o c a l c u l a d a . . . \ n\n )
18
son format long G, format long E, format short E, etc. fscanf y fprintf, input, load,
save.
19
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f u n c t i o n [ F P I ] = e j e m p l o 5 (M)
% ( 1 ) F a c t o r i a l m u l t i p l i c a n d o de 1 a M . . .
F = 1;
f o r k = 1 :M
F = Fk ;
end
f p r i n t f ( E l f a c t o r i a l m u l t i p l i c a n d o de 1 a %d r e s u l t a : %f \n , M, F )
% . . . y desde M a 1 . . .
F = 1;
f o r k=M: 1:1
F = Fk ;
end
f p r i n t f ( E l f a c t o r i a l m u l t i p l i c a n d o de %d a 1 : %f \n , M, F )
% . . . o con un c i c l o w h i l e ( aunque s e s a b e e l n u mero e x a c t o de i t e r a c i o n e s )
F = 1;
k = M;
w h i l e k>1
F = Fk ;
k = k 1;
end
f p r i n t f ( E l f a c t o r i a l d e t e r m i n a d o con un c i c l o w h i l e : %f \n , F )
% . . . y con l a f u n c i o n de M a t l a b f a c t o r i a l ( )
F = f a c t o r i a l (M) ;
f p r i n t f ( E l f a c t o r i a l d e t e r m i n a d o con l a f u n c i o n de M a t l a b f a c t o r i a l ( ) : %f \n , F )
1.7.1 Iterativas
Las estructuras de control iterativas usuales son el ciclo for y el ciclo while. El
ciclo for es una estructura iterativa controlada por un contador. Se emplea cuando
20
1.7.2 De decision
Las palabras clave que definen las estructuras de decision son if, else, elseif, case.
Las estructuras condicionales o de decision sirven igualmente para controlar el flujo
del programa. En este caso, en funcion de si se cumple o no una condicion, se ejecuta
un bloque u otro de instrucciones.
Las condiciones se establecen mediante operadores relacionales (logicos), estos
son, <, <=, >, >=, ==, &, |, , =.
Example 1.5. Mediante el uso de la estructura iterativa for (tambien con while) se
puede determinar el factorial de un numero M, definido como M! = M (M 1)
(M 2) 1. No obstante, MatlabTM incluye una funcion propia (factorial(M)) que
realiza el calculo directamente.
Example 1.6. Otro ejemplo sencillo que proponemos para que el lector se vaya familiarizando con el uso de estas estructuras de datos es determinar el numero de
numeros pares e impares en un determinado intervalo dado.
Listing 1.6 Ejemplo sencillo del uso de estructuras de datos iterativas (for y while) y condicionales
if-else.
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % PROGRAMA: e j e m p l o 5
%
: C a l c u l a e l f a c t o r i a l de un n u mero
% % DESCRIPCION
%
% % e m p l e a n d o e s t r u c t u r a s i t e r a t i v a s ; c a l c u l a l a suma %
% % de n u meros p a r e s e i m p a r e s a n i d a n d o un c i c l o f o r .%
% % con una e s t r u c t u r a de d e c i s i o n .
%
% % USO: [ F P N] = e j e m p l o 5 (M)
%
% % ENTRADAS : E n t e r o p o s i t i v o M
%
% % SALIDAS : M a t r i z de d a t o s
%
%%
F : F a c t o r i a l de M
%
%%
P : Suma de p a r e s
%
%%
I : Suma de i m p a r e s
%
% % FECHA: 3 0 / 0 3 / 2 0 1 2
%
% % see also for , i f , while , switch , f a c t o r i a l
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f u n c t i o n [ F P I ] = e j e m p l o 5 (M)
% ( 1 ) F a c t o r i a l m u l t i p l i c a n d o de 1 a M . . .
F = 1;
f o r k = 1 :M
F = Fk ;
end
f p r i n t f ( E l f a c t o r i a l m u l t i p l i c a n d o de 1 a %d r e s u l t a : %f \n , M, F )
21
% . . . y desde M a 1 . . .
F = 1;
f o r k=M: 1:1
F = Fk ;
end
f p r i n t f ( E l f a c t o r i a l m u l t i p l i c a n d o de %d a 1 : %f \n , M, F )
% . . . o con un c i c l o w h i l e ( aunque s e s a b e e l n u mero e x a c t o de i t e r a c i o n e s )
F = 1;
k = M;
w h i l e k>1
F = Fk ;
k = k 1;
end
f p r i n t f ( E l f a c t o r i a l d e t e r m i n a d o con un c i c l o w h i l e : %f \n , F )
% . . . y con l a f u n c i o n de M a t l a b f a c t o r i a l ( )
F = f a c t o r i a l (M) ;
f p r i n t f ( E l f a c t o r i a l d e t e r m i n a d o con l a f u n c i o n de M a t l a b f a c t o r i a l ( ) : %f \n , F )
Chapter 2
23