%-------------------------------------------------------------------------------
% lecture5-?
%-------------------------------------------------------------------------------
% Notation: 
% + The percent symbol (%) means that MATLAB treats everything that follows as a
%   comment which is ignored by MATLAB.
% + Anything surrounded by dollar signs ($something$) means a portion of MATLAB
%   or a MATLAB command that you would enter.
% + you might wish to uncomment one of the following two lines:
% more on
% more off
% + menus: "->" means selecting a submenu
%   example) File->Set Path means "click on File menu then click on Set Path
%            submenu"
% + CW means "Command Window"
% + MF means "M-File"
%-------------------------------------------------------------------------------
% Handy commands for programming
  % pause % I am assuming you're trying to run this MATLAB
          % $pause$ pauses processing of the MF until you hit any key
          % no, there's no "Any" key :-)
  % echo  % repeat everything in the MF that was run
          % the CW still evaluates everything in the MF
% more neat stuff:
% + miscellaneous: see $keyboard$, $pause(n)$, $waitforbuttontopress$
% + input:         $input$
% + output:        $disp$
%-------------------------------------------------------------------------------
% Programming
% + automating problem solving
% + what does a program look like?
%
%   Input -> Block Box -> Output
%            ^^^^^^^^^
%            program
%
%   - input comes from user, other program, model (monitoring)
%   - output goes to user, screen, file, printing, model (control)
%   - black box -- the code hidden from users
%   - but the program could include all 3
%-------------------------------------------------------------------------------
% Example1
% --------
% World's most common intro program

  % Print Hello, World! to user
  disp(['Hello, World!'])   % display output
  pause                     % pause for keystroke

  % explanation: $disp$: display an array of strings
  %                      - 'Hello, World!' is a string
  %                      - ['Hello, World!'] is an array containing one string
  %                      - disp(['Hello, World!']) tells MATLAB to display
  %                        'Hello, World!' in the Command Window
%-------------------------------------------------------------------------------
% Example2
% --------
% Let's clean that up a bit for the MATLAB interface.
% "cleaning up" here means making the program a bit more user-friendly.
% Example 1 is called "bare-bones" because little is explained to the user.

  % Print Hello, World! to user
  clc                       % clear screen
  disp(['Running a classic program....'])
  disp(['Hello, World!'])
  disp(['Hit any key when ready to continue'])
  pause
%-------------------------------------------------------------------------------
% Example3
% --------
% Now let's be a bit fancier.
% Calculator-type program
% Ex2 determines the percent error for a measured quantity
% compared to an ideal value.

  % Determine percent error
    clc
    disp(['Hello, user. I am about to calculate a percent error.'])
 
  % Get measured and ideal values
    measured = 11; % a measured value
    ideal = 10;    % an ideal, or perfect) value
  
  % Formula to determine percent error
  % For absolute value, use ABS function: abs(value)
    percenterror = 100*abs(measured-ideal)/ideal;
 
  % Output to user
    disp(['The percent error is ' num2str(percenterror)])
    disp(['Hit any key when ready to continue'])
    pause

  % notes
  % -----
  % + program is a succession of statements
  % + MATLAB processes each statement one at a time
  %   (technically, MATLAB interprets each statement)
  % + the code works left to right, top to bottom
  %   (known as control-flow or flow of control -> the fact that 
  %    the software executes in a controlled fashion)
  % + I've attempted to document each "important" variable
  % + $num2str$ converts the numerical value of $percenterror$ to
  %   a string. $disp$ displays an ARRAY of strings, so I left a space
  %   between the first and second strings
%-------------------------------------------------------------------------------
% Example4
% --------
% The last program involved a black box and output.
% It would be more fancy (and useful) if the user could choose the values
% used in evaluating percent error.
% Use $input$ command...demonstrated below, then explained more in Ex5

  % Determine percent error
    clc
    disp(['Hello, user. I am about to calculate a percent error.'])

  % Obtain input from user
    measured = input('Enter a measured value: ');
    ideal = input('Enter the ideal value: ');
  
  % Formula to determine percent error
  % For absolute value, use ABS function: abs(value)
    percenterror = 100*abs(measured-ideal)/ideal;
 
  % Output to user
    disp(['The percent error is ' num2str(percenterror)])
    disp(['Hit any key when ready to continue'])
    pause

  % notes
  % -----
  % How does $input$ work?
  % $input(string)$ causes MATLAB to prompt the user for input
  % + a prompt is text output that tells the user what to do
  % + the $input$ command outputs the string as the prompt
  % MATLAB then waits for the user to input a value or array
  % (an array would be entered with brackets [])
  % That input value is then stored in the variable on the left side.
  % Try picturing that the input value from the user replaces the entire
  % code on the right side of the assignment. 

%-------------------------------------------------------------------------------
% Example5
% --------
% More practice using $input$
  
  clc
  disp(['Now demonstrating more about input....'])

  echo

  % test1)
    input('Enter it! ')

  % In test1 MATLAB waits for the user input.
  % The user types a value and presses Enter.
  % MATLAB replaces "input('Enter it! ')" with the value you entered.
  % So, MATLAB reports "ans = <the value you entered>"
  % Notice how I put a space in the string because the input appears 
  % immediately to the right of the string.

  pause
  clc
  'Next test...';
  
  % test2)
    L = input('R: ')
    L

  % In test2 I'm using variables L and R to remind you about what's on 
  % the left and right, respectively. I enter L AGAIN so MATLAB can prove 
  % to you that L is assigned

  pause
  clc
  'Last test...';
  % test3)
    L = input('R: ');

  % In test3, do you see the semicolon? Why on Earth did I do that?
  % MATLAB evaluates the right-side as your input value.
  % Any time you enter a value, what does MATLAB do? Evaluates it.
  % So, the semicolon suppresses the output of assigning the input to 
  % the variable on the left.

  echo
  pause


% more to come next time....
