% Updated 20/09/16 by Wei-Cheng Wang
%
% The beginning '%' stands for 'comments' in matlab, everything
% after it is ignored by the matlab.
% When you start matlab, you are entering the ineractive mode
% of matlab, try type the following in matlab window and
% see what happens (Thee empty line (2nd line) is irrelevant,
% it makes your reading easier):
%% Free, cloned versoin of matlab: octave 
1

% now try this, the ; makes a difference
1;

% next try 
a = 1

% and 
a=2;

% Both of the above saves the variable a, the second command overwrites the
% first. if you want to know later the value of a, just type in
a

% To exit from matlab, type exit or quit.

% The most important command is 
help

% if the output of the help command is too long to fit within the screen,
% you can do the following first in a unix/linux environment and
% the output will be paged. (I don't know how this works in a MS windows)
more on
help

% one of the output line is
% matlab/general       -  General purpose commands.
% so you can try this
help general

% or you want to learn about a specific command
help plot

% We will come back to the command plot later.

% There is a mistake I made in class about the precision
help format

% it will tell you all computations in MATLAB are done in double precision.
% so MATLAB cannot be used for the Example 1.13 in homework 1
% To display 15 digits (about double precision) from now on, type in
format long;

% To switch back to your original display
format short;

% The MATLAB has most of the standard mathematical functions,
% for example
sin(1.0) + cos(2)

% The MATLAB treat variables as matrices (could be 1xn, nx1 or 1x1 matrices):
b1 = [1 2 3 4]
b2 = [1 2 0 -1]
c1 = [1; 2]
c2 = [1;-1]
d1 = [1 2; 3 4]
d2 = ones(2,2)
d3 = eye(2)

% What are ones and eyes? Well, you know how to find it out, just
% type the magic word: help xxx


% The standard operations like '*'are interpreted as matrix opearions
% while '.*' opeartion is element-wise operation
% try the following
% and 'help ops', 'help arith' for more details
b1.*b2
b1*b2   % you should see some error messages
c1*c2
b2'
b1*b2'
b1.*b2' % In old version of matlab (such as R2008a), 
        % this gives another error message (size of matrices do not match)
        % In newer version (such as R2019b, this is allowed, same as b2'*b1
b2'*b1
d1^2
d1.^2
c1*d1
d2/d1
d2*inv(d1)
d1\d2
inv(d1)*d2
d1/d2
d2\d1

%% want to solve for d1 x = c1
d1\c1  % this is the answer
% verify
d1 * d1\c1
% and this
d1 *(d1\c1)

%% try solve for y d1 = c2'

% At this point you should be able to understand the output
% of 'help plot'

% To plot y = x^2, try the following
x = 0:.1:2
x2 = 0:.2:2  % You should be able to guess the meaning of ':' here
y = x.^2;
z = 1:12
plot(x,y)
figure(2)
plot(x,y,'o')
% and guess what happens if you do
plot(x2,y)

%% To save the plot as a file in a certain directory, 
%% first switch to the target directory using the command 'cd'. 
%% For example, 
d:
cd my_directory
% this will first take you to 'drive D'
% then change to the directory 'my_directory' in drive D.

%% To save the plot as .ps file or .pdf file, use 'print'. 
%% See 'help print' for details:
print -dpsc myfig1.ps
print -dpdf myfig1.pdf

% To plot from the data, suppose you have 3 files
% fort.11, fort.12, fort.13 
% (for example, the output of your fortran routine)
% in the same directory where you run matlab,
% fort.11:
% 1
% 2
% 3
% 4
% 5
% 6
% 7
% 8
% 9
% 10
% fort.12:
% 1
% 4
% 9
% 16
% 25
% 36
% 49
% 64
% 81
% 100
% and
% fort.13:
% 1   .1
% 2   .11
% 3   .1
% 4   .11
% 5   .1
% 6   .12
% 7   .1
% 8   .11
% 9   .1
% 10  .12

% Here is how to plot the data
load fort.11 % the content is saved in a matrix called fort, 
             % after loading, matlab only uses the 'fort' part to
             % save the content of fort.11, and ignores the file
             % extension (the '11' part)
x=fort;    
load fort.12 % this overwrites fort, so you must save it as x in previous step
y=fort;
plot(x,y)

load fort.13
x1=fort(:,1); % x1 will then be the first column of fort
x2=fort(:,2); % x2 will then be the second column of fort
figure(2)     % this will pop up another window called figure 2
plot(x1,x2)

% alternatively, you can do
plot(fort(:,1),fort(:,2))

% to add a title, text, labelling of your graph or some fancy things
% See SEMILOGX, SEMILOGY, LOGLOG, GRID, CLF, CLC, TITLE,
% XLABEL, YLABEL, AXIS, AXES, HOLD, COLORDEF, LEGEND, and SUBPLOT.

%% To save time and help organiziing, you can save all your commands in
%% a file with extension .m (plotxy.m for example). Then run the command
plotxy
