Matlab Practiced

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

>> x=-10:0.

2:10;

>> y=1/2*sin(2.*x)./x;

>> plot(x,y),xlabel('x'),ylabel('sin(2x)/2x'),grid

>> x=-10:0.2:10;

>> x=x+(x==0)*eps;

>> y=1/2*sin(2.*x)./x;

>> plot(x,y),xlabel('x'),ylabel('sin(2x)/2x'),grid
Axes of plots (page 411)

>> x=-10:0.2:10;

>> x=x+(x==0)*eps;y=1/2*sin(2.*x)./x;

>> plot(x,y),xlabel('x'),ylabel('sin(2x)/2x'),axis([-10 10 -0.4 1.2]),grid

Parametrically Defined curve

>> t=0:0.02:2*pi;

>> plot(cos(3*t),sin(2*t)),axis('equal'),grid
Setting line and mark types and colors

>> t=0:0.04:2*pi;

>> x=t.*cos(3*t);

>> y=2.*t.*sin(2*t);

>> plot(x,y,'o'),title('Parametriic contour'),grid

>> t=0:0.04:2*pi;

>> x=t.*cos(3*t);

>> y=2.*t.*sin(2*t);

>> plot(x,y,'o'),title('Parametriic contour'),grid

>> close all

>> plot(x,y,'og'),title('Parametric Contour'),grid


Histograms, Bar charts and stem diagrams

>> x=1:6;

>> y=zeros(1,6);

>> for i=1:600;

ii=ceil(6*rand(1));

y(1,ii)=y(1,ii)+1;

end

>> bar(x,y),xlabel('dice number'),ylabel('scores'),axis([0 8 0 125]),grid

>> x=1:6;

>> y=zeros(1,6);

>> for i=1:600;

ii=ceil(6*rand(1));

y(1,ii)=y(1,ii)+1;

end

>> stem(x,y),xlabel('Dice number'),ylabel('scores'),title('Stem plot'),axis([0 8 0 125])


Multiple Curves

>> x=0:0.01:2*pi;

>> y1=sin(x);

>> y2=sin(2*x);

>> y3=sin(4*x);

>> plot(x,y1,'b',x,y2,'dr',x,y3,'+k'),xlabel('x'),ylabel('y1,y2,y3'),title('Multiple curves'),grid


>> x=0:0.01:2*pi;

>> y1=sin(x);

>> a=y1';

>> y2=sin(2*x);

>> b=y2';

>> y3=sin(4*x);

>> c=y3';

>> y=[a,b,c];

>> plot(x,y),xlabel('x'),ylabel('ys'),title('Multiple curves'),grid

>> x=0:0.01:2*pi;

>> y1=sin(x);

>> y2=sin(2*x);

>> y3=sin(4*x);

>> plot(x,y1),hold,plot(x,y2,'dk'),plot(x,y3,'or'),xlabel('x'),ylabel('ys'),title('Multi Curves'),grid

Current plot held


Three Dimensional Plots

>> data=[2.5 1.3 0;

0 2 0;

1 3 0;

2.5 3.5 4;

3 1 -2;

2 -1 -2;

3.5 4 -2.5;

0 1 0]

data =

2.5000 1.3000 0

0 2.0000 0

1.0000 3.0000 0

2.5000 3.5000 4.0000

3.0000 1.0000 -2.0000

2.0000 -1.0000 -2.0000

3.5000 4.0000 -2.5000

0 1.0000 0

>> plot3(data(:,1),data(:,2),data(:,3),'o'),xlabel('x'),ylabel('y'),zlabel('z'),axis([-1 4 0 5 -5 5]),grid


Mesh Plot

>> xx=-10:0.4:10;

>> yy=xx;

>> yy=yy+(yy==0)*eps;

>> [x y]=meshgrid(xx,yy);

>> z=(x.^2+y.^2).*sin(y)./y;

>> mesh(xx,yy,z),xlabel('x'),ylabel('y'),zlabel('z'),title('Mesh Plot'),grid

Contour Plot

>> xx=-10:0.4:10;

>> yy=xx;

>> yy=yy+(yy==0)*eps;

>> [x y]=meshgrid(xx,yy);

>> z=(x.^2+y.^2).*sin(y)./y;

>> mesh(xx,yy,z),xlabel('x'),ylabel('y'),zlabel('z'),title('Mesh Plot'),grid


>> contour(xx,yy,z)

Combined Surface and contour plot

>> xx=-10:0.4:10;

>> yy=xx;

>> yy=yy+(yy==0)*eps;

>> [x y]=meshgrid(xx,yy);

>> z=(x.^2+y.^2).*sin(y)./y;

>> mesh(xx,yy,z),xlabel('x'),ylabel('y'),zlabel('z'),title('Mesh Plot'),grid

>> surfc(xx,yy,z)
>> x=-2:0.01:2;

>> y=-2:0.01:2;

>> z=exp(-x.^2-y.^2).*sin(y).*sin(y);

>> plot3(x,y,z),title('3D plot'),grid

>> x=-10:0.01:10;

>> y=-10:0.01:10;

>> z=(4.*sin(x).^3.*sin(y).^3)+0.5;

>> plot3(x,y,z),title('3D function curve'),grid


Function Example Script Output

fplot >> fplot(@(x)x.*sin(x),[0 10*pi])

semilogx
>> t=linspace(0,2*pi,200);
>> x=exp(-t); y=t;
>> semilogx(x,y),grid

>> t=linspace(0,2*pi,200);
semilogy >> semilogy(t,exp(t)),grid

>> y=exp(2*t)+100;
loglog >> loglog(x,y),grid

>> t=linspace(0,2*pi,200);
polar >> r=sqrt(abs(2*sin(5*t)));
>> polar(t,r)
>> t=linspace(0,2*pi,200);
fill >> r=sqrt(abs(2*sin(5*t)));
>> x=r.*cos(t);
>> y=r.*sin(t);
>> fill(x,y,'k'),axis('square')

>> t=linspace(0,2*pi,200);
bar >> r=sqrt(abs(2*sin(5*t)));
>> y=r.*sin(t);
>> bar(t,y),axis([0 pi 0 inf]);

>> x=0:0.1:2;
errorbar >> aprx2=x-x.^3/6;
>> er=aprx2-sin(x);
>> errorbar(x,aprx2,er)

hist >>
cont=char('Asia','Europe','Africa','N.A
merica','S.America');
>> pop=[3332;696;694;437;307];
>>
barh(pop),xlabel('Population'),title('W
orld Population','fontsize',18),for
i=1:5,gtext(cont(i,:));end

>> x=1:0.1:10;
>> y1=exp(-x).*sin(x);
>> y2=exp(x);
plotyy >>
Ax=plotyy(x,y1,x,y2),hy1=get(Ax(1),'yla
bel'),hy2=get(Ax(2),'ylabel');set(hy1,'st
ring','e^-sin(x)'),set(hy2,'string','e^x')
>> x=linspace(-3*pi,3*pi,100);
>> y=-sin(x)./x;
>>
area(x,y),xlabel('x'),ylabel('sin(x)./x'),
area hold
on,x1=x(46:55);y1=y(46:55);area(x1,y1
,'facecolr','y')

>>
cont=char('Asia','Europe','Africa','N.A
pie merica','S.America');
>> pop=[3332;696;694;437;307];
>> pie(pop),title('World
Population','fontsize',18),for
i=1:5,gtext(cont(i,:)),end

>> y=randn(50,1);
hist >> hist(y)

stem >> t=linspace(0,2*pi,200);


>> f=exp(-0.2*t).*sin(t);
>> stem(t,f)
compass
>> th=-pi:pi/5:pi;
>> zx=cos(th);
>> zy=sin(th);
>> z=zx+i*zy;
>> compass(z)

stairs >> t=linspace(0,2*pi,200);


>> r=sqrt(abs(2*sin(5*t)));
>> y=r.*sin(t);
>> stairs(t,y),axis([0 pi 0 inf])

>> r=-5:0.2:5;
>> [x y]=meshgrid(r,r);
contour >> z=-5*x.^2+x.*y+y.^2;
>> cs=contour(x,y,z),clabel(cs)

>> r=-2:0.2:2;
>> [x y]=meshgrid(r,r);
Quiver >> z=x.^2-5*sin(x.*y)+y.^2;
>> [dx dy]=gradient(z,0.2,0.2);
>> quiver(x,y,dx,dy,2)
Fill3 >> x=[0 0 0 0;1 1 -1 1;1 -1 -1 -1];
>> y=[0 0 0 0;4 4 4 4;4 4 4 4];
>> z=[0 0 0 0;1 1 -1 -1;-1 1 1 -1];
>> fill3(x,y,z,rand(3,4)),view(120,30)

>> t=linspace(0,1,100);
>> x=t;y=t.^2;z=t.^3;
plot3 >> plot3(x,y,z),grid

>> r=linspace(-3,3,50);
contour3 >> [x,y]=meshgrid(r,r);
>> z=-5./(1+x.^2+y.^2);
>> contour3(z)

sphere >> [x,y,z]=sphere(20);surf(x,y,z)

You might also like