Вы находитесь на странице: 1из 15

GRAFICOS CON MATLAB

1.
>> x=-1:0.1:0.5;
>> y=sin(x.^2);
>> plot(x,y)

>> x=-3:0.2:3;
>> y=exp(-x.^2);
>> plot(x,y)

>> x=-3:0.2:3;
>> y=exp(-x.^2);
>> bar(x,y)
>> x=0:0.2:10;
>> y=sin(x);
>> plot(x,y)

>> x=0:0.2:10;
>> y=sin(x);
>> stairs(x,y)

>> x=0:0.1:2*pi;
>> y=sin(x);
>> polar(x,y)
>> fplot('cos(x)',[-3,3])

>> x=1:0.2:10;
>> y=11+exp(-x.^2);
>> loglog(x,y)

>> x=-3:0.2:4;
>> y=(sin(x)).^3-x;
>> plot(x,y,'o')
>> plot(x,y,'*','color','r')
>> plot(x,y,'*','color','b')
>> x=-5:0.1:5;
>> y=sin(x)-cos(x).^2;
>> plot(x,y,'*','color','b')

>> x=-2:0.2:2;
>> y=abs(cos(x));
>> plot(x,y,'*')
>>

>> x=-3:0.2:3;
>> y=-x.^2+3*x+8;
>> plot(x,y,'+')
>> plot(x,y,'--')
>> x=1:0.2:10;
>> y=cos(x);
>> plot(x,y,'--')

>> x=0:0.1:4*pi;
>> y=sin(x)./(1+cos(x));
>> plot(x,y)
>> xlabel('X'),ylabel('Y')
>> title('y=sin(x)./(1+cos(x))')
>>

>> x=-2:0.05:5;
>> y=-x.^2+sin(x);
>> h=plot(x,y);
>> text(2.7,-4,'y=-x.^2+sin(x)');
>> xlabel('valores de x');
>> ylabel('valores de y');
>> legend('CURVA X vs Y');
>> set(h,'LineWidth',2,'LineStyle','--','Color','r');
>> grid on;
>> figure;
>> plot(x,y,'-.rs','LineWidth',2,'LineStyle','--
','Color','r','MarkerEdgeColor','k','MarkerFaceColor'
,'g',...
'MarkerSize',10);
>> grid on;
MULTIPLES GRAFICOS EN UNA SOLA VENTANA FIGURE

>> x=0:0.05:5;
>> y=sin(x);
>> z=cos(x);
>> plot(x,y,x,z)

El comando hold on antes de un nuevo


comando plot podemos incrementar
graficas en una sola ventana

>> x=0:0.05:5;
>> y=sin(x);
>> z=cos(x);
>> plot(x,y,x,z);
>> hold on;
>> w=abs(x-2);
>> plot(x,w,'r')

GRAFICOS EN MULTIPLES EJES

>> t=0:0.3:5;
>> x=t.^2;
>> y=2*t-5;
>> z=sin(t);
>> u=cos(t);
>> v=abs(t);
>> w=sqrt(t);
>> subplot(3,2,1),plot(t,x),title('GRAFICO 1');
>> subplot(3,2,2),plot(t,y),title('GRAFICO 2');
>> subplot(3,2,3),plot(t,z),title('GRAFICO 3');
>> subplot(3,2,4),plot(t,u),title('GRAFICO 4');
>> subplot(3,2,5),plot(t,v),title('GRAFICO 5');
>> subplot(3,2,6),plot(t,w),title('GRAFICO 6');
ESTE PROGRAMA FUE ELABORADO EN EL
SCRIPT
%PROGRAMA PARA GRAFICAR
x=linspace(1,5,17);
y1=27*exp(-2*x);
y2=15*0.3.^x;
%plot1
subplot(2,2,1)
plot(x,y1,'*',x,y2)
%plot2
subplot(2,2,2)
semilogy(x,y1,'*',x,y2)
%plot3
subplot(2,2,3)
semilogx(x,y1,'*',x,y2)
%plot4
subplot(2,2,4)
loglog(x,y1,'*',x,y2)

>> x=-2*pi:0.2:2*pi;
>> subplot(3,1,1),ezplot('sin(x)')
>> subplot(3,1,2),ezplot('sin(2*x)')
>> subplot(3,1,3),ezplot('y=tan(x)')

% PROGRAMA QUE RETORNA EL GRAFICO DE


LAS ECUACIONES
% R1=10SIN(x),R2=-
10sin(x),R3=10sin(x),R4=-10cos(x)
x=linspace(0,2*pi,50);
R1=10*sin(x);
subplot(2,2,1)
polar(x,R1)
title('10sin(x)')
subplot(2,2,2)
R2=-R1;
polar(x,R2)
title('-10sin(x))')
subplot(2,2,3)
R3=10*cos(x);
polar(x,R3)
title('10*cos(x)')
subplot(2,2,4)
R4=-R3
polar(x,R4)
title('-10cos(x)')
% PROGRAMA QUE RETORNA EN UN
SOLO GRAFICO LAS ECUACIONES
% R1=10SIN(x),R2=-
10sin(x),R3=10sin(x),R4=-10cos(x)
x=linspace(0,2*pi,50);
R1=10*sin(x);
R2=-R1;
R3=10*cos(x);
R4=-R3;
polar(x,R1);
hold on
polar(x,R2);
hold on
polar(x,R3);
hold on
polar(x,R4);
title('[10sin(x),-10sin(x),10cos(x),-
10cos(x)] en un solo grafico')

%PROGRAMA QUE RETORNA EL GRAFICO


POLAR DE UNA ROSA EN EL RANGO DE 0 A
2PI
%LAS ECUACIONES SON: R1=3COS(nx) y
R2=3sin(nx) vs x ; para n=4,5
x=linspace (0,2*pi,200);
R1=3*cos(4*x);
subplot(2,2,1),polar(x,R1)
title('ROSA1,para R1=3*cos(4*x)')
R2=3*sin(4*x);
subplot(2,2,2),polar(x,R2)
title('ROSA2,para R1=3*sin(4*x)')
R3=3*cos(5*x);
subplot(2,2,3);polar(x,R3)
title('ROSA3,para R3=3*cos(5*x)')
R4=3*sin(5*x);
subplot(2,2,4);polar(x,R4)
title('ROSA4,para R4=3*sin(5*x)')

%PROGRAMA PARA CREAR GRAFICOS


%GUARDAR COMO grafico2.m
x=linspace(0,2*pi,40);
y1=5*cos(2.*x);
y2=3*sin(x);
plot(x,y1,'*',x,y2,'+')
xlabel('X')
ylabel('y')
title('5cos(2x) y 3sin(x) VS X')
text (0.5,4,'5cos2x')
gtext('3sin(x)')
%Script: grfico avanzado
%Script:grafico avanzado
%Guardar como graficoavanzado
x=linspace(0,3*pi/2,20);
y1=sin(x);
y2=cos(x);
plot(x,y1,'linewidth',6);hold on;
plot(x,y2,'linewidth',3,'marker','hexagr
am','markersize',20)
title('\it [sin(\alphat) & cos(\alphat)]
vs \alphat','fontsize',18)
legend('\bf sin(\alphat)','\bf
cos(\alphat)')
xlabel('\rm\alp hat','rotation',45)
ylabel('\bf magnitude')
text(3.2,0,0,'\bf
sin(\alphat)','fontsize',13,'rotation',3
0)
text(1.7,0,0,'\bf
cos(\alphat)','fontsize',15,'rotation',6
0)
box on;
grid on

El comando fplot(f(x),[xmin,xmax ymin ymax],retorna un grfico de f(x) entre los rangos


indicados, donde el nmero de puntos son automticamente elegidos por Matlab para
mostrar una buena aproximacin. Tambin retorna mltiples grficos de funciones
f1(x),f2(x)..fn(x),en el rango.

%Ejemplo del comando fplot


%GUARDAR COMO GRAFICO_FPLOT
fplot('4*cos(x)*cos(10*x)',[0 2*pi -5 5]);
title('f(x)=4*cos(x)*cos(10*x)')
xlabel('x')
ylabel('y')
COMANDO EZPLOT
ezplot(fun,[min,max])
ezplot(fun2,[xmin,xmax,ymin,ymax])

>> ezplot('x^2+2*x*y-3*x+1',[-4 4 -4 4])

%Ejemplo de fplot
fplot('[tan(x),sec(x),cot(x)]',[-pi pi -3 3])
xlabel('x');ylabel('y');
title('[tan(x),sec(x),cot(x)] vs x, para -pi<x<pi')

GRAFICO DE FUNCIONES PARAMETRICAS: X=SIN(-t)+t ; Y=1-cos(-t) ; desde 0 a 4*pi

>> t=0:0.1:4*pi;
>> X=sin(-t)+t;
>> Y=1-cos(-t);
>> plot(X,Y)
>> xlabel('x=sin(t)+t')
>> ylabel('y=1-cos(-t)')
>>
%FUNCION DEFINIDA POR
INTERVALOS
>> x=-10:.1:-5;
>> y=2+sin(x);
>> z=-5:.1:2;
>> v=log(u.^2+1);
>> u=2:.1:10;
>> t=exp(z);
>> grid on
>> plot(x,y,z,t,u,v)
>> xlabel('x'), ylabel('f(x)')
>> title('Representacion grafica de una
funcion definida por intervalos')

GRAFICOS EN 3D

plot3

>> t=-6:0.2:8;
>> x=t;
>> y=3-t;
>> z=cos(t);
>> plot3(x,y,z);
>> grid

fill3
>> t=-6:0.2:8;
>> x=t;
>> y=3-t;
>> z=cos(t);
>> fill3(x,y,z,t);
%PROGRAMA QUE RETORNA EL GRAFICO DE
Z=sin(2x).cos(2y)exp(-sqrt(x^2+y^2)/2)
%EL RANGO ES: EN X -PI a PI , EN Y -PI
a PI
x=linspace(-pi,pi,100);
y=linspace(-pi,pi,100);
[X,Y]=meshgrid(x,y);
Z=sin(2.*Y).*cos(2.*X).*exp(-
sqrt(X.^2+Y.^2)./2);
subplot(2,1,1);
mesh(X,Y,Z)
xlabel('X'),ylabel('Y'),zlabel('z')
title('Mesh Plot')
subplot(2,1,2);
surf(X,Y,Z)
xlabel('X'),ylabel('Y'),zlabel('Z')
title('Surf Plot')

Las funciones surf y mesh dibujan funciones tridimensionales en perspectiva

meshgrid: Define 2 matrices con valores


idnticos, los valores de z dependern de
los valores de estas matrices
>> [x,y]=meshgrid(-2:0.2:2);
>> z=x.*exp(-x.^2-y.^2);
>> mesh(x,y,z);
>> title('z=x.*exp(-x.^2-y.^2');
>> xlabel('x');ylabel('y');zlabel('z');
>> [x,y]=meshgrid(-2:0.2:2);
>> z=x.*exp(-x.^2-y.^2);
>> mesh(x,y,z);
>> mesh(x,y,z);
>> title('z=x.*exp(-x.^2-y.^2');
>> xlabel('x');ylabel('y');zlabel('z');

>> [x,y]=meshgrid(-3:0.2:3);
>> z=x.^2-y.^2-9;
>> meshz(x,y,z)
>> title('z=x.^2-y.^2-9');
>> xlabel('x');ylabel('y');zlabel('z');
surf:
>> [x,y]=meshgrid(-2:0.2:2);
>> z=x.*exp(-x.^2-y.^2);
>> surf(x,y,z);
>> title('GRAFICO DE Z=x.*exp(-x.^2-y.^2)');
>> xlabel('x');ylabel('y');zlabel('z');

%GRAFICO EN 3D SOMBRERO MEXICANO >> sombrero


%GUARDAR CON EL NOMBRE SOMBRERO INGRESE EL INTERVALO EN X =-10:0.5:10
x= input('INGRESE EL INTERVALO EN X =');
y= input('INGRESE EL INTERVALO EN Y ='); INGRESE EL INTERVALO EN Y =-10:0.5:10
[X,Y]= meshgrid(x,y);
Z=sin(sqrt(X.^2+Y.^2))./sqrt(X.^2+Y.^2+0
.1);
mesh(X,Y,Z)
%DADA LA FUNCION Z=16X^4+15X^2-X+6- FIGURA1
2YX^2+15Y^2-Y , SOBRE LOS RANGOS
% X:-3,3 , Y: -3,9 ,
USAR LOS COMANDOS,
%mesh,meshc,meshz,surf,surfc,waterfall
% GUARDARLO COMO
GRAFICO3D_VARIOS_COMANDOS
X=linspace(-3,3,19);
Y=linspace(-3,9,21);
[x,y]=meshgrid(X,Y);
z=16*x.^4+15*x.^2-x+6-2*y.*x.^2+15*y.^2-
y;

figure(1);
mesh(x,y,z);
box on;axis on;
xlabel('x-axis');ylabel('y-
axis');zlabel('z-axis')
title('PLOT USANDO mesh')

figure(2);
meshc(x,y,z); FIGURA2
box on;axis on;
xlabel('x-axis');ylabel('y-
axis');zlabel('z-axis')
title('PLOT USANDO meshc')

figure(3);
meshz(x,y,z);
box on;axis on;
xlabel('x-axis');ylabel('y-
axis');zlabel('z-axis')
title('PLOT USANDO meshz')

figure(4);
surf(x,y,z);
box on;axis on;
xlabel('x-axis');ylabel('y-
axis');zlabel('z-axis')
title('PLOT USANDO surf')

figure(5); FIGURA3
surfc(x,y,z);
box on;axis on;
xlabel('x-axis');ylabel('y-
axis');zlabel('z-axis')
title('PLOT USANDO surfc')

figure(6);
waterfall(x,y,z);
box on;axis on;
xlabel('x-axis');ylabel('y-
axis');zlabel('z-axis')
title('PLOT USANDO waterfall')
FIGURA5 FIGURA4

FIGURA6

Вам также может понравиться