Some plots in Matlab

This assumes you've gotten matlab started and are staring at a matlab prompt. You can copy and paste text into the matlab window. Do that with each block of statements, one block at a time, so you can see the results of each plot.

%%  make a row vector from -3 to 3
%% you can see the contents of xvals by typing 'xvals' with no trailing ';'
xvals = -3:.25:3;

%% make a row vector of gaussian densities.
yvals = 1/(sqrt(2*pi))*exp(-(xvals.^2)/2);

%%%% The FOR loop below produces the same result as the statement above
%% for i = 1:length(xvals);
%%    yvals(i) = 1/(sqrt(2*pi))*exp(-(xvals(i)^2)/2);
%% end;

%% make a simple 2D plot
plot(xvals, yvals);

%% dress it up a little
clf;
h = axes('position',[.2 .1 .6 .8]);  %% also try "help subplot"
set(h,'fontsize', 7);	  %% type "set(h)" to get huge list of options %%
plot(xvals, yvals), axis([-3.1 3.1 0 .5]), grid on;
xlabel('Standard Deviations'), ylabel('Probablility Density'),
legend('The curve'), text(-.4, .42, 'The peak');
   title('Your Basic Gaussian Density')

%%  make another row vector
zvals = log10(abs(yvals))/2;

%% simple 2-color plot
plot(xvals, yvals, ':', xvals, zvals, '--');
legend('The curve', 'Its log');

%% Clear, then put three plots on the same graph
clf;
plot(xvals, yvals,'-'), hold on,
plot(xvals, zvals,'o'), hold on,
plot(xvals, xvals/7,'+'), hold off;

%% save it in a file in postscript format
print -deps killmeplot.ps 

%% example from 'help quiver'
xord = -2:.2:2;
yord = -2:.2:2;
[x,y] = meshgrid(xord,yord);
z = x .* exp(-x.^2 - y.^2);
[px,py] = gradient(z,.2,.2);
contour(x,y,z),hold on, quiver(x,y,px,py), hold off

%% one kind of 3D plot
plot3(x,y,z), grid on;
% spin it with the mouse
rotate3d on;

%% another kind of 3D plot
mesh(xord, yord, z), view(10,20);
rotate3d on;


lcampbellweb@media.mit.edu