Topic 2: MatLab as Calculator

Current idea is largely uploadable or cut and pastable scripts. Pain to get out and back in, so maybe best to run, then take questions, ad lib, free associate.

Demos: The Matlab Programming Environment

Command Window

Illustrate command window, help, function list icon, function completion, some calculator-like examples. Also maybe pwd, ls, cd /whatever (on windows??) cd ../ (does this work?)

Note also: Debugger and Profiler

Editor

why not just make their first script? Start in command window, file:new:script type or paste

N= 100;
xrange = linspace(0,2*pi,N);
yrange = linspace(0,25*pi,N);
lf_sine = sin(xrange);
hf_sine = sin(yrange);
figure
plot(lf_sine+ .2*hf_sine);

yrange = linspace(0,8*pi,N);
mf_sine = sin(yrange);
z= zeros(N,N);
for x = 1:N
    for y = 1:N
        z(x,y) = lf_sine(x)+.2*mf_sine(y);
    end
end

figure
surf(xrange,yrange,z);
disp('done! Over to you...')

that's pretty advanced but maybe impressive... a lot to type. Could just do stuff like

disp('average of squares of integers from 1 to 5.');
total = 1^2+ 2^2+ 3^2+ 4^2+ 5^2;
avesq = total/5

disp('add up  integers from 1 to 50.');
disp(sum(1:50));  %increments by 1 unless say otherwise

disp('another average of squares of integers from 1 to 5.');
avesq2= sum([1:5].^2) / 5  % using more built-in power

(CTRL-X) CTRL-S a useful thing: save!

Work thru the tabs and menus across top of editor...lots there, may not be worth all the detail but good to show 'em maybe

Command History Window

Workspace Window

Back Arrow to Return

Demos 2: Data Types

%demo2.m

disp('logical');
true
false
pause
if false
100
else
-999
end
pause

disp('integers');
-32
x = 987497203398726723098306736098038072309
pause
disp('conversion: usually does The Right Thing')
1/2
isreal(1/2)
pause
disp('non-zero value converts to logical true');
if -1
100
else
-999
end
pause
disp('complex nos: solve quadratic equation x = x^2 -3x +5 =0');
x= -3 + sqrt(3*3-4*1*5)/(2*1); % quadratic formula
disp(x)
pause 
disp('row and column vectors');

r1 = [1 2 3 4]
r2 = [1.5, 2.5, 3.4, 444.0]
c1 = [3; 2; 1.01]
arr = [
1 2 3
4 5 6
7 8 9]
arr2 = [9 8 7 10; 6 5 4 11; 3 2 1 12]
pause
randarr = rand(2)
weirdI= eye(2,3)
pause
disp('strings');
first='Pig';
second='-headed';
concatenation = [first second]
findstr('gattacacaggattacccaggaggattaaac','tacc')
strrep('blihblihblih','ih','ah,')

pause
%end demo2.m

Back Arrow to Return

Demos 3: Variables and Operations

% demo3.m
pause
disp('variables and operations');
clear
x
x = 1
1x = 49
IH8U! = -999
pause
num = 6 %assignment
num = num+7;  %re-assignment and access
num+8;   %no assignment 
pause
5+6
100^3
64/2/2/2
disp('etc etc');
%end demo3.m

Demos 4: Expressions

% demo4.m

pause
% what gets printed?
A = 5;
B = A;
B
A = -99;
B

% X = Y is "copy the value of Y into X" not "give a value 2 names"


Descriptive_Variable = 2*pi*Radius
neg_sqrt_x= -3  - sqrt(3*3-4*1*5)/(2*1) % quadratic formula
my_var = 4* 6^3 + x / ...
  (3* cos(.5))
% end demo4

Demos 5: Built-in Functions

% demo5.m
pause
disp('/,\');
10/2
10\2
2/10
2\10
pause
disp('precedence');
2-3-4
1+2*3
3/4*2
2^2 + 10 / 7
disp('and so on...use parens!');
pause
rem(32,6)*gcd(60,12)/sqrt(sin(abs(-pi/2)))
pause
disp('klik the function list icon!');
pause
disp('try the function( completion facility');
disp('on sqrt, linspace, rand);
% end demo5.m

Back Arrow to Return

Demos 6: Functions on variables, constants

% demo6.m
pause
clear
x1 = 1;
x2 = 2;
arr = [2 4 6 8];

who;
who x*
pause
whos;
pause
clear arr;
who;
pause
isvector(5);
arr = [2 4 6 8];
isvector(arr);
isreal(pi)
isempty(arr)
iskeyword('sin')
iskeyword('if')
isnan(0/0)
isinf(pi/0)
pause
pi
eps
pause

datevec= clock
pause
disp('note how number formatting makes for bulky vector display');
datevec = fix(clock)
date
pause
% end demo6.m

Demos 7: Matlab Numerical Datatypes (Classes)

% demo7.m

x = [-200, -129, -128, -100, -50, -1,0,3, 60, 120, 127, 128, 200]
y = int8(x)
pause
intmin('int16')
intmax('int16')
intmin('int32')
intmax('int32')
intmin('int64')
intmax('int64')
realmin('double');
realmax('double');
pause
true
false

x = [1,2,3]
x(1.0)
x(2.1)

x(false)
disp('wtf1?!')
pause
x(0)
disp('wtf2?!')
pause
x(true)
x(true+ true)
disp('wtf3?!')
pause
if (-1) 
5 
else 
-99 
end
% end demo7.m

Demos 8: Matrices and Vectors

% demo8.m
x = [1,2,3]
y = [1 2; 3, 4; 5 6]
z = [
1 2 3 4;
5 6 7 8]
ztranspose = z'
pause
v = 1:5  
w = 10:-2:-4
u = linspace(0,pi,10)
utranspose = u'
pause
zero33 = zeros(3)
zero14 = zeros(1,4)
zerolikez=zeros(size(z))
I44 =eye(4)
Iwtf = eye(2,4)
% end demo8.m

Back Arrow to Return

Demos 9: Fun with Matrices

% demo9.m
x = [1,2,3,-4]
y = [5 6 7 8]
concatxy = [x y]
sqrtx = sqrt(x)
pause
xplusy = x + y
z = [1 2;3 4]
rotz = rot90(z)
ztranspose = z'
x = x
rotxtwice = rot90(x,2)
y = y
y(7) = 3
% end demo9.m

Demos 10: Array Indexing

% demo10.m

x = linspace(1,10,10)
x3 = x(3)
x(2) = -9
x(1) = x(2)+ x(3)
x(4) = log(23)*pi*x(8)
pause
x = linspace(2,20,10)
xvecindex= x([5 4 3 2 1])
pause
x = [1:4;2:5]
x(:,1)
x(2,:)
x(:,2:3)
pause
null = []
length(null)
y =x
y(:,1) = []
x = linspace(2,20,10)
x(1:2:9) = []
pause
y = [1 2 3; 4 5 6; 7 8 9]
y(1:2,[1,3]) = [20 21; 22 23]
y(1:2,1:2) = -99
pause
x1 =x(1)
x0 = x(0)  % kee-rash!
% end demo10.m

Demos 11:

% demo11.m

x22 = zeros(2)
size(x22)
pause
x41= zeros(4,1)
size(x41)
pause
x234 = zeros(2,3,4)
size(x234)
pause
x = [1 2 3 4; 5 6 7 8; -1 -2 -3 -4]
xends = x(2:end, end)

y = size(x)
[r,c] = size(x)
% end demo11.m

Demos 12:

% demo12.m
disp('uniform')
rand2 = rand(2)
rand2 = rand(2)
pause
disp('uniform ints')
randi2 = randi(10,2,2)
randi2 = randi(10,2,2)
pause
disp('normal (Gaussian), mean=0 var=1')
randn(2)
disp('normal (Gaussian), mean=10 var=2')
10 + 2*randn(2)
pause
disp('try help random!')
% end demo12.m

Back Arrow to Return

---

Last update: 04/22/2011: RN