Command | |
---|---|
matlab | loads the program matlab into your workspace |
quit | quits matlab, returning you to the operating system |
exit | same as quit |
who | lists all of the variables in your matlab workspace |
whos | list the variables and describes their matrix size |
clear | deletes all matrices from active workspace |
clear x | deletes the matrix x from active workspace |
... | the ellipsis defining a line continuation is three successive periods |
save | saves all the matrices defined in the current session into the file, matlab.mat |
load | loads contents of matlab.mat into current workspace |
save filename | saves the contents of workspace into filename.mat |
save filename x y z | saves the matrices x, y and z into the file titled filename.mat |
load filename | loads the contents of filename into current workspace; the file can be a binary (.mat) file or an ASCII file |
! | the ! preceding any unix command causes the unix command to be executed from matlab |
% single-line comment
%{
multiple-line
comments
%}
% the semicolon suppresses output
a = 4;
b = 2;
ans = a + b
% note that only the result for ans is shown below
ans = 6
help — use help to lookup command usage, similar to 'man' in terminal
% help example
help clc
CLC Clear command window. CLC clears the command window and homes the cursor. See also HOME. Reference page in Doc Center doc clc
doc — use doc to open a verbose help-document that includes examples
% doc example
doc clc
clc — clears the entire command window; workspace and command history remain intact
% clc example
clc
clear — clears selected variables/answers (or all) from the workspace
% clear example
clear x % clears the variable x
clear all % clears entire workspace
home — moves insertion point to top of command window while retianing previous commands
% home example
home
who — displays the current variables in the workspace
% who example
who
whos — verbose display of variables
% whos example
whos
% output is 'loose' by default, meaning there is liberal line-spacing
a = 4
b= 2
a + b
a = 4 b = 2 ans = 6
% to change output to a tighter format
format compact
a
b
a+b
a = 4 b = 2 ans = 6
Operator | |
---|---|
+ | addition |
- | subtraction |
* | scalar/matrix multiplication |
.* | array multiplication |
/ | right division (a/b means a $\div$ b) |
|left division (a\b means b $\div$ a) | |
.|array left division | |
./ | array right division |
^ | scalar/matrix exponentiation |
.^ | array exponentiation |
: | generates regularly spaced elements; represents an entire row/column |
() | enclose function args and array indices; override precedence |
[] | enclose array elements |
, | separate statements and elements in a row |
; | separate columns and suppress output |
% | comment |
' | transpose |
= | assignment |
There are 2 ways to do this. Given a script named test_script.m:
% running script in command window
test_script
x = 2 y = 4
% differences in forward and back slash with division
a = 4;
b = 2;
a/b % a is divided by b
a\b % b is divided by a
ans = 2 ans = 0.5000
sym(expression) — the expression maintains its symbolic representation
% normal division
2/4
% sym() example
sym(2/4)
ans = 0.5000 ans = 1/2
% casting just ONE expression will cause the result to be symbolic
sym(1/2) + 2/5
ans = 9/10
% conversely, the entire computation can be put in
sym(1/2 + 2/5)
ans = 9/10
% a symbolic result can be casted back to double
double(ans)
ans = 0.9000
Variables can be assigned as symbolics:
x = sym('x')
x = x
x = sym(1/2)
x = 1/2
syms — declare variable(s) as symbolic
NOTE: the syms command is currently not working in Jupyter...
% NOTE: the syms a b c method is not working in Jupyter...
% using syms, we would write: syms x
% the alternative is to write: x = sym('x')
r = sym(3/4);
area = sym('area');
area = pi * r^2
area = (9*pi)/16
% we can make the output better looking by using pretty command
pretty(area)
9 pi ---- 16
% a more elaborate scenario:
a=sym('a');b=sym('b');c=sym('c');x=sym('x');y=sym('y');z=sym('z');
sym(x^5+742*y-z^(3))/sym(a^2+b^2-c^2)
ans = (x^5 - z^3 + 742*y)/(a^2 + b^2 - c^2)
% this is a little better...
pretty(ans)
5 3 x - z + 742 y --------------- 2 2 2 a + b - c