UNIVERSIDAD POLITÉCNICA SALESIANA FACULTAD DE INGENIERÍAS INGENIERA ELÉCTRICA LABORATORIO 00 TEMA: Manual de MATLAB Luis Cruz Quinto Eléctrica 26-03-2010
Nov 18, 2014
UNIVERSIDAD POLITÉCNICA SALESIANA
FACULTAD DE INGENIERÍAS
INGENIERA ELÉCTRICA
LABORATORIO 00
TEMA:
Manual de MATLAB
Luis Cruz
Quinto Eléctrica
26-03-2010
1. MATRICES Y ARREGLOS
>> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
1.1. SUMA, TRANSPUESTA Y DIAGONAL
>> sum(A)
ans =
34 34 34 34
Se halla la traspuesta de A
>> A'
ans =
16 5 9 4
3 10 6 15
2 11 7 14
13 8 12 1
Encontramos la diagonal de A
>> diag(A)
ans =
16
10
7
1
Nos sirve para girar una matriz a la izquierda
>> fliplr(A)
ans =
13 2 3 16
8 11 10 5
12 7 6 9
1 14 15 4
1.2 SUBSCRIPTS
>> A(1,4) + A(2,4) + A(3, 4) +A(4,4)
ans =
34
Como queramos A(4,5) como no existe no lo encontraremos
>> A(4,5)
??? Attempted to access A(4,5); index out of bounds because size(A)=[4,4].
Para incrementar el tamaño de una matriz.
>> X=[16 3 2 13;5 10 11 8;9 6 7 12;4 15 14 1]
X =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
>> X(4,5) = 17
X =
16 3 2 13 0
5 10 11 8 0
9 6 7 12 0
4 15 14 1 17
1.3 OPERADOR COLON
>> 1:10
ans =
1 2 3 4 5 6 7 8 9 10
Para obtener un incremento determinado
>> 100:-7:50
ans =
100 93 86 79 72 65 58 51
Otro ejemplo
>> 0:pi/4:pi
ans =
0 0.7854 1.5708 2.3562 3.1416
Para calcular la de los elementos de la cuarta columna, se puede utilizar la expresión
>> sum(A(1:4,4))
ans =
34
LA FUNCIÓN MAGIC
Sirve para crear matrices de cuadrados mágicos
>> B = magic(4)
B =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> A = B(:,[1 3 2 4])
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
>> A = B(:,[4 2 3 1])
A =
13 2 3 16
8 11 10 5
12 7 6 9
1 14 15 4
2. EXPRESIONES
>> num_estudiantes=25
num_estudiantes =
25
Ejemplos de Expresiones
>> rho = (1+sqrt(5))/2
rho =
1.6180
>> rho = (1+sqrt(5))/2
rho =
1.6180
>> a = abs(3+4i)
a =
5
>> z = sqrt(besselk(4/3,rho-i))
3. TRABAJANDO CON MATRICES
z =
0.3730 + 0.3214i
>> huge = exp(log(realmax))
huge =
1.7977e+308
>> toobig = pi*huge
toobig =
Inf
>> ros(2,4)
??? Undefined function or method 'ros' for input arguments of type
'double'.
>> Z = zeros(2,4)
Z =
0 0 0 0
0 0 0 0
>> F = 5*ones(3,3)
F =
5 5 5
5 5 5
5 5 5
>> N = fix(10*rand(1,10))
N =
8 9 1 9 6 0 2 5 9 9
>> R = randn(4,4)
R =
-1.3499 0.7147 1.4090 0.7172
3.0349 -0.2050 1.4172 1.6302
0.7254 -0.1241 0.6715 0.4889
-0.0631 1.4897 -1.2075 1.0347
3.2 LA FUNCIÓN load
>> load magik.dat
>> magik
magik =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
3.4 ARCHIVOS –M
A = [ ...
16.0 3.0 2.0 13.0
5.0 10.0 11.0 8.0
9.0 6.0 7.0 12.0
4.0 15.0 14.0 1.0 ];
3.5 CONCATENACIÓN
Ejemplo:
>> B = [A A+32; A+48 A+16]
B =
13 2 3 16 45 34 35 48
8 11 10 5 40 43 42 37
12 7 6 9 44 39 38 41
1 14 15 4 33 46 47 36
61 50 51 64 29 18 19 32
56 59 58 53 24 27 26 21
60 55 54 57 28 23 22 25
49 62 63 52 17 30 31 20
3.5 Borrando filas y columnas
>> X = A
X =
13 2 3 16
8 11 10 5
12 7 6 9
1 14 15 4
Para borrar la segunda columna de X
>> X(:,2) = []
X =
13 3 16
8 10 5
12 6 9
1 15 4
4. MATRICES Y ARREGLOS ADICIONAL
4.1 ALGEBRA LINEAL
>> A=[16 3 2 13;5 10 11 8;9 6 7 12;4 15 14 1]
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Suma de una matriz con su traspuesta
>> A + A'
ans =
32 8 11 17
8 20 17 23
11 17 14 26
17 23 26 2
Producto de dos matrices mediante el operador *.
>> A*A'
ans =
438 236 332 150
236 310 278 332
332 278 310 236
150 332 236 438
Determinante de una matriz
>> d = det(A)
d =
0
Dada la matriz B se halla la inversa
>> B = [ 1 2 3 4; 5 6 7 8; 1 2 4 4 ; 1 1 3 7]
B =
1 2 3 4
5 6 7 8
1 2 4 4
1 1 3 7
>> C = inv(B)
C =
-1.7500 0.4167 0.3333 0.3333
2.1250 -0.1250 -1.0000 -0.5000
-1.0000 0 1.0000 0
0.3750 -0.0417 -0.3333 0.1667
5. COMANDOS DE CONTROL
>> x = [4/3 1.2345e-6]
x =
1.3333 0.0000
>> format short
>> x
x =
1.3333 0.0000
>> format short e
>> x
x =
1.3333e+000 1.2345e-006
>>
>> format short g
>> x
x =
1.3333 1.2345e-006
>> format long
>> x
x =
1.333333333333333 0.000001234500000
>> format long g
>> x
x =
1.33333333333333 1.2345e-006
>> format bank
>> x
x =
1.33 0.00
>> format rat
>> x
x =
4/3 1/810045
>> format hex
>> x
x =
3ff5555555555555 3eb4b6231abfd271
>> A = magic(100);
GRAFICAS
1. FUNCIONES BÁSICAS PARA DIBUJO CREANDO LA GRAFICA DE UNA FUNCIÓN
>> x=0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
AÑADIR ETIQUETA A LOS EJES Y TÍTULO.
>> x=0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
>> xlabel('x = 0:2\pi')
>> ylabel('Sine of x')
>> title('Plot of the Sine Function','FontSize',12)
DIBUJANDO MULTIPLES DATOS EN UNA GRÁFICA
Múltiples parejas de argumentos x-y al utilizar plot crean múltiples gráficas. MATLAB utiliza
colores distintos para discriminar los tipos de datos.
>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> y2 = sin(x-.25);
>> y3 = sin(x-.5);
>> plot(x,y,x,y2,x,y3)
ESPECIFICANDO TIPOS DE LÍNEAS Y COLORES
Los string para color son: 'c', 'm', 'y', 'r', 'g', 'b', 'w' y ‘k’, que corresponden a los colores cyan,
magenta, amarillo, rojo, verde, azul , blanco y negro.
Los string para estilos de línea son: '-' para línea solida, '--' para línea entrecortada, ':' para línea
punteada, and '-.' Para línea y punto.
Los tipos de marcas son: '+', 'o', '*', y 'x', and marcas con relleno son 's' para cuadrado, 'd' para
diamante, '^' para triangulo superior, 'v' para triángulo inferior, '>' para triangulo derecho, '<' para
triángulo izquierdo, 'p' para pentagrama, 'h' para hexagrama.
DIBUJANDO LÍNEAS Y MARCADORES
Si se especifica una marcador y no el tipo de línea solo
>> x1 = 0:pi/100:2*pi;
>> x2 = 0:pi/10:2*pi;
>> plot(x1,sin(x1),'r:',x2,sin(x2),'r+')
1.5 DIBUJANDO DATOS IMAGINARIOS Y COMPLEJOS
>> t = 0:pi/10:2*pi;
>> plot(exp(i*t),'-o')
>> axis equal
1.6 AÑADIENDO GRÁFICOS A UNA GRÁFICA EXISTENTE
>> [x,y,z] = peaks;
>> pcolor(x,y,z)
>> shading interp
>> hold on
>> contour(x,y,z,20,'k')
>> hold off
1.7 COMANDO FIGURE
>> t = 0:pi/10:2*pi;
>> [X,Y,Z] = cylinder(4*cos(t));
>> subplot(2,2,1); mesh(X)
>> subplot(2,2,2); mesh(Y)
>> subplot(2,2,3); mesh(Z)
>> subplot(2,2,4); mesh(X,Y,Z)
1.7 CONTROLANDO LOS EJES\
>> t = -pi:pi/100:pi;
>> y = sin(t);
>> plot(t,y)
>> axis([-pi pi -1 1])
>> xlabel('-\pi \leq {\itt} \leq \pi')
>> ylabel('sen(t)')
>> title('Grafica de la función seno')
>> text(1,-1/3,'{\itNote la simetria impar.}')
SCRIPTS Y FUNCIONES
Ejemplo:
En la ventana principal de MATLAB, de un clic sobre la barra desplegable
FILE, New M-file, esto le permite crear un archivo nuevo.
r =
Columns 1 through 8
0 0 3.00 3.00 5.00 5.00 7.00 3.00
Columns 9 through 16
9.00 7.00 11.00 3.00 13.00 9.00 15.00 3.00
Columns 17 through 24
17.00 11.00 19.00 3.00 21.00 13.00 23.00 3.00
Columns 25 through 32
25.00 15.00 27.00 3.00 29.00 17.00 31.00 3.00
FUNCIONES
>> S=8
S =
8.00
>> potdos(S)
x =
256.00
ans =
256.00
CONCLUISIONES
EL manual nos sirve como una guía para correcto uso de matlab donde encontramos comandos básicos
que nos permite usar fácilmente de matlab.
También facilita realizar graficas, funciones y operaciones básicas con solo ingresar la función, para esto
necesitamos manejar bien los comandos básicos.