Code and Output

Code 2.3.1:

>> A = 2

Output 2.3.1:

A =
2

Code 2.3.2:

>> 2 = A

Output 2.3.2:

??? 2 = A
Error: The expression to the left of the equals sign is not a valid target for an assignment.

Code 2.3.3:

>> A

Output 2.3.3:

A =
2

Code 2.3.4:

>> a

Output 2.3.4:

??? Undefined function or variable 'a'.

Code 2.3.5:

>> a = 3

Output 2.3.5:

>> a =
3

Code 2.3.6:

>> My_Difference = a - A

Output 2.3.6:

My_Difference =
1

Code 2.3.7:

>> for = 4

Output 2.3.7:

??? for = 4;
|
Error: The expression to the left of the equals sign is not a valid target for an assignment.

Code 2.3.8:

>> a + A

Output 2.3.8:

ans =
5

Code 2.3.9:

>> ans

Output 2.3.9:

ans =
5

Code 2.4.1:

>> My_Difference;

Output 2.4.1:

>>

Code 2.4.2:

>> My_Difference_2 = A - My_Difference;

Output 2.4.2:

>> 

Code 2.4.3:

>> My_Difference_2

Output 2.4.3:

My_Difference_2 =
1

Code 2.5.1:

>> A = 5;
>> B = 3;
>> C = A + B;  %Oops! no output

Output 2.5.1:

>>

Code 2.5.2:

>> C = A + B   %restored by one up-arrow, then ';' deleted

Output 2.5.2:

C =
8

Code 2.5.3:

>> A = 15;    %restored by 4 up-arrows, then 5 changed to 15
>> B = 13;    %restored by 4 up-arrows, then 3 changed to 13
>> C = A + B  %Short cut! restored by typing C then one up-arrow

Output 2.5.3:

C =
28

Code 2.6.1:

% My_Program_01
% A program to add two numbers called X and Y.

X = 10;
Y = 12;

Z = X + Y

Output 2.6.1:

Z =
22

Code 2.6.2:

% Continuation_Illustration

Method_1_Score_1 = 899;
Method_2_Score_1 = 1286;

Method_1_Score_2 = 1018;
Method_2_Score_2 = 1344;

Method_1_Score_3 = 1167;
Method_2_Score_3 = 1389;

Summed_Differences_Between_Method_2_and_Method_1_Scores = ...
  Method_2_Score_1 - Method_1_Score_1...
+ Method_2_Score_2 - Method_1_Score_2...
+ Method_2_Score_3 - Method_1_Score_3

Output 2.6.2:

Summed_Differences_Between_Method_2_and_Method_1_Scores =
   935

Code 2.8.1:

% My_Program_3
diary('My_Program_3_diary.txt')
a = 1 + 2 + 3 + 4;
b = 1 + 4 + 9 + 16;
disp('The sum and sum of squares of the four integers is:')
disp(a);
disp(b)
diary off

Output 2.8.1:

The sum and sum of squares of the four integers is:
10
30

Code 2.8.2:

>> type My_Program_3_diary.txt

Output 2.8.2:

The sum and sum of squares of the four integers is:
10
30

Problem 2.9.9:

% My_Program_03
a
b = a + 1
c = = b - 2
d b x c
e = b divided by c

Solutions

% Solutions_Chapter_02

% Solutions for selected problems from MATLAB for Behavioral Scientists,
% Second Edition (D. A. Rosenbaum, J. Vaughan, & B. Wyble),
% (c) 2015, Taylor & Francis

% To generate the solution for one problem, copy and run the code for that
% problem in a file or paste it into the Command window. Show the Command
% window to see the results.

% To generate sll the solutions for Chapter 2, save this code as a
% MATLAB script file and run the program.

% Problem 2.9.1
% Open MATLAB's Command window and get today's date.
clc
commandwindow
fprintf('\n\n          %s\n\n','Output 2.9.1')
date

% Problem 2.9.2
% In MATLAB's Command window, get this month's calendar.
fprintf('\n\n          %s\n\n','Output 2.9.2')
fprintf('Solution left to the student.\n\n');

% Problem 2.9.3
% Next, look at the calendar for a year ago this month. Hint: Although
% nothing you have read in this chapter tells you directly how to do this,
% there was mention of help.
fprintf('\n\n          %s\n\n','Output 2.9.3')
help calendar
calendar(2013,9)   % A year prior to September 2014

% Problem 2.9.4
% Find out what time it is using MATLAB by getting help about clock. If
% you first execute the command format bank, the output of clock will be
% most readable.
fprintf('\n\n          %s\n\n','Output 2.9.4')
fprintf('Solution left to the student.\n\n');

% Problem 2.9.5
% In the command window, add 2 + 2 and then observe the value of ans
fprintf('\n\n          %s\n\n','Output 2.9.5')
2 + 2

% Problem 2.9.6
% In the command window, get the result of adding 4 to ans. Looking at
% the new answer, what does this tell you about MATLAB's 'willingness'
% to redefine values?
fprintf('\n\n          %s\n\n','Output 2.9.6')
fprintf('Solution left to the student.\n\n');

% Problem 2.9.7
% Use the editor to write and then save a short program called
% My_Program_01 which assigns 1 to w. Run the program so the value of w
% displays in the Command window.
fprintf('\n\n          %s\n\n','Output 2.9.7')
% My_Program_01.m
w = 1

% Problem 2.9.8
% Save My_Program_01.m as My_Program_02.m and expand it so that after w
% gets 1, x gets w + 1, and then y gets x - 2. Add another one-line command
% that immediately brings up the command window.
fprintf('\n\n          %s\n\n','Output 2.9.8')
fprintf('Solution left to the student.\n\n');

% Problem 2.9.9
% Debug My_Program_03 so b gets the sum of a and 3, c gets b - 2, d gets
% the product of b and c, and e gets b divided by c.
fprintf('\n\n          %s\n\n','Output 2.9.9')
% My_Program_03_before
% a
% b = a + 1
% c = = b - 2
% d b x c
% e = b divided by c
%
% My_Program_03_after
a = 0;
b = a + 3;
c = b - 2;
d = b * c;
e = b / c
commandwindow

% Problem 2.9.10
% Write a program called My_Program_04 in which Code 2.6.2 is expanded so
% there is a Method_1_Score_4 which gets 1267 and a Method_2_Score_4 which
% gets 1289 and all scores used in method 1 and 2 are subtracted in the way
% already established. Use disp to generate labeled output.
fprintf('\n\n          %s\n\n','Output 2.9.10')
% My_Program_04
Method_1_Score_1 = 899;
Method_2_Score_1 = 1286;
Method_1_Score_2 = 1018;
Method_2_Score_2 = 1344;
Method_1_Score_3 = 1267;
Method_2_Score_3 = 1389;
Method_1_Score_4 = 1167;
Method_2_Score_4 = 1289;
Summed_Differences_Between_Method_2_and_Method_1_Scores = ...
  Method_2_Score_1 - Method_1_Score_1...
+ Method_2_Score_2 - Method_1_Score_2...
+ Method_2_Score_3 - Method_1_Score_3...
+ Method_2_Score_4 - Method_1_Score_4;
disp('Sum of differences between Method 2 and Method 1')
disp(Summed_Differences_Between_Method_2_and_Method_1_Scores);
commandwindow

% Problem 2.9.11
% What information do you learn from executing the VER command?
fprintf('\n\n          %s\n\n','Output 2.9.11')
fprintf('Solution left to the student.\n\n');

Output 2.9.1

ans =
29-Apr-2014

Output 2.9.2

Solution left to the student.

Output 2.9.3

CALENDAR Calendar.
    CALENDAR or CALENDAR(DATE) or CALENDAR(YEAR,MONTH) is a 6-by-7
    matrix containing a calendar for the current or specified month.
 
    The first column of the returned matrix corresponds to Sunday.
 
    See also DATENUM.

    Reference page in Help browser
       doc calendar

                   Sep 2013
     S     M    Tu     W    Th     F     S
     1     2     3     4     5     6     7
     8     9    10    11    12    13    14
    15    16    17    18    19    20    21
    22    23    24    25    26    27    28
    29    30     0     0     0     0     0
     0     0     0     0     0     0     0

Output 2.9.4

Solution left to the student.

Output 2.9.5

ans =
     4

Output 2.9.6

Solution left to the student.

Output 2.9.7

w =
     1

Output 2.9.8

Solution left to the student.

Output 2.9.9

e =
     3

Output 2.9.10

Sum of differences between Method 2 and Method 1
   957

Output 2.9.11

Solution left to the student.