%;; Assignment
v = 10;            % v is a variable that has been assigned value 10
v                  % prints " v = 10"

%;; Input a number from the console
v = input('number> ');
v                  % v has the number that was input by the user


%;; Vectors
v = [2 3 4];       % the value of v is now a vector of three numbers
v                  % prints "v = 2 3 4"
w = [5 6 7];       % another vector of three numbers
x = [v w];         % concatenation of vectors
x                  % prints "x = 2 3 4 5 6 7"
x = [w v];
x                  % prints "x = 5 6 7 2 3 4"
x = v+w;           % vector addition
x                  % prints "x = 7 9 11"
x = v+3;           % addition of vector and a constant
x                  % prints "x = 5 6 7"
%[2 3 4]+[5 6 7 8] % Error: dimension of both vectors must agree
v'                 % transpose of v - prints "ans = 2
                                                    3
                                                    4"
x = v.*w;          % multiplies vectors element by element
x                  % prints "x = 10 18 28"
x = v*w';          % vector multiplication, row times column
x                  % prints "x = 56"  (due to 2*5 + 3*6 + 4*7)
x = v'*w;          % vector multiplication, column times row
x                  % prints "x = 10 12 14   
                                 15 18 21 
                                 20 24 28"  three rows and three columns
a = 1;
b = 2;
c = 3;
x = [32 22 66]     % prints "x = 32 22 66"
x(1)               % prints "ans = 32"
x(2)               % prints "ans = 22"
x(3)               % prints "ans = 66"


%;; Vectors of numbers
x = 1:10            % prints "x = 1 2 3 4 5 6 7 8 9 10"
x = 1:2:10          % prints "x = 1 3 5 7 9"
x = 10:-1:1         % prints "x = 10 9 8 7 6 5 4 3 2 1"
x = 10:-2:1         % prints "x = 10 8 6 4 2"
x = linspace(0,7,5) % prints "x = 0.0  1.75  3.5  5.25  7.0"


%;; Strings
%;;   A string is a vector of numbers between 0 and 255 interpreted as
%;;   a vector of ascii characters.  These may be shown to the user
%;;   as familiar latin characters by using certain matlab operations.
v = [72 101 108 108 111 32 87 111 114 108 100];   % v is a vector
v = char(v)        % prints "v = Hello World"
v = 'Hello World'; % characters between quote marks are a string
v                  % prints "v = Hello World"
v = '3'+45;        % addition results in a number
v                  % prints "v = 96" (the number 96)
v = '3'+'3';       % addition of two ascii characters
v                  % prints "v = 102" (the number 102)
v = 'Hello';
w = 'World';
x = [v ' ' w];     % string concatenation
x                  % prints "x = Hello World", x is a string
x = '{.'+0;        % vector addition
x                  % prints "x = 123 46"
x = '3'-'0';       % convert a character numeral to a number
y = '4'-'0';
z = x+y            % prints "z = 7"
x = num2str(345);  % converts a number to an ascii string of numerals
x                  % prints "x = 345"
x+0                % prints "ans = 51 52 53"
x = str2num('345');% converts a numeric string to a number
x                  % prints "x = 345"
x+1                % prints "ans = 346"
v = ['a' 'b' 'c']; % concatenation
v                  % prints "v = abc"
v(1)               % prints "ans = a"
v(1)               % prints "ans = b"
v(1)               % prints "ans = c"


%;; Macros
f='2*x';           % the macro
x = 2;
x = eval(f);       % evaluate 2*x with x=2
x                  % prints "x = 4"


%;; Input a string from the console
v = input('a string> ','s');
v                  % v has the string that was input

%;; Matrices
v = [1 2 3 ; 4 5 6 ; 7 8 9; 10 11 12];  % A 3x4 matrix
v                  % prints  "v =  1  2  3
                                   4  5  6
                                   7  8  9
                                  10 11 12"
x = v(1,:);        % take the first row of v
x                  % prints "x = 1 2 3"
x = v(:,1);        % take the first column of v
x                  % prints "x =  1
                                  4
                                  7
                                 10"
x = v(2,3);        % take the element in the second row, third column
x                  % prints "x = 6"


%;; Output to console
fprintf('Hello World\n');   % prints "Hello World"
v = 'Hello';
fprintf('%s World\n',v);    % prints "Hello World"
w == 'World';
fprintf('%s %s\n',v,w);     % prints "Hello World"
w = 10;
fprintf('%s = %d\n',v,w);   % prints "Hello = 10"
p = [1 2 3; 4 5 6; 7 8 9];
i = 2;
j = 1;
fprintf('p(%d,%d) = %d\n',i,j,p(i,j));  % prints "p(2,1) = 4"


%;; Booleans - simple comparisons
123 == 123             % prints "ans = 1"
123 == 405             % prints "ans = 0"
123 ~= 405             % prints "ans = 1"
123 < 405              % prints "ans = 1"
123 <= 405             % prints "ans = 1"
405 <= 405             % prints "ans = 1"
123 > 405              % prints "ans = 0"


%;; Booleans - complex comparisons
3 < 4 | 6 < 5          % prints "ans = 1"
3 < 4 & 6 < 5          % prints "ans = 0"
3 < 4 & 5 < 6          % prints "ans = 1"
1 < 2 & 2 < 3 & 3 < 4  % prints "ans = 1"


%;; Program control - the 'if' statement
if 3 < 4 fprintf('1\n'); else fprintf('2\n'); end
if 3 < 4
  fprintf('1\n');
else
  fprintf('2\n');
end


%;; Code to reverse a 2D vector whose elements are out of order
%;; Assume the name of the vector is v
if v(1) > v(2) v=[v(2) v(1)]; end


%;; Plots
x = linspace(0,2*pi,100);
plot(x,sin(x));
hold on;
plot(x,cos(x),'color',[1 .5 0]);


%;; Sums
m = 10;
x = 1:1:m;
s1 = sum(x)                      % prints "s1 = 55"
s2 = sum(x.^2)                   % prints "s2 = 385"

%;; A population model
%;; 650000000 in 2006, grows at 1% annually
%;; 650000000(1.01) -> 2007
%;; 650000000(1.01)(1.01) -> 2008
%;; 650000000(1.01)^(t-2006) -> year t
%;; Use .^ if t is a vector
figure(2);
t = 1:50:3000;
plot(t,650000000*(1.01).^(t-2006));
%;; When is (1.01)^(t-2006) = 2?
%;; When is (t-2006)*log(1.01) = log(2)?
%;; When is (t-2006) = log(2)/log(1.01)?
%;; So t = 2006 + log(2)/log(1.01) = 2006 + 69 <- doubles every 69 years

%;; for loop
for i=1:10            % prints "1 2 3 4 5 6 7 8 9 10"
   fprintf('%d ',i);
end
fprintf('\n');

%;; Find the maximum number and its position in a given list of numbers
%;; Assume the given list is a vector named v
v = input('vector of numbers> ');
if length(v) < 1 
   fprintf('Empty vector, quitting...\n');
else
   n = v(1);          % n is the value of the maximum seen so far
   pos = 1;           % pos is the position in the vector where n resides
   for i=2:length(v)
      if v(i) > n 
         n = v(i); 
         pos = i; 
      end
   end
   fprintf('The maximum number is %d at position %d\n',n,pos);
end

%;; Reverse a given list of numbers
%;; Assume the list is given as a vector named v
v = input('vector of numbers> ');
if length(v) < 1 
   fprintf('Empty vector, quitting...\n');
else
   t = [];
   for i=1:length(v) t = [v(i) t]; end
   fprintf('%d ',t);
   fprintf('\n');
end

% From the continued fraction
%  4/pi  =            1
%           1 + --------------
%                         4
%                3 + --------------
%                             9
%                     5 + -----------
%                                 16
%                          7 + ----------
%                                     25
%                               9 + ---------
%                                          36
%                                    11 + -------
%                                               49
%                                          13 + ----
%                                               ...
%
%  4/pi = 1 + 1/x -> x = 3.659.. 
%  3.659.. = 3 + 4/x -> x = 6.069...
%  6.069.. = 5 + 9/x -> x = 8.419...
%  8.419.. = 7 + 16/x -> x = 11.275..
format long
frac = 0;
for i=100:-1:0
   frac = ((i+1)*(i+1))/(frac + 2*i+3);
end
frac = frac+1;
result = 4/frac;
result