introducci n al matlab 03s4f282e0ad17f2003.jimcontent.com/download/version...gráficos y ploteo en...

37
UNSAAC UNSAAC Lic. Guillermo Mario Chuquipoma Pacheco [email protected] www.mariochuqui.jimdo.com

Upload: others

Post on 18-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

UNSAACUNSAACLic. Guillermo Mario Chuquipoma Pacheco

[email protected] www.mar iochuqui.jimdo.com

Page 2: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ploteo y Visualización de DatosPloteo y Visualización de Datos

Lic. Guillermo Mario Chuquipoma PachecoLic. Guillermo Mario Chuquipoma Pacheco 20092009

Page 3: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Temario GeneralTemario General1. Introducción.2. Interfaz de usuario de MATLAB.3. Variables del MATLAB.4. Ploteo y visualización de datos.5. m-Files.6. Estadística básica y análisis de datos.6. Estadística básica y análisis de datos.7. Tipos de datos.8. Entrada y salida de datos.9. Programación.10.Construcción de interfaces de usuarios gráficas.

Page 4: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

ObjetivoObjetivo• Se introduce el lado visual de MATLAB presentando

cómo crear ploteos de vectores y matrices.

• La visualización complementa las capacidadesnuméricas de MATLAB.

Page 5: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Fundamentos de GráficosFundamentos de Gráficos

Page 6: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Gráficos y Ploteo en MATLABGráficos y Ploteo en MATLAB

• Ploteo básico– plot, título, xlabel , grid, legend, hold, axis

• Edición del Plot– Editor de propiedad

• Ploteo de malla (mesh) y de superficie (surface)– meshgrid , mesh, surf

• Gráficos en 3-D

• Gráficos especiales

Page 7: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ploteo 2Ploteo 2--DD• Sintaxis:

plot(y) plot(x,y) plot(x,y,'clm') plot(x1, y1, 'clm1', x2, y2, 'clm2', ...)

color line marker

• Ejemplo:

x = -pi:pi/10:pi;y = tan(sin(x)) - sin(tan(x));plot(x,y, 'r --s ')

Page 8: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 1Ejercicio 1

PlotearPlotear lala funciónfunción tan()tan()..IdentifiqueIdentifique elel rangorango parapara elel ejeeje XX..

UseUse lala siguientesiguiente funciónfunción

plot(x, tan(x), 'plot(x, tan(x), '--ro')ro')

Page 9: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ploteo 2Ploteo 2--DD• PLOT(X,Y,S), donde S se forma uniendo :

b blue . point - solidg green o circle : dottedr red x x-mark -. dashdot c cyan + plus -- dash ed m magenta * star (none) no liney yellow s squarek black d diamond

v triangle (down)^ triangle (up)^ triangle (up)< triangle (left)> triangle (right)p pentagramh hexagram

• Por ejemplo PLOT(X,Y,'c+:') presenta un gráfico de color Cian, líneas de dos puntos con signos más

Page 10: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 2Ejercicio 2• Grafique la función seno + coseno , presentando el

menos dos ciclos usando estilos de línea, color y marca:

1

1.5

-8 -6 -4 -2 0 2 4 6 8-1.5

-1

-0.5

0

0.5

Page 11: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

hold on, hold offhold on, hold off• La función hold (suspenso) determina si los nuevos

objetos gráficos se añaden al grafico o reemplazan elobjeto en el gráfico.– hold on . conserva el gráfico actual y sus

propiedades de ejes. El siguiente gráfico seadiciona al actual.

– hold off . resetea las propiedades de los ejes. El– hold off . resetea las propiedades de los ejes. Elsiguiente gráfico se grafica completamente.

– hold all. Suspende el gráfico, el color y estilo delínea

Page 12: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 3Ejercicio 3• Ingrese las siguientes instrucciones por separado y

verifique el resultado.

plot(rand(12,2))

hold all

plot(randn(12,2))plot(randn(12,2))

plot(randn(12,2))

• Modifique las instrucciones para que se presenten s ólo las 3 últimas curvas.

Page 13: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 4Ejercicio 4• Grafique la función seno para tres diferentes rango s

usando diferentes estilo de línea, color y marcas:

sin(t) , sin(t –pi/2), sin(t –pi)

'-.r*‘ , '--mo', ':bs'

Page 14: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Color y tamaño de las marcasColor y tamaño de las marcas• Se puede especificar el color y el tamaño de las

marcas usando las propiedades del gráfico.– LineWidth– MarkerEdgeColor– MarkerFaceColor– MarkerSize

x = -pi:pi/10:pi;y = tan(sin(x)) - sin(tan(x));

plot(x,y,'--rs', 'LineWidth',2,...

'MarkerEdgeColor','k',...

'MarkerFaceColor','g',...

'MarkerSize',10)

Page 15: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Títulos, etiquetas y leyendasTítulos, etiquetas y leyendas• Si se desea presentar gráficos con títulos, etiquet as y

leyendas.

x=[0:0.1:2*pi];

y=sin(x);

z=cos(x);z=cos(x);

plot(x,y,x,z,'linewidth',2)

title('Sample Plot','fontsize',14);

xlabel('X values','fontsize',14);

ylabel('Y values','fontsize',14);

legend('Y data','Z data')

grid on

Page 16: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejemplo de PloteoEjemplo de Ploteo

Titulo (Title)

Etiqueta el

eje Y (Ylabel)

Etiqueta el eje X (Xlabel)

Cuadricula (Grid)

Leyenda (Legend)

Page 17: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 5Ejercicio 5• Presente el siguiente gráfico:

t=[0 : pi/200 : 2*pi]t=[0 : pi/200 : 2*pi]

x=sin(t);x=sin(t);

y1=sin(t+0.5);y1=sin(t+0.5);

y2=sin(t+1.0);y2=sin(t+1.0);

plot(x,y1,'rplot(x,y1,'r--',x,y2,'g',x,y2,'g----'); ');

title('Angulo difuso');title('Angulo difuso');

xlabel('x=sin(t)');xlabel('x=sin(t)');

ylabel('y=sin(t+)');ylabel('y=sin(t+)');

text(0,0,' text(0,0,' \\leftarrow sin(leftarrow sin(\\pi)','FontSize',18)pi)','FontSize',18)

Page 18: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Presentando múltiples gráficosPresentando múltiples gráficos• Nomenclatura:

– figura – la ventana donde se plotea los gráficos.– plot – una región en una ventana donde una curva

es presentada

• Tres formas de mostrar múltiples curvas:– Una figura que contiene 1+ plots .– Una figura que contiene 1+ plots .

• Usar hold

– Una figura contiene varios plots , c/plot tiene unacurva.

• Usar subplot

– Varias figuras , c/figura contiene 1+ plots , cada plotcontiene 1+ curvas

• Usar figure , subplot .

Page 19: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

La orden de “figure”La orden de “figure”• Si se desea tener varias figuras abiertas para el p loteo.• El comando crea una nueva ventana (figure window).

>> figure• Si tenemos 20 figuras abiertas y queremos que la

figura 9 sea el default (ahí se ploteara la siguien te curva)>> figure (9)>> plot (…)>> plot (…)

• Use el comando close (9) si se quiera cerrar la fig ura 9

Page 20: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 6Ejercicio 6• Gráfique 5 curvas de 20 números aleatorios c/u, cad a

uno de los cuales debe de presentarse en una ventan a diferente. Use el comando figure de la siguiente manera:figurefigure

plot . . . .plot . . . .

• Gráfique 5 curvas de 20 números aleatorios c/u, tal que las dos primeras y las dos últimas se presente en u na misma ventana (figure).

• Use los comandos figurefigure , plotplot y holdhold

Page 21: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

SubplotsSubplots

Sintaxis

»»subplot(2,2,1); subplot(2,2,1);

»» ……

»»subplot(2,2,2)subplot(2,2,2)

subplot(rows,cols,index)subplot(rows,cols,index)

»»subplot(2,2,2)subplot(2,2,2)

»» ......

»»subplot(2,2,3)subplot(2,2,3)

»» ......

»»subplot(2,2,4)subplot(2,2,4)

»» ......

Page 22: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 7Ejercicio 7• Diseña un programa para presentar cuatro gráficos e n

una sola ventana.

x = linspace(x = linspace(--pi,pi,100);pi,pi,100);

subplot(2,2,1)subplot(2,2,1)

plot(x,sin(x))plot(x,sin(x))plot(x,sin(x))plot(x,sin(x))

subplot(2,2,2)subplot(2,2,2)

plot(x,cos(x))plot(x,cos(x))

subplot(2,2,3)subplot(2,2,3)

plot(x,sinh(x))plot(x,sinh(x))

subplot(2,2,4)subplot(2,2,4)

plot(x,cosh(x))plot(x,cosh(x))

Page 23: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Cambio de escalaCambio de escala• El matlab dispone de las siguientes funciones para

cambios de escala en los ejes, operan al igual que la función plot .–– semilogx semilogx

• Dibuja una curva bidimensional utilizando una escala logarítmica en el eje x.

–– semilogy semilogy • Dibuja una curva bidimensional utilizando una

escala logarítmica en el eje y.–– loglog loglog

• Dibuja una curva bidimensional utilizando una escala logarítmica en ambos ejes.

Page 24: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 8Ejercicio 8• Desarrolle el ejercicio 1, en una escala logarítmic a

apropiada.

Page 25: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

SuperficiesSuperficies

12

34

56

0

2

4

60

20

40

60

80

100

Y axes 12

34

56

0

2

4

60

20

40

60

80

100

Y axes

surf mesh

10X axesY axes 10

X axesY axes

12

34

56

0

2

4

60

20

40

60

80

100

X axesY axes 12

34

56

0

2

4

60

20

40

60

80

100

X axesY axes

surfc meshc

Page 26: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

SuperficiesSuperficies

x = 0:0.1:2;y = 0:0.1:2;[xx, yy] = meshgrid(x,y);zz = sin(xx.^2+yy.^2);surf(xx,yy,zz)xlabel('X axes')ylabel('Y axes')

Page 27: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 9Ejercicio 9• Usted recibe los siguientes tamaños de 36 árboles d e

un bosque, grafique estos datos en matlab.

80 21 31 75 64 3980 21 31 75 64 39

20 46 46 47 93 8620 46 46 47 93 86

71 7 83 94 46 8271 7 83 94 46 82

22 60 30 1 51 9122 60 30 1 51 9122 60 30 1 51 9122 60 30 1 51 91

79 19 71 5 42 8479 19 71 5 42 84

17 93 7 74 42 8717 93 7 74 42 87

Page 28: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ploteo de superfies en 3Ploteo de superfies en 3--DD• contourf-plot3-waterfall-contour3-mesh-surf

Page 29: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 10Ejercicio 10z = rand(20,20);z = rand(20,20);

contourf(z,4);contourf(z,4);

colormap autumncolormap autumn

t = 0:pi/50:10*pi;t = 0:pi/50:10*pi;

plot3(sin(t),cos(t),t)plot3(sin(t),cos(t),t)

[X,Y,Z] = peaks(30);[X,Y,Z] = peaks(30);

waterfall(X,Y,Z)waterfall(X,Y,Z)

[X,Y] = meshgrid([X,Y] = meshgrid(--3:.125:3);3:.125:3);

[Rows, Cols] = size(X);[Rows, Cols] = size(X);

for r = 1:Rowsfor r = 1:Rowsplot3(sin(t),cos(t),t)plot3(sin(t),cos(t),t)

grid ongrid on

axis squareaxis square

for c = 1:Colsfor c = 1:Cols

Z(r,c) = sin(X(r,c)) Z(r,c) = sin(X(r,c)) + sin(Y(r,c));+ sin(Y(r,c));

endend

endend

waterfall(X,Y,Z)waterfall(X,Y,Z)

Page 30: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 11Ejercicio 11[X,Y] = meshgrid([[X,Y] = meshgrid([--2:.25:2]);2:.25:2]);

Z = X.*exp(Z = X.*exp(--X.^2X.^2--Y.^2);Y.^2);

contour3(X,Y,Z,30)contour3(X,Y,Z,30)

surface(X,Y,Z,'EdgeColor',[.8 .8 8],'FaceColor','no ne')surface(X,Y,Z,'EdgeColor',[.8 .8 8],'FaceColor','no ne')

grid offgrid off

view(view(--15,25)15,25)

colormap coolcolormap cool

Page 31: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ploteo especializadoPloteo especializado• bar-bar3h-hist-area-pie3-rose

Page 32: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 12Ejercicio 12x = [x = [--2.9:0.2:2.9];2.9:0.2:2.9];

bar(exp(bar(exp(--x.*x)); x.*x));

bar(x, exp(bar(x, exp(--x.*x),'r')x.*x),'r')

y = round(rand(5,3)*10);y = round(rand(5,3)*10);

subplot(2,2,1)subplot(2,2,1)

y = cool(7);y = cool(7);

subplot(3,2,1)subplot(3,2,1)

bar3(Y,'detached')bar3(Y,'detached')

title('Detached')title('Detached')

subplot(3,2,2)subplot(3,2,2)

bar3(Y,0.25,'detached')bar3(Y,0.25,'detached')

title('Width = 0.25')title('Width = 0.25')

subplot(3,2,3)subplot(3,2,3)

bar3(Y,'grouped')bar3(Y,'grouped')

bar(Y,'group')bar(Y,'group')

title 'Group'title 'Group'

subplot(2,2,2) subplot(2,2,2)

bar(Y,'stack')bar(Y,'stack')

title 'Stack'title 'Stack'

subplot(2,2,3)subplot(2,2,3)

barh(Y,'stack')barh(Y,'stack')

title 'Stack'title 'Stack'

subplot(2,2,4)subplot(2,2,4)

bar(Y,1.5)bar(Y,1.5)

title 'Width = 1.5'title 'Width = 1.5'

title('Grouped')title('Grouped')

subplot(3,2,4)subplot(3,2,4)

bar3(Y,0.5,'grouped')bar3(Y,0.5,'grouped')

title('Width = 0.5')title('Width = 0.5')

subplot(3,2,5)subplot(3,2,5)

bar3(Y,'stacked')bar3(Y,'stacked')

title('Stacked')title('Stacked')

subplot(3,2,6)subplot(3,2,6)

bar3(Y,0.3,'stacked')bar3(Y,0.3,'stacked')

title('Width = 0.3')title('Width = 0.3')

colormap([1 0 0;0 1 0;0 0 1])colormap([1 0 0;0 1 0;0 0 1])

Page 33: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 13Ejercicio 13y = randn(10000,1);y = randn(10000,1);

hist(y)hist(y)

y = randn(10000,1);y = randn(10000,1);

x = [0:0.1:1];x = [0:0.1:1];

hist(y,x)hist(y,x)

Y = [1, 5, 3;Y = [1, 5, 3;

3, 2, 7;3, 2, 7;

1, 5, 3;1, 5, 3;

2, 6, 1];2, 6, 1];

area(Y)area(Y)

grid ongrid on

colormap summercolormap summer

theta = 2*pi*rand(1,50);theta = 2*pi*rand(1,50);

rose(theta)rose(theta)

colormap summercolormap summer

set(gca,'Layer','top')set(gca,'Layer','top')

title 'Stacked Area Plot'title 'Stacked Area Plot'

Page 34: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Ejercicio 14Ejercicio 14•• x = [1 3 0.5 2.5 2]x = [1 3 0.5 2.5 2]•• explode = [0 1 0 0 0]explode = [0 1 0 0 0]•• pie3(x,explode)pie3(x,explode)•• colormap hsvcolormap hsv

•• Y = [1, 5, 3;Y = [1, 5, 3;•• 3, 2, 7;3, 2, 7;•• 1, 5, 3;1, 5, 3;•• 2, 6, 1];2, 6, 1];•• area(Y)area(Y)•• grid ongrid on•• colormap summercolormap summer•• colormap summercolormap summer•• set(gca,'Layer','top')set(gca,'Layer','top')•• title 'Stacked Area Plot'title 'Stacked Area Plot'

Page 35: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

Coordenadas PolaresCoordenadas Polaresz = 0:0.1:2*pi;z = 0:0.1:2*pi;

r = sin(5*z);r = sin(5*z);

polar(z,r)polar(z,r)

0.25

0.5

0.75

1

30

6090

120

150

0.25

210

240270

300

330

180 0

Page 36: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

GRACIAS POR SU ATENCIÓNGRACIAS POR SU ATENCIÓN

Page 37: Introducci n al MatLab 03s4f282e0ad17f2003.jimcontent.com/download/version...Gráficos y Ploteo en MATLAB • Ploteo básico – plot, título, xlabel , grid, legend, hold, axis •

UNSAACUNSAACLic. Guillermo Mario, Chuquipoma Pacheco

[email protected]

www.mariochuqui.jimdo.com