%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Octave (or MATLAB) commands to graph
% intersecting lines and planes.
%
% Author: M.V.Wickerhauser
% Date: 2020-08-09
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
% PLOTTING LINES
%
% Suppose we have two general line equations:
%
% -2*x + 3*y = 1
% -x - 2*y = -2
%
% To graph these in Octave (or MATLAB) we must first
% decide how to write one of the variables in terms of
% the other two, since the software requires the form
% y=f(x) with x specified and y computed, or else x=f(y).
% For this example, we choose to solve for y to get the
% slope-intercept form y=f(x)=m*x+b. This gives us
%
% y = (2/3)*x + 1/3 = f1(x)
% y = (-1/2)*x + 1 = f2(x)
%
% Next, we choose a grid of x points at which to evaluate
% the y values f1, f2:
x = linspace (-5, 5, 101)'; % 101 equispaced points in [-5,5]
% Evaluate f1, f2 to get the two lines' y-coordinates:
f1 = (2/3)*x + 1/3;
f2 = (-1/2)*x + 1;
% Plot multiple graphs on the same figure using "hold on"
clf; % [cl]ear any previous [f]igure
plot(x,f1,color="k"); % first line in blac[k]
hold on; % superpose subsequent graphs rather than erase
plot(x,f2,color="r"); % second line in [r]ed
% NOTE: the intersection point is [4/7 5/7]
%%%%%%%%%%%%%%%%%
% PLOTTING PLANES
%
% Suppose we have three general plane equations:
%
% -2*x + 3*y + z = 1
% -x - 2*y + z = -2
% 3*x - y + z = 4
%
% To graph these in Octave (or MATLAB) we must first
% decide how to write one of the variables in terms of
% the other two, since the software requires the form
% z=f(x,y) with x,y specified and z computed, or else
% y=f(x,z), or x=f(y,z). For this example, it is natural
% to solve for z since z appears with coefficient 1 in all
% three equations. This gives us
%
% z = 2*x - 3*y + 1 = f1(x,y)
% z = x + 2*y - 2 = f2(x,y)
% z = -3*x + y + 4 = f3(x,y)
%
% Next, we choose a grid of x,y points at which to evaluate
% the z values f1, f2, f3:
tx = linspace (-8, 8, 41)'; % 41 equispaced points in [-8,8]
ty = tx; % Same for y. Note that these are column vectors.
% Now create two 41x41 matrices of the x and y coordinates
[x, y] = meshgrid (tx, ty);
% Evaluate f1, f2, f3 to get the three planes' z-coordinates:
f1 = 2*x - 3*y + 1;
f2 = x + 2*y - 2;
f3 = -3*x + y + 4;
% Plot in three dimensions using "mesh()"
% Plot multiple graphs on the same figure using "hold on"
clf; % [cl]ear any previous [f]igure
mesh(tx,ty,f1); % first plane
hold on; % superpose subsequent graphs rather than erase
mesh(tx,ty,f2); % second plane
mesh(tx,ty,f3); % third plane
% Change the colors.
colormap hot % red, orange, yellow
colormap cool % blue, green, magenta
colormap list % print a list of predefined colormap names.
% NOTE: the intersection is a single point [9/7, 6/7, 1]