Documentos de Académico
Documentos de Profesional
Documentos de Cultura
M. en C. Erika Vilches
Toda
>>
es utilizada para denotar la lnea de comando de Matlab, donde se
insertan los comando.
Casi
VECTORES
Un
Esto
El
Matlab
Si
>> v = [3 1];
>>
Si
>> v
v =
3
De
>> v = [3 1 7 -21 5 6]
v =
3
Note
-21
Si
Crear
Para
>> v = [2:.25:4]
v =
Columns 1 through 7
2.0000
2.2500
3.2500
3.5000
Columns 8 through 9
3.7500
4.0000
2.5000
2.7500
3.0000
Acceder
>> v(1)
ans =
2
Este
Se
Matlab
Ej: Una
>> [0:2:8]
ans =
0
>> ans'
ans =
0
2
4
6
8
Matlab
>> v = [0:2:8]
v =
0
>> v(1:3)
ans =
0
0
4
>> v(1:2:4)'
ans =
Una
En
-2
-2
Ejemplo: Definir
pasos de -1.
>> u = [0:-1:-4]
u =
0
Sumar
-1
-2
-3
-4
>> u+v
ans =
0
La
>> -2*u
ans =
0
>> v/3
ans =
0
0.6667
1.3333
2.0000
2.6667
Con
>> -2*u+v/3
ans =
0
2.6667
5.3333
8.0000
10.6667
Estas
Ejemplo: Sumar
>> u+v'
??? Error using ==> plus
Matrix dimensions must agree.
MATRICES
Definir
Para
2
4
7
3
5
8
Tambin
2
4
7
3
5
8
Si
>> whos
Name
Size
A
B
ans
u
v
3x3
3x3
1x5
1x5
1x5
Bytes
72
72
40
40
40
Class
double
double
double
double
double
Attributes
La
Es
>> v = [0:2:8]
v =
0
>> A*v(1:3)
??? Error using ==> *
Inner matrix dimensions must agree.
>> A*v(1:3)'
ans =
16
28
46
Es
3
5
>> A(1:2,2:3)'
ans =
2
3
4
5
FUNCIONES DE MATRICES
Una
>> inv(A)
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND =
3.469447e-18.
ans =
1.0e+15 *
-2.7022
5.4043
-2.7022
4.5036
-9.0072
4.5036
-1.8014
3.6029
-1.8014
Matlab
es case sensitive.
>> inv(a)
??? Undefined function or variable a.
Otras
Si
>> eig(A)
ans =
14.0664
-1.0664
0.0000
>> [v,e] = eig(A)
v =
-0.2656
-0.4912
-0.8295
0.7444
0.1907
-0.6399
-0.4082
0.8165
-0.4082
0
-1.0664
0
0
0
0.0000
e =
14.0664
0
0
Ej: Obteniendo
>> diag(A)
ans =
1
4
8
Para
>> clear
>> whos
FUNCIONES DE VECTORES
Sumas
y restas de vectores.
Ej: Definiremos
Definiendo
>> v = [1 2 3]'
v =
1
2
3
>> b = [2 4 6]'
b =
2
4
6
Sumndolos
>> v+b
ans =
3
6
9
>> v-b
ans =
-1
-2
-3
y restndolos:
Las
Ej: En
>> v*b
??? Error using ==> *
Inner matrix dimensions must agree.
>> v*b'
ans =
2
4
6
>> v'*b
ans =
28
4
8
12
6
12
18
Matlab
matriz.
Ej: Multiplicar
>> v.*b
ans =
2
8
18
>> v./b
ans =
0.5000
0.5000
0.5000
Si
Ej: Podemos
0.1000
0.6000
0.2000
0.3000
0.4000
99.7000
99.8000
[stuff deleted]
Columns 995 through 1001
99.4000
99.5000
99.9000 100.0000
99.6000
>> y = sin(x).*x./(1+cos(x));
>> plot(x,y)
>> plot(x,y,'rx')
Para
blue
green
red
cyan
magenta
yellow
black
.
o
x
+
*
s
d
point
circle
x-mark
plus
star
square
diamond
:
-.
--
solid
dotted
dashdot
dashed
v
^
<
>
p
h
triangle (down)
triangle (up)
triangle (left)
triangle (right)
pentagram
hexagram
>> plot(x,y,'y',x,y,'go')
>> plot(x,y,'y',x,y,'go',x,exp(x+1),'m--')
Crear
>> a = zeros(5)
a =
0
0
0
0
0
Crear
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
>> a = zeros(2, 3)
a =
0
0
0
0
0
0
0
0
0
0
0
CICLOS
Nos
Todas
Ciclos
Para
for:
Ej: Un
Cuando
j
Cada
Ej: Definiendo
10
Ej: Realizar
3
2
1
2
1
3
3
-1
1
2
-1
3
1
1
2
3
-1
2
2
-1
4
A =
Ciclo
while:
Repite
>> n = 1
n =
1
>> while n < 3
n = n+1
end
n =
2
n =
3
PLOTTEANDO EN MATLAB
El
Acepta
Ej: Si
>> plot(x,y,'go')
Si
Ej: Una
>>
>>
>>
>>
subplot(1,2,1);
plot(x,y,'go',x,true)
subplot(1,2,2);
plot(x,abs(true-y),'mx')
El
Para
Para
Para
Ej:
plot(x1,y1,'go',x1,true1)
plot(x1,abs(true1-y1),'mx')
subplot(1,2,1);
plot(x,abs(true-y),'mx')
subplot(1,2,2);
plot(x1,abs(true1-y1),'mx')
title('Errors for h=1/32')
xlabel('x');
ylabel('|Error|');
subplot(1,2,1);
xlabel('x');
ylabel('|Error|');
title('Errors for h=1/16')
Para
Si
ARCHIVOS EJECUTABLES
Primero, necesitamos
El
crear un archivo.
Los
En
Si
La
Para
>> path
MATLABPATH
/Applications/MATLAB74/toolbox/matlab/general
/Applications/MATLAB74/toolbox/matlab/ops
/Applications/MATLAB74/toolbox/matlab/lang
/Applications/MATLAB74/toolbox/matlab/elmat
/Applications/MATLAB74/toolbox/matlab/elfun
/Applications/MATLAB74/toolbox/matlab/specfun
/Applications/MATLAB74/toolbox/matlab/matfun
/Applications/MATLAB74/toolbox/matlab/datafun
/Applications/MATLAB74/toolbox/matlab/polyfun
/Applications/MATLAB74/toolbox/matlab/funfun
/Applications/MATLAB74/toolbox/matlab/sparfun
/Applications/MATLAB74/toolbox/matlab/scribe
/Applications/MATLAB74/toolbox/matlab/graph2d
... etc
Para
Para
Para
Para
Una
% file: rectangle_area.m
% This matlab file will find the area of a rectangle
%
% A = b * h
%
%
% To run this file you will first need to specify
% the following:
%
b
: the base size
%
h
: the height size
%
% The routine will mutiply the given base times the
% heigth and will store the area of the rectangle in A.
%
% If you haven't guessed, you can use the percent sign
% to add comments =).
%
A = b * h;
Salvar
En
>> rectangle_area
??? Undefined function or variable 'b'.
Error in ==> rectangle_area at 20
A = b * h;
Si
Es
>>
>>
>>
>>
b = 5;
h = 4;
rectangle_area
who
>> whos
Name
A
b
h
Size
1x1
1x1
1x1
>> A
A =
20
Bytes
8
8
8
Class
double
double
double
Attributes
Una
Una
SUBRUTINAS EN MATLAB
En
Una
Las
Para
>> A = rectangle_area(5,4)
A =
20
>> rectangle_area(5,4)
ans =
20
Si
De
V =
120
Cuando
b * h
EL ENUNCIADO IF
En
La
(condition statement)
(matlab commands)
Una
if
(condition statement)
(matlab commands)
elseif (condition statement)
(matlab commands)
elseif (condition statement)
(matlab commands)
.
.
.
else
(matlab commands)
end
Las
Las
<
- Menor que
>
- Mayor que
<=
>=
==
- Igual
- No es igual
Ej: Hacer
>> a = 2;
>> b = 3;
>> if a<b
j = -1;
end
Ej: Hacer
Ej: Hacer
Se
ARCHIVOS
Conforme
vectores
El
El
Ej: Guardar
1x3
1x3
Bytes
24
24
Class
Attributes
double
double
ls
En
Ej: Los
>> clear
>> whos
>> load todo.mat
>> whos
Name
Size
u
v
Bytes
1x3
1x3
24
24
Class
Attributes
double
double
>> u + v
ans =
3
En
No
Despus
Ej: Solamente
>> clear
>> whos
>> load todo.mat u
>> whos
Name
Size
u
1x3
24
>> u
u =
1
Bytes
-4
Class
double
Attributes
El
Si
Para
Hay
Matlab
Es
El
Es
Seguido
Al
Ej: Generar
>> b = [4 5 6]
b =
4
>> a + b
ans =
5
>> diary
El
DOCUMENTACIN
Para
Lo
EJERCICIO
Realice
Dispone
de 30 minutos.
Solucin:
function [x1, x2] = raices (a, b, c)
x1 = (-b+sqrt((b^2)-4*a*c))/(2*a);
x2 = (-b-sqrt((b^2)-4*a*c))/(2*a);
end
Corriendo
el programa:
x2 =
-2.6180
Si
Click
el cdigo a corregir
Nuestro
Si
Si
Para
Ej: Validar
Si
Si
Ej: Indicar
Al
x1 =
x1 =
-0.3820
x2 =
-2.6180
-1.5000 + 1.9365i
x2 =
-1.5000 - 1.9365i
Para
DEBUGGER
Para
Click
Si
Para
Esto
Para
Para
Dar
Indicar
Correr
Para
TIPOS DE DATOS
Enteros
Strings
Booleanos
Celdas
Estructuras
Variables
Objetos
Simblicas
Conversiones
GENERACIN DE REPORTES
Una
Matlab
El
Ir
Para
Para
la descripcin.
Colocaremos
este script.
Una
Al
Para
Negritas
Italicas
Para
insertar una
ecuacin
Click
derecho
Insert Text
Markup
Tex
Equation
Nos
Para
Seleccionar
Bajar
la segunda opcin
Generando
>> syms a b c x
>> ec = a * (x^2) + b * x + c;
>> raices = solve(ec)
raices =
-1/2*(b-(b^2-4*a*c)^(1/2))/a
-1/2*(b+(b^2-4*a*c)^(1/2))/a
>> latex(raices(1))
ans =
-1/2\,{\frac {b-\sqrt {{b}^{2}-4\,ac}}{a}}
>> latex(raices(2))
ans =
-1/2\,{\frac {b+\sqrt {{b}^{2}-4\,ac}}{a}}
Copiando
Al
Asimismo, podemos
... etc