Professional Documents
Culture Documents
ITW10
ITW10
Workshop
Graphics
Types of MATLAB Plots (2-D and 3-D Plots)
Line Plots Vector Fields
Polar Plots
y = sin(x);
plot(x,y)
Subplot
subplot(2,1,1);
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
subplot(2,1,2);
y2 = sin(5*x);
plot(x,y2)
Multiple Line Plots
x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);
figure
plot(x,y1,x,y2)
Using matrix to Plot
Y = magic(4)
figure
plot(Y)
Specifying Line Style
x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);
figure
plot(x,y1,x,y2,'--',x,y3,':')
Specifying Line Style, Color, and Marker
x = 0:pi/10:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);
figure
plot(x,y1,'g',x,y2,'b--o',x,y3,'c*')
Markers at Specific Data Points
x = linspace(0,10);
y = sin(x);
plot(x,y,'-o','MarkerIndices',1:5:length(y))
Specifying Line Width, Marker Size, and
Marker Color
x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
figure
plot(x,y,'--gs',...
'LineWidth',2,...
'MarkerSize',10,...
'MarkerEdgeColor','b',...
'MarkerFaceColor',[0.5,0.5,0.5])
Add Title and Axis Labels
x = linspace(0,10,150);
y = cos(5*x);
figure
plot(x,y,'Color',[0,0.7,0.9])
xlabel('x')
ylabel('cos(5x)')
Polynomial Curve Fitting
x = [1 2 3 4 5];
p = polyfit(x,y,3)
x2 = 1:.1:5;
y2 = polyval(p,x2);
plot(x,y,'o',x2,y2)
grid on
text(2,400,s)
Interpolation
x = 0:pi/4:2*pi;
v = sin(x);
xq = 0:pi/16:2*pi;
figure
vq1 = interp1(x,v,xq);
plot(x,v,'o',xq,vq1,':.');
xlim([0 2*pi]);
st = sin(t);
ct = cos(t);
plot3(st,ct,t)
Meshgrid in Plotting
x = -2:0.25:2;
y = x;
[X,Y] = meshgrid(x);
F = X.*exp(-X.^2-Y.^2);
surf(X,Y,F)
Mesh Plot
[X,Y] = meshgrid(-8:.5:8);
Z = sin(R)./R;
mesh(X,Y,Z)
Surface Plot
[X,Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
C = X.*Y;
surf(X,Y,Z,C)
colorbar
Histogram
x = randn(10000,1);
nbins = 25;
h = histogram(x,nbins)
Scatter Plot
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
scatter(x,y)
Bar Plot
x = 1900:10:2000;
bar(x,y)