% The beginning '%' stands for comment 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): 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' % another error message d^2 d.^2 c1*d1 % 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; plot(x,y) plot(x,y,'o') % and guess what happens if you do plot(x2,y) % 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, see help load 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,y1) % 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.