matrix laboratory “matlab” tecnolÓgico de monterrey campus saltillo dr. marco iván...

54
MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán http://www.geocities.com/mirsm/matlabprint.ppt 04/07/22 CURSO DE MATLAB ITESM SALTILLO 1

Upload: maria-armendariz

Post on 02-Feb-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

MATrix LABoratory“MATLAB”

TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO

Dr. Marco Iván Ramírez-Sosa Moránhttp://www.geocities.com/mirsm/matlabprint.ppt

22/04/23CURSO DE MATLAB ITESM

SALTILLO 1

Page 2: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

MATrix LABoratory MATLAB

PROGRAMACIÓN BÁSICA

Y APLICACIONES

22/04/23

CURSO DE MATLAB ITESM SALTILLO 2

Page 3: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Contenido

Desktop de Matlab Command Window Workspace Command History Current Directory

Tipos de programación Programación en el CW

Asignación de valores Operaciones básicas con

variables Formatos de presentación Iteraciones y bifurcaciones Vectores

22/04/23CURSO DE MATLAB ITESM

SALTILLO 3

Gráficas 2D y 3DGráficas 2D y 3D

PolinomiosPolinomios

MatricesMatrices

Imágenes RGB, JPGImágenes RGB, JPG

Sistemas de ecuacionesSistemas de ecuaciones

Funciones de transferenciaFunciones de transferencia

Matlab simbólicoMatlab simbólico

– Programación de Programación de archivos *.marchivos *.m

Del Command HistoryDel Command History

Creación de FuncionesCreación de Funciones

– Programación en Programación en SimulinkSimulink

– Interacción Matlab-Interacción Matlab-SimulinkSimulink

Page 4: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Desktop de Matlab(Ventanas más importantes)

Command Window: Donde se ejecutan las funciones y se

realizan operaciones en forma de calculadora. Todo lo que se escribe en esta pantalla se graba en el Command History, y se pueden buscar operaciones anteriores con las flechas hacia arriba y abajo con la primera letra de la palabra a buscar). Se ponen comentarios con %.

22/04/23CURSO DE MATLAB ITESM

SALTILLO 4

Page 5: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Desktop de Matlab

Workspace: Variables definidas (nombre, valor y

tipo de dato) Se puede grabar, borrar y cargar variables guardadas.

22/04/23CURSO DE MATLAB ITESM

SALTILLO 5

Page 6: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Desktop de MatlabOtras ventanas

Command History: Comandos y operaciones ejecutadas con

anterioridad. La lista aparece separada por fechas automáticamente

22/04/23CURSO DE MATLAB ITESM

SALTILLO 6

Page 7: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Desktop de MatlabOtras ventanas

Current Directory: Lugar donde se almacenarán archivos de

datos, de programa y funciones creadas por usuario. (Se puede cambiar usando comandos de windows o usando los íconos en la misma ventana)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 7

Page 8: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Tipos de Datos

1.Complex Double-Precision Matrices

2.Numeric Matrices

3.Logical Matrices

4.MATLAB Strings

5.Cell Arrays

6.Structures

7.Objects

8.Multidimensional Arrays

9.Empty Arrays

22/04/23CURSO DE MATLAB ITESM

SALTILLO 8

Page 9: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

TIPOS DE PROGRAMACIÓN1. En el Command Window Se asignan valores a variables de

diferentes tipos, constantes, vectores, matrices, arreglos de caracteres, etc.

>>A=34 >>B=[1 2 3;4 5 6]>>i='hola‘ >>m=['A34';'A25']>>v=[1 2 3 4] >>Q=[1;2;3;4]>>r=3.14 >>E=1.23333e4>>u=j >>I=9+8*j

22/04/23CURSO DE MATLAB ITESM

SALTILLO 9

Page 10: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Programación en el Command Window …• Se realizan operaciones con constantes

ya definidas por default o por el usuario

>>3*8 >>1/pi>>9-4

>>78*[3;5;2]>>78^2 >>1/eps>>[‘s’ ‘a’ ‘ltillo’] >>intmin>>realmax >>i/2>>[2 3 4]+[8 9 10]

22/04/23CURSO DE MATLAB ITESM

SALTILLO 10

Page 11: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Programación en el Command Window … Se evalúan funciones con argumentos

definidos y los resultados se pueden asignar a variables o manejar en la variable ans

>>sin(30) >>floor(3.5)

>>3*exp(-6) >>x_1=factorial(6)

>>y=tan(4)/abs(9+3*i) >>ans/3>>var_num_4=5+[3 0 -5]

22/04/23CURSO DE MATLAB ITESM

SALTILLO 11

Page 12: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Programación en el Command Window …

• Las asignaciones de variables se alamacenan en el Workspace y las de operaciones sin asignación se almacenan en la varible ans

• Funciones de windows y propias de matlab que se pueden ejecutar desde la ventana de comandos: dir cd delete close clear clc whos who what home which isvarname

22/04/23CURSO DE MATLAB ITESM

SALTILLO 12

Page 13: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Programación en el Command Window …• Las variables almacenadas en el

Workspace pueden ser grabadas en un archivo con el comando:

>>save Nom_Arch Nom_Var (sólo graba una)

>>save Nom_Arch (graba todas)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 13

Page 14: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Programación en el Command Window …

• Las variables almacenadas en el Workspace pueden ser borradas con el comando:

>>clear Nom_Var (sólo borra una)

>>clear all (borra todas)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 14

Page 15: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Programación en el Command Window …

• Las variables grabadas pueden recuperarse en cualquier momento con el comando:

>>load Nom_Arch

>> load C:\matlab71\work\Nom_Arch

22/04/23CURSO DE MATLAB ITESM

SALTILLO 15

Page 16: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Formatos de presentación

FORMAT SHORT FORMAT LONG FORMAT SHORT E FORMAT LONG E FORMAT SHORT G FORMAT LONG G

22/04/23CURSO DE MATLAB ITESM

SALTILLO 16

Page 17: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Formatos de presentación …

FORMAT SHORT ENG FORMAT LONG ENG FORMAT HEX FORMAT BANK FORMAT RAT FORMAT COMPACT FORMAT LOOSE

22/04/23CURSO DE MATLAB ITESM

SALTILLO 17

Page 18: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Iteraciones y bifurcaciones: For & While

>> for i=inicio:paso:fin... %operaciones a iterarend>>>>while (condición) ... %operaciones a iterar end>>

22/04/23CURSO DE MATLAB ITESM

SALTILLO 18

Page 19: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Iteraciones y bifurcaciones: if & elseif

>>if (condición 1).....elseif (condición 2)….else (no se cumplen 1 y 2)…end>>

22/04/23CURSO DE MATLAB ITESM

SALTILLO 19

Page 20: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Iteraciones y bifurcaciones: Switch & case

>>switch switch_expresion case case_expresion, … case {case_expr1, case_expr2,...}…otherwise ….end>>

22/04/23CURSO DE MATLAB ITESM

SALTILLO 20

Page 21: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Tipos de datosARREGLOS• Vectores (Arreglos unidimensionales)

operaciones con vectores: normas, longitud, suma y producto, etc.

>>v1=[1 2 3];%vector renglón>>v2=[1;2;3];%vector columna>>v3=3:.5:10;%vector con paso, inicio y fin>>v4=ones(1,4);%vector de unos>>v5=zeros(5,1);%vector de ceros

22/04/23CURSO DE MATLAB ITESM

SALTILLO 21

Page 22: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Vectores

• Elementos de un vector, asignación y presentación Nom_vector(#reng or #col)

>>v1(2) >>v2(3)>>v3(4:end)

>>v3(1:3)=[.1 .3 .5]>>v3(1:2:end) >>m=find(v3==7)>>v3(m) >>[M i]=max(v3)>>n=length(v4) >>v4(n)>>v0(10)=3 >>v0(1:9)=rand

22/04/23CURSO DE MATLAB ITESM

SALTILLO 22

Page 23: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Vectores …

• Operaciones con vectores:>>vt=v3’ %transpuesta de un vector>>d=norm(v3) %norma Euclideana>>v6=[1 2 3]/2+[5 6 7]-[4 0 2]*4+.5>>v7=[3 5 6].*[1 -4 0] %prod Hadamard>>v8=[2 4 8].^2 %potencia de cada

elemento>>v9=[v6 v8 v7]>>d1=norm(v9)>>s=sum(v9)>>d1=sqrt(sum(v9.^2))

22/04/23CURSO DE MATLAB ITESM

SALTILLO 23

Page 24: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Gráficas con Vectores

Graficar y1=sin(t)^2, t entre 0 y 10 seg>>t=0:.1:10;>>y1=(sin(t)).^2;>>plot(y1) >>plot(t,y1) >>plot(t,y1,’xr’)>>xlabel(‘tiempo s’),ylabel(‘voltaje v’)>>title(‘grafica de y_1 = (sin (t))^2’)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 24

Page 25: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Gráficas con Vectores

• Cerrar y abrir figuras>>figure(1) >>close>>figure(2),figure(5) >>close all• Empalmar figuras>>figure(1)>>y2=3*sin(0:.1:10);y3=sin(0:.1:10);>>plot(y2,’r*’);hold on;plot(y3,’om’);>> grid,axis([0 100 -4 4]),hold off

22/04/23CURSO DE MATLAB ITESM

SALTILLO 25

Page 26: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Gráficas con Vectores

• Gráficas 3D>>x=[-2:.1:2];>>y=[-2:.1:2];>>z1=sin(x).^2+cos(y).^2;>>z2=cos(x).^2+sin(y).^2;>>plot3(x,y,z1)>>hold on,plot3(x,y,z2)>>grid

22/04/23CURSO DE MATLAB ITESM

SALTILLO 26

Page 27: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Gráficas 3D

Mallas de datos>> [xm ym]=meshgrid(x,y);>> z2=cos(xm).^2+sin(ym).^2;>> mesh(xm,ym,z2)>> figure,surf(xm,ym,z2)>> figure,meshc(xm,ym,z2)>> figure,meshz(xm,ym,z2)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 27

Page 28: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Gráficas con Vectores

Polígonos:>>x=[0 0 1 1 0];y=[0 1 1 0 0];>>plot(x,y),axis([-3 4 -3 4]),hold on,

plot(x,y,'or')

Mover polígonos:>>x1=x+1.5;plot(x1,y),y1=y-

1.5;plot(x1,y1)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 28

Page 29: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Gráficas con Vectores

• Escalamiento y movimiento de polígonos:>>close all,clear all>> x=[0 0 1 1 0];y=[0 1 1 0 0];>>for i=0:.1:5x1=x*i+i;y1=y*i+i*iplot(x1,y1),hold onaxis([-3 25 -3 25])end>>

22/04/23CURSO DE MATLAB ITESM

SALTILLO 29

Page 30: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Rotación de vectores

Rotación a la derecha>>vector=[1:10]>>vector=[vector(2:end) vector(1)]

Rotación a la izquierda>>vector2=1:10>>vector2=[vector2(end) vector2(1:end-

1)]

22/04/23CURSO DE MATLAB ITESM

SALTILLO 30

Page 31: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Vectores como polinomios

• Polinomio p(x) = 7x^4+5x^2-4x+3 se representa en Matlab:

>>p=[7 0 5 -4 3];• Raíces del polinomio:

>>r=roots(p)• Evaluación del polinomio:

>>pval_1=polyval(p,-3)>>pval_2=polyval(p,r(1))

22/04/23CURSO DE MATLAB ITESM

SALTILLO 31

Page 32: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Polinomios

Producto de Polinomios>>p1=[1 0 1],p2=[1 -2 3]>>p3=conv(p1,p2) Creación de polinomios con base en sus raíces>>p4=poly([1 1 1]) >>p5=poly([-1 -1 2 0])>>p6=conv(p4,p5) >>raices=roots(p6)>>[factor,res]=deconv(p6,p4) %factorización

22/04/23CURSO DE MATLAB ITESM

SALTILLO 32

Page 33: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Polinomios

Fracciones de polinomios>>num=[1 2 3];>>den=[1 -4 5 -7];>>f=tf(num,den)

Fracciones parciales>>[res, polos,

kte]=residue(num,den)>>[num1,

num2]=residue(res,polos,kte)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 33

Page 34: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Polinomios

Derivada de polinomios

>>p = [1 0 -2 -5]>>q = polyder(p)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 34

Page 35: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Curvas de mínimos cuadrados

Valores de entrada x, salida y>>x = [1 2 3 4 5]; y=[5.5 43.1 128 290.7

498.4];>>p=polyfit(x,y,1); %1 es el grado del

polinomio>>x2 = 1:.1:5;>>y2 = polyval(p,x2);>>plot(x,y,'o',x2,y2)>>grid on

22/04/23CURSO DE MATLAB ITESM

SALTILLO 35

Page 36: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

MatricesArreglos n-dimensionales

Definición matriz nxm (n=reng,m=col)

>>M1=[1 2 3 4 5;0 9 8 -1 2] %n=2,m=5>>M2=ones(3,4) >>M3=zeros(2,3)>>M4=eye(4) >>M5=rand(5,2)>>M=pascal(5) >>M=magic(5)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 36

Page 37: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Tamaño, elementos y operaciones

>>s=size(M1) >>n=s(1)>>m=s(2)

>>M5(3,1)>>M6(3,5)=-1 >>M6(4,7)=pi>>M7=eye(size(M5))

>>M7+2*M5>>M6(1,:) >>M6(:,1)>>M6(2,1:3) >>M6(4,1:2:7)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 37

Page 38: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Potencias de matrices

>>B=A^2 %B=A*A>>C=A.^2 %element

wise>>R=sqrt(A) %element wise>>R.^2 %A= R.^2>>Q=sqrtm(A)>>Q^2 %A=Q*Q>>exp(A) %element wise>>D=expm(A) %A=logm(D)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 38

Page 39: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Matrices

• Producto punto>>v=[1 2 3]>>w=[4 6 8]>>M1=v*w’ %producto punto M1

n=1,m=1

• Producto de vectores y matrices>>M2=v’*w %producto M2 n=3, m=3>>M3=[1 0 2;0 1 -1];M4=[1 3;7 8;9 0];>>M=M3*M4 % n=? m=? >>M=M4*M3 %????????

22/04/23CURSO DE MATLAB ITESM

SALTILLO 39

Page 40: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Imágenes

>>I=imread(‘c:/trayectoria/archivo.jpg’);

>> imshow(I)>> R(:,:,1)=I(:,:,1);R(:,:,2:3)=0;>> figure,imshow(R)>> G(:,:,2)=I(:,:,2);G(:,:,1:2:3)=0;>> figure,imshow(G)>> B(:,:,3)=I(:,:,3);B(:,:,1:2)=0;>> figure,imshow(B)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 40

Page 41: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Matrices

Sistemas de ecuacionesx1= 3 a + 2 b

x2 = - 4 a + 8 bEvaluación con valores a y b conocidos:>>a=-1;b=3;>>x=[3*a+2*b,-4*a+8*b] >>x=[3 2;-4 8]*[a;b]

22/04/23CURSO DE MATLAB ITESM

SALTILLO 41

Page 42: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Solución de sistemas de ecuaciones Solución

x1= 3 a + 2 b x2 = - 4 a + 8 b

Dados x1 y x2 encontrar a y b:>>x=[4; -1];>>M=[3 2;-4 8];>>d=det(M) % d diferente de 0?>>sol=inv(M)*x;a=sol(1),b=sol(2)

%existe?22/04/23

CURSO DE MATLAB ITESM SALTILLO 42

Page 43: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Pseudoinversa

Solución del sistema sobredeterminado:x1 = 3b + 4cx2 = -5b +2cx3 = b – 3c

Dados x1, x2, x3 encontrar b y c>>x=[-1 0 4]’>>M=[3 4;-5 2;1 -3];>>sol=pinv(M)*x >>sol=M\x>>xapp=M*sol >>norm(x-

xapp)22/04/23

CURSO DE MATLAB ITESM SALTILLO 43

Page 44: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Solución de sistemas de ecuaciones• Solución con a y b desconocidos

x1 = 3 a + 2 b x2 = - 6 a - 4 b Dados x1 y x2 encontrar a y b:>>x=[4 -1]’;>>M=[3 2;-6 -4]; %existe solución?>>x=[1 -2]’;>>solucion=????

%rank(M)==rank([M x]) inf

22/04/23CURSO DE MATLAB ITESM

SALTILLO 44

Page 45: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Eigenvalores, Eigenvectores y Factorización de matrices

• Polinomio característico de una matriz p(s) = det ( s I – A ) eigenvalores = {roots[p(s)]}

>>A=rand(4,4); >>p=poly(A) >>r=roots(p) >>[V L]=eig(A)

>>[K,J]=jordan(A) >>K*J*inv(K)>>[U S]=schur(A) >>U*A*U’>>[U S V]=svd(A) >>U*S*V’

22/04/23CURSO DE MATLAB ITESM

SALTILLO 45

Page 46: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

SYMBOLIC

>>sym a b k t s % def variables simbol

>>a+a+b+b+b>>a*a*a>>x=laplace(sin(t)) >>y=ilaplace(1/s)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 46

Page 47: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

TIPOS DE PROGRAMACIÓN2. Creación archivos *.m

• Archivos .m de listado de comandos y de funciones:– Programas de ejecución de comandos,

lectura y grabación de datos con inicio: close all, clear all, clc, sólo usa variables globales.

– Programación de una Función: fuction()– Ejecución de una función desde un archivo

*.m (usa variables locales y globales)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 47

Page 48: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Archivos *.m

Entrada y salida de datos de un archivo *.m

c=input(‘Dame un dato numérico’);s=input(‘Dame un string’,’s’);disp([‘el dato numérico es ’

num2str(c)])disp([‘el dato string es ’ s])eval([s ‘_1=’ num2str(c)])

22/04/23CURSO DE MATLAB ITESM

SALTILLO 48

Page 49: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

EJERCICIO 1

Hacer un programa que grafique un paraboloide usando programación en el CW y en un archivo *.m

22/04/23CURSO DE MATLAB ITESM

SALTILLO 49

Page 50: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Creación de una función

Definición dentro de un archivo *.m

function [salida1,salida2,…] =

nombre_funcion(arg1,arg2,…);…salida1=…salida2=…

22/04/23CURSO DE MATLAB ITESM

SALTILLO 50

Page 51: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

EJERCICIOS 2 y 3

Hacer una función que rote cualquier arreglo n pasos.

Hacer una función que calcule el factorial de cualquier número dado entre 180 y 250.

22/04/23CURSO DE MATLAB ITESM

SALTILLO 51

Page 52: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

TIPOS DE PROGRAMACIÓN3. SIMULINK Creación de funciones con diagramas

de bloques, utilizando funciones de Matlab.

Interacción entre funciones de Matlab y Simulink.

Graficación usando señales obtenidas en Simulink.

22/04/23CURSO DE MATLAB ITESM

SALTILLO 52

Page 53: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Fuentes y entradas Bloques Funciones Dispositivos de Monitoreo Comunicación con Matlab

22/04/23CURSO DE MATLAB ITESM

SALTILLO 53

Elementos de SimulinkElementos de Simulink

Page 54: MATrix LABoratory “MATLAB” TECNOLÓGICO DE MONTERREY CAMPUS SALTILLO Dr. Marco Iván Ramírez-Sosa Morán  18/07/2015

Control

Función de transferencia sist=tf(num,den)

Polos y ceros roots(den), roots(num) Root-locus rlocus(num,den) step(num,den) impulse(num,den) bode(num,den) sisotool(sist)

22/04/23CURSO DE MATLAB ITESM

SALTILLO 54