0% found this document useful (0 votes)
8 views22 pages

Matlab 2

Manual

Uploaded by

romario souza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views22 pages

Matlab 2

Manual

Uploaded by

romario souza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

I.

FUNDAMENTOS DE MATLAB
U N I V E R S I D A D DE G U A D A L A J A R A
21 DE FEBRERO DE 2013
31 DE AGOSTO DE 2016
(c) Dr. Orfil Gonzalez Reynoso
Version 1.0
Version 2.0

Contents
• Definicion de variables en MATLAB
• MATLAB define una matriz de dimensiones de 1 x 1 con el nombre de
• Formato de la variables
• Definiendo Vectores
• Guardando desde Matlab
• Cargando datos a Matlab
• Operaciones con matrices
• Operaciones logicas con MATLAB
• Comandos de control de flujo
• Cuanto vale a ?
• De igual forma si se tuviera la ecuacion condicional
• El enunciado -if- se puede utilizar con -else- o -elseif-, por
• MATLAB CUENTA CON CICLOS for/end y while/end
• Otra forma de realizar lo anterior

Definición de variables en MATLAB


Similar a otros lenguajes de programaci\’on, MATLAB soporta
herramientas para la introduccio\’on de Expresiones Matem\’aticas (EM).
Estas EM generalmente se componen de:
Variables
Numeros
Operadores y
Funciones.

Variables
MATLAB no requiere ning\’un tipo de declaraci\’n para definir las
dimensiones de una variable. Cuando MATLAB encuentra una nueva
variable, autom\’aticamente determina el numero de renglones y
columnas en funci\’on de los elementos emplados para definir una
variable. Por ejemplo si definimos

grupoA=40

1
grupoA =

40

MATLAB define una matriz de dimensiones de 1 x 1 con


el nombre de
grupoA y el valor de 40.
Los nombres de las variables generalmente
consisten de letras y numeros. El tamanno del nombre de una variable
esta restringindo a un total de 31 caracteres.
MATLAB es sensitivo al
empleo de letras en mayusculas y minuscuals i.e.

area=4
%
AREA =5

total =area + AREA

area =

AREA =

total =

Formato de la variables
clear
clc

format short
% format long
% format long e

2
% format short e

a= 12.45678934567
b = 2+a

a =

12.4568

b =

14.4568

Definiendo Vectores
clear
clc
n= 20;
for i=1:n;
x(i)=i
y(i)=sin(3*i+1)
end
%Transpuesta de un vector
x=x’
y=y’

% Imprimiendo en pantalla
fprintf( ’Los valores de x = %2.5f and y=%2.3f\n\n’,x(:),y(:));

x =

y =

-0.7568

x =

1 2

3
y =

-0.7568 0.6570

x =

1 2 3

y =

-0.7568 0.6570 -0.5440

x =

1 2 3 4

y =

-0.7568 0.6570 -0.5440 0.4202

x =

1 2 3 4 5

y =

-0.7568 0.6570 -0.5440 0.4202 -0.2879

x =

1 2 3 4 5 6

y =

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499

4
x =

1 2 3 4 5 6 7

y =

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

x =

1 2 3 4 5 6 7 8

y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

Column 8

-0.1324

x =

1 2 3 4 5 6 7 8 9

y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

Columns 8 through 9

-0.1324 0.2709

x =

1 2 3 4 5 6 7 8 9 10

5
y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

Columns 8 through 10

-0.1324 0.2709 -0.4040

x =

1 2 3 4 5 6 7 8 9 10 11

y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

Columns 8 through 11

-0.1324 0.2709 -0.4040 0.5291

x =

1 2 3 4 5 6 7 8 9 10 11 12

y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

Columns 8 through 12

-0.1324 0.2709 -0.4040 0.5291 -0.6435

x =

6
1 2 3 4 5 6 7 8 9 10 11 12 13

y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

Columns 8 through 13

-0.1324 0.2709 -0.4040 0.5291 -0.6435 0.7451

x =

Columns 1 through 13

1 2 3 4 5 6 7 8 9 10 11 12 13

Column 14

14

y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

Columns 8 through 14

-0.1324 0.2709 -0.4040 0.5291 -0.6435 0.7451 -0.8318

x =

Columns 1 through 13

1 2 3 4 5 6 7 8 9 10 11 12 13

Columns 14 through 15

14 15

7
y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

Columns 8 through 14

-0.1324 0.2709 -0.4040 0.5291 -0.6435 0.7451 -0.8318

Column 15

0.9018

x =

Columns 1 through 13

1 2 3 4 5 6 7 8 9 10 11 12 13

Columns 14 through 16

14 15 16

y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

Columns 8 through 14

-0.1324 0.2709 -0.4040 0.5291 -0.6435 0.7451 -0.8318

Columns 15 through 16

0.9018 -0.9538

x =

Columns 1 through 13

8
1 2 3 4 5 6 7 8 9 10 11 12 13

Columns 14 through 17

14 15 16 17

y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

Columns 8 through 14

-0.1324 0.2709 -0.4040 0.5291 -0.6435 0.7451 -0.8318

Columns 15 through 17

0.9018 -0.9538 0.9866

x =

Columns 1 through 13

1 2 3 4 5 6 7 8 9 10 11 12 13

Columns 14 through 18

14 15 16 17 18

y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

Columns 8 through 14

-0.1324 0.2709 -0.4040 0.5291 -0.6435 0.7451 -0.8318

Columns 15 through 18

9
0.9018 -0.9538 0.9866 -0.9998

x =

Columns 1 through 13

1 2 3 4 5 6 7 8 9 10 11 12 13

Columns 14 through 19

14 15 16 17 18 19

y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

Columns 8 through 14

-0.1324 0.2709 -0.4040 0.5291 -0.6435 0.7451 -0.8318

Columns 15 through 19

0.9018 -0.9538 0.9866 -0.9998 0.9929

x =

Columns 1 through 13

1 2 3 4 5 6 7 8 9 10 11 12 13

Columns 14 through 20

14 15 16 17 18 19 20

y =

Columns 1 through 7

-0.7568 0.6570 -0.5440 0.4202 -0.2879 0.1499 -0.0089

10
Columns 8 through 14

-0.1324 0.2709 -0.4040 0.5291 -0.6435 0.7451 -0.8318

Columns 15 through 20

0.9018 -0.9538 0.9866 -0.9998 0.9929 -0.9661

x =

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

y =

-0.7568
0.6570
-0.5440
0.4202
-0.2879
0.1499
-0.0089
-0.1324
0.2709
-0.4040
0.5291

11
-0.6435
0.7451
-0.8318
0.9018
-0.9538
0.9866
-0.9998
0.9929
-0.9661

Los valores de x = 1.00000 and y=2.000

Los valores de x = 3.00000 and y=4.000

Los valores de x = 5.00000 and y=6.000

Los valores de x = 7.00000 and y=8.000

Los valores de x = 9.00000 and y=10.000

Los valores de x = 11.00000 and y=12.000

Los valores de x = 13.00000 and y=14.000

Los valores de x = 15.00000 and y=16.000

Los valores de x = 17.00000 and y=18.000

Los valores de x = 19.00000 and y=20.000

Los valores de x = -0.75680 and y=0.657

Los valores de x = -0.54402 and y=0.420

Los valores de x = -0.28790 and y=0.150

Los valores de x = -0.00885 and y=-0.132

Los valores de x = 0.27091 and y=-0.404

Los valores de x = 0.52908 and y=-0.644

Los valores de x = 0.74511 and y=-0.832

Los valores de x = 0.90179 and y=-0.954

12
Los valores de x = 0.98663 and y=-1.000

Los valores de x = 0.99287 and y=-0.966

Guardando desde Matlab


tabla1=[x,y]
save ejemplo2.dat tabla1 -ascii

tabla1 =

1.0000 -0.7568
2.0000 0.6570
3.0000 -0.5440
4.0000 0.4202
5.0000 -0.2879
6.0000 0.1499
7.0000 -0.0089
8.0000 -0.1324
9.0000 0.2709
10.0000 -0.4040
11.0000 0.5291
12.0000 -0.6435
13.0000 0.7451
14.0000 -0.8318
15.0000 0.9018
16.0000 -0.9538
17.0000 0.9866
18.0000 -0.9998
19.0000 0.9929
20.0000 -0.9661

Cargando datos a Matlab


%clear
%clc
load ejemplo2.dat
[n,m]=size(ejemplo2)
col1=1;
col2=2;
xc(:,1)=ejemplo2(:,1)
yc(:,1)=ejemplo2(:,2)

13
n =

20

m =

xc =

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

yc =

-0.7568
0.6570
-0.5440
0.4202
-0.2879
0.1499
-0.0089
-0.1324
0.2709
-0.4040

14
0.5291
-0.6435
0.7451
-0.8318
0.9018
-0.9538
0.9866
-0.9998
0.9929
-0.9661

Operaciones con matrices


clear
clc

%--------------------------------------------------------------------------
% COMANDOS PARA LA GENERACION DE DIFERENTES
% MATRICES

% COMANDOS: Funcion
% ones(n,m): Genera una matriz de unos n x m
% zeros(n,m): Genera una matriz de ceros n x m
% eye (n): Genera una matriz identidad n x n
%--------------------------------------------------------------------------

A=[1 4 6; 6 8 9; -3 7 -5]
B=[A, ones(3,2);zeros(2,3), eye(2)]

%-------------------------------------------------------------------------
% ALGLUNAS FUNCIONES PARA MATRICES
% Comando Funcion
% diag (A) Encuentra la diagonal de A
% triu(B) Matriz triangular superior de B
% tril(B) Matriz triangular inferior de B
% rand (n) Genera una matriz al azar n x n
% A’ Traspuesta de la matriz A
% eig(A) Eigenvalore y eigenvectores de la matriz A
% svd(A) Singular value descomposicion
% inv(A) Inversa de la Matriz A
% poly(A) Polinomio carcteristico de la matriz A
% det(A) Determinante de la matriz A
% size(A) Tamanno de la matriz A (n x m)
% rank(A) Rango de la matriz A

15
%-------------------------------------------------------------------------

z= diag(B)
z1 = triu(B)
z2= tril(B)
z3 = rand(3)
A2 = A’
eig(A)
[V,D]=eig(A)
svd(A)
t1= inv(A)
poly(A)
det(A)
size(A)
rank(A)

A =

1 4 6
6 8 9
-3 7 -5

B =

1 4 6 1 1
6 8 9 1 1
-3 7 -5 1 1
0 0 0 1 0
0 0 0 0 1

z =

1
8
-5
1
1

z1 =

1 4 6 1 1
0 8 9 1 1

16
0 0 -5 1 1
0 0 0 1 0
0 0 0 0 1

z2 =

1 0 0 0 0
6 8 0 0 0
-3 7 -5 0 0
0 0 0 1 0
0 0 0 0 1

z3 =

0.3922 0.7060 0.0462


0.6555 0.0318 0.0971
0.1712 0.2769 0.8235

A2 =

1 6 -3
4 8 7
6 9 -5

ans =

-5.0000
-4.5139
13.5139

V =

-0.5937 -0.6392 0.4065


-0.2545 -0.2228 0.8745
0.7634 0.7360 0.2648

D =

-5.0000 0 0
0 -4.5139 0

17
0 0 13.5139

ans =

15.1515
9.0842
2.2159

t1 =

-0.3377 0.2033 -0.0393


0.0098 0.0426 0.0885
0.2164 -0.0623 -0.0525

ans =

1.0000 -4.0000 -106.0000 -305.0000

ans =

305

ans =

3 3

ans =

Operaciones logicas con MATLAB


% Matlab cuenta con una poderosa biblioteca de funciones de procesamiento.
% La programacion en Matlab es muy simple y tiene soporte par las
% funciones DO, WHILE, y IF, los cuales operan de igual forma
% como en ottros lenguajes.
% Las relaciones y operaciones logicas en Matlab se definen
% como

18
% < Menor que
% > Mayor que
% <= Menor o igual que
% >= Mayor o igual que
% == Igual que
% ~= Diferente que
% & y
% | o
% ~ Diferente

Comandos de control de flujo


Comando Funcion
if Condicionante para ejecutar una instruccion
elseif Usado con if
else Usado con if
end Terminacion del comando if, for, while
for Repetir una instruccion un numero de veces
break Romper un for o while
pause Realizar una pausa hasta presionar alguna tecla

%-------------------------------------------------------------------------

clear
clc
% El enunciado - if - siempre debe terminar con un enunciado - end -
% Cuanto vale el volumen?
radio = 8;
if radio == 2
volumen = (4/3)*pi*(radio^3)
else
volumen=7
end

% Realizar ejemplos con ==, ~= etc.

% Los enunciados logicos - and y o - se denotan con & y |


% respectivamente. Por ejemplo la ecuacion condicional
% si g > 3 o g < 0, entonces a = 6

volumen =

19
Cuanto vale a ?
clear
clc
a=0;
g=-3;
if g >3 | g <0 ;
a= 6;
end

%pause

fprintf(’El valor de a =%f\n’,a)

El valor de a =6.000000

De igual forma si se tuviera la ecuacion condicional


si a > 3 y c < 0, b = 19
clear
clc
a=2;
c=-2;
if a>3 & c<0
b=19
else b=0
end

b =

El enunciado -if- se puede utilizar con -else- o -elseif-, por


ejemplo
clear
clc

r = 3
if r > 3 , bb = 1
elseif r ==3, bb=2
else bb=0
end

20
r =

bb =

MATLAB CUENTA CON CICLOS for/end y while/end


clear
clc
for r1 = 1:5
vol = (4/3)*pi*(r1^3);
disp ([r1,vol])
end

1.0000 4.1888

2.0000 33.5103

3.0000 113.0973

4.0000 268.0826

5.0000 523.5988

Otra forma de realizar lo anterior


clear
clc
r2=0;
while r2<5
r2 = r2+1;
vol = (4/3)*pi*(r2^3);

disp ([r2,vol])
%pause
end

21
1.0000 4.1888

2.0000 33.5103

3.0000 113.0973

4.0000 268.0826

5.0000 523.5988

22

You might also like