% another highly organized way of getting help for matlab is to use the
>> helpdesk

% e.g., -> matlab graphics -> basic plotting

% also stressed the recipe  A = X(b,c)  for making a new matrix from the
% rows and columns of a given one, with  b  and  c  vectors of natural numbers
% such that all the entries of  b   are from the sequence 1:size(X,1), and
%           all the entries of  c   are from the sequence 1:size(X,2),
% and with certain abbreviations permitted:
% e.g., X(:,c) is the same as X(1:end,c) which is, in turn, the same as 
% X(1:size(X,1),c)
% etc.
% E.g., X(:,1) is the first column of  X , and X(end,:) is the last row of X.

% finish the discussion of computing and plotting a weighted sum of sin(nx)
terms

>> x = linspace(-10,10);
>> plot(x,sin(x*(1:4))*[2;3;7;5])
??? Error using ==> *
Inner matrix dimensions must agree.

% forgot the prime at the end of the statement for x, hence x is now a 
% row vector, and that makes it impossible for matlab to understand the
% next command, where it says  x*(1:4). Whenever we multiply two matrices, the
% the column number of the first factor must equal the row number of the
% second. 
% the only exception in matlab occurs when one of the factors is a scalar,
% i.e., a 1-by-1 matrix.

% Now, rather than turning x into a column vector, by the command  x = x',
% I simply make it a column vector only where it matters:

>> plot(x,sin(x'*(1:4))*[2;3;7;5])

% ... and this works fine. Note that PLOT(x,y) works fine if one is a row
% vector and the other is a column vector, as long as they have the same length.
% If they don't, there is trouble:

>> plot(x(1:end-1),sin(x'*(1:4))*[2;3;7;5])
??? Error using ==> plot
Vectors must be the same lengths.

%  Next, generating and plotting two curves at once:

>> plot(x,sin(x'*(1:4))*[2 8;3 2;7 6;5 9])

%  Next, the updown sequence: I discussed the math, then looked at the script

% (first, I copied the script from an htm file I got from the book's 
% web page, but since I couldn't copy it in one piece, I got into trouble.
% So, I simply took it from the current directory where I had placed it
% earlier)

>> type Updown

% Script File: UpDown
% Generates a column vector x(1:n) of positive integers 
% where x(1) is solicited and
% 
%          x(k+1) = x(k)/2    if x(k) is even.
%          x(k+1) = 3x(k)+1   if x(k) is odd.
%
% The value of n is either 500 or the first index with the
% property that x(n) = 1, whichever comes first.

x = zeros(500,1);
x(1) = input('Enter initial positive integer:');
k = 1;
while ((x(k) ~= 1) & (k < 500))
   if rem(x(k),2) == 0
      x(k+1) = x(k)/2;
   else
      x(k+1) = 3*x(k)+1;
   end
   k = k+1;
end
n = k;
x = x(1:n);

clc
disp(sprintf('x(1:%1.0f) = \n',n))
disp(sprintf('%-8.0f',x))
[xmax,imax] = max(x);
disp(sprintf('\n x(%1.0f) = %1.0f is the max.',imax,xmax))
density = sum(x<=x(1))/x(1);
disp(sprintf(' The density is %5.3f.',density))

close all
figure
plot(x)
title(sprintf('x(1) = %1.0f, n = %1.0f',x(1),n));
figure
plot(-sort(-x))
title('Sequence values sorted.')
I = find(rem(x(1:n-1),2)==1);
if length(I)>1
   figure
   plot((1:n),zeros(1,n),I+1,x(I+1),I+1,x(I+1),'*')
   title('Local Maxima')
end


% I stressed various commands:  while, ..., end, if ..., else ... end,
% relational ops: ==, ~=, <=, >=, <, >,  &, |, ~  for expressing conditions
% ops that input vectors and output numbers: max, sum, ...
% also ops from vectors to vectors: sort, ...
% and noted that some may have additional optional output, like max and sort
%
% Also stressed ops good for displaying stuff, like
% disp(a)   for displaying the value of a,  and
% sprintf(<format>,var1,var2,...)   for generating a string according to the
% specs given by <format> that may include format statements for the display
% of the values of var1, var2, ....

>> updown
Enter initial positive integer: 1023
x(1:63) = 

1023    3070    1535    4606    2303    6910    3455    10366   5183    15550   7775    23326   11663   34990   17495   52486   26243   78730   39365   118096  59048   29524   14762   7381    22144   11072   5536    2768    1384    692     346     173     520     260     130     65      196     98      49      148     74      37      112     56      28      14      7       22      11      34      17      52      26      13      40      20      10      5       16      8       4       2       1       

 x(20) = 118096 is the max.
 The density is 0.034.

% Finally, I began discussion of the use of random numbers, both uniformly and
% normally distributed.
diary off
