% CLASS21OCT   in-class Octave session on 21 Oct, Math 310, Bueler
% covers plotting lines and surfaces, and matrix/vector manipulations

% plot intersecting lines
x = -5:5;  y1 = (7 - 3*x)/(-2);  y2 = -8*x;
figure(1)  
plot(x,y1,x,y2)
grid on
axis([0 0.5 -5 0])

figure(2)  % show that we just have points
plot(x,y1,'o',x,y2,'*')

% set-up and solve a system
A=[3 -2; 8 1], b = [7; 0]
A\b

% plot a plane as a surface
[x y]=meshgrid(-2:3,-2:3);  % what sizes are x,y?
figure(3)
surf(x,y,7-3*x+2*y)

% plot with more decorations
figure(4)
z = 7-3*x+2*y;  % what size array is z?
surf(x,y,z)
xlabel('x axis by ed')
ylabel('y (m)')
zlabel('z (light years)')
title('foo')

% how to change axis and viewpoint:
%axis([-100 100 -100 100 -100 100])
%view(2)  % from-above view
%view(3)  % default 3D view

% create matrix and a vector, get entries, do some operations related to system A v = b
A = magic(4);
A(1,1)
A(1,3)
b = randn(4,1);
A*b
A\b  % GIVES "matrix singular to machine precision" WARNING for this BAD matrix
% does not work at all:  b*A

% operations on a single matrix
A*A
A^2
A.^2
A'  % transpose

% extract some parts of A
A(1:3,3:4)
A([1 4],3:4)
A([1 4],:)
A(:,[1 4])

% augment
[A b]

% special matrices
eye(3,3)
zeros(3,5)

% create a matrix and alter some of its entries
B=zeros(3,5);
B(1,3)=7; B(3,5)=-2; B(:,1)=[3 4 5]';
B

% functions of matrices
M = rand(4,4);
det(M)
inv(M)
rref(M)

% just for fun:  answering "is a random 80x80 matrix invertible?" by picture
B = rand(80,80);
figure(5)
surf(B*inv(B))

