% matnotes is a matlab script containing lots of examples of using Matlab.
% It corresponds very closely to what was presented in the Matlab lecture.
echo on

% mathematical operations on numbers
2+3
2*3
2/3
2^3

% mathematical operations on arrays (vectors)
[1,2] + [3,4]
[1,4,6,4,7] - [1,2,5,4,6]

% common mistakes
%[1,4,6,3,5] - [2,7]
%     vector dimensions almost always must agree! (scalars are special)
%     however, these are legal:  
[1,4,6,3,5] - 1
2*[1,4,6,3,5]

%[1,2]*[3,4]
%[1,2]/[3,4]
%[1,2]^[3,4]
%[1,2]^2
%     these don't work because Matlab interprets * as matrix multiplication (i.e. dot product 
%     when two vectors, one of which is transposed), etc.  If we want to apply the operator
%     *, /, ^ etc to corresponding entries in the two vectors, we use the pairwise operators
%     .*, ./, .^
[1,2].^2
[1,2].*[3,4]
[1,2]./[3,4]
[1,2].^[3,4]
  
% variables
x=3
y=4
x
x+y
z=x+y
z
% ; is used at the end of a line to prevent MATLAB from printing the answer to the screen
z=x+y;
z;
z
% clear gets rid of all the current variable values
clear
%x
x=[1,2]
x+[3,4]

% making vectors
x=[1:5]
x=[0:0.3:1.5]
x=[1:2:5]
n=9;
x=[1:2:n]
2*x
% getting entries -- index starts at 1, unlike in Java, where it starts at 0!
x(1)
x(2)
length(x)
x(length(x))

% elementary functions defined in Matlab
help elfun
cos(0)
th=[-pi:pi/6:pi];
cos(th)
y = 3*cos(th).^2

% for loops -- syntax is different than Java!
x=[1,2,3,4];
y=0;
for i = 1:10
	y=y+x;
end

% plotting 2D graphs
x
y
plot(x,y);
title('lame title');
xlabel('x');
ylabel('f(x)');
plot(th,cos(th));
% there are lots of things you can do to make plots prettier
%help plot
plot(th,cos(th),'r+');
% note that plot just plots vectors that you've already generated.  
% It's up to you to use enough different values so that the curve that gets drawn looks
% like the real thing
th=[-pi:pi:pi]
plot(th,cos(th))


% saving current plot.
% (I said to save to .eps file in the homework, but a .bmp file is fine, too.  I'll show
% how to do both).
% 	generate an encapsulated postscript file
print -deps cosplot.eps
% 	generate a Windows Bitmap file (this doesn't work on Unix machines)
print -dbitmap cosplot.bmp
% 	can also go through Export in the file menu
% dir


% plotting 3D graphs
x=[-10:10];
y=[-10:10];
[X,Y] = meshgrid(x,y);
X
Z=X.^2 + Y.^2;
mesh(X,Y,Z);
mesh(X,Y,-Z);
surf(X,Y,Z);
print -dbitmap 3Dplot.bmp
% dir

% script files.  
% any commands you could type at a MATLAB prompt you can put in a text file whose name ends with .m
% If you put that file in your MATLAB working directory, you can type that filename from MATLAB
% (without the .m at the end), and it will run those commands for you.
% You can find your MATLAB working directory by going to File->Set Path in your MATLAB window.
% For the Upson Basement machines, the working directory is:
%	C:\Program Files\MATLABR11\work\
simpleplot
% You can put comments in your .m files by having lines start with %
% If you have comments at the beginning of your script file, they will be displayed if you type
% help filename (where filename is the name of your .m file, without the .m).
% It's always a good idea to put some help comments at the top of your .m files!

help simpleplot

help myfactorial
% echo can be useful, to let you see what's happening inside a script
% also, pause can be useful
myfactorial

% functions.  You can define your own functions in MATLAB.  Each function has to be in is own .m
% file, and the file needs to have a line at the top of the following format:
% 	function y = f(x)
% A function can have any number of input parameters.  It can also return multiple values, e.g.
% 	function [y,z] = f(x,a,b)
% NOTE: you don't need to declare types for your input and output parameters!  This is different
% from in Java.
% Help comments work the same way for functions
help sillyfact
%sillyfact
sillyfunc([0,1,2,10],3)
n=5;
x=[0:10];
sillyfunc(x,5)

% NOTE: you can call functions from inside other functions or scripts!
help showsillyfuncs
showsillyfuncs(5,'here they is');






