Code and Output
Code 5.1.1:
b = 2;
a = 1;
if a == 1
b = 2 * b;
else
b = -2 * b;
end
b
Output 5.1.1:
b =
4
Code 5.1.2:
b = 2;
a = -1;
if a == 1
b = 2*b;
end
b
Output 5.1.2:
b =
2
Code 5.1.3:
b = 2;
a = 1;
if a < 0
b = -1;
elseif a == 0
b = 0;
elseif a == 1
b = 1;
end
b
Output 5.1.3:
b =
1
Code 5.1.4:
b = 2;
a = 2.7;
if (a >= 1) & (a <= 3)
b = 2*b
end
Output 5.1.4:
b =
4
Code 5.1.5:
b = 2;
a = 3.7;
if (a <= 1) | (a >= 3)
b = -b;
end
b
Output 5.1.5:
b =
-2
Code 5.1.6:
b = -2;
a = 3.7;
if a ~= 10
b = -b;
end
b
Output 5.1.6:
b =
2
Code 5.1.7:
A = -2.3
a = 10
if A <= 0
if a <= -5
b = 1; %if A is <=0 and if a <=-5, b gets 1
else
b = 2; %if A is <=0 and if a is not <=-5, b gets 2
end
else
if a <= 5
b = 3; %if A is not <=0 and if a <=5, b gets 3
else
b = 4; %if A is not <=0 and if a is not <=5, b gets 4
end
end
b
Output 5.1.7:
A =
-2.3000
a
10
b =
2
Code 5.1.8:
if 1
disp('Extensive output')
end
Code 5.1.9:
if false
disp('Extensive output')
end
Code 5.1.10:
verbose = true;
% ... other code of your program
if verbose
disp('Extensive output')
end
% ... other code of your program
if verbose
disp('More extensive output')
end
Code 5.1.11:
variable_1 = 5;
variable_1_exists = exist('variable_1')
variable_2_exists = exist('variable_2')
Output 5.1.11:
variable_1_exists =
1
variable_2_exists =
0
Code 5.1.12:
variable_3 = [];
variable_4 = 9;
variable_3_empty = isempty(variable_3)
variable_4_empty = isempty(variable_4)
Output 5.1.12:
variable_3_empty =
1
variable_4_empty =
0
Code 5.2.1:
year = 2017 % For example
switch year
case 2018
disp('First-year');
case 2017
disp('Sophomore');
case 2016
disp('Junior');
case 2015
disp('Senior');
otherwise
disp('Not a valid class year');
end
Output 5.2.1:
year =
2017
Sophomore
Code 5.3.1:
disp(' i a');
disp(' ');
for i=1:6
a=2*i;
disp([i,a]);
end
Output 5.3.1:
i a
1 2
2 4
3 6
4 8
5 10
6 12
Code 5.3.2:
for i=1:6
a(i)=2*i;
end
a
Output 5.3.2:
a =
2 4 6 8 10 12
Code 5.3.3:
for i=1:6
for j=1:3
a(i,j)=i+j;
end
end
a
Output 5.3.3:
a =
2 3 4 8 10 12
3 4 5 0 0 0
4 5 6 0 0 0
5 6 7 0 0 0
6 7 8 0 0 0
7 8 9 0 0 0
Code 5.3.4:
clear a
for i=1:6
for j=1:3
a(i,j)=i+j;
end
end
a
Output 5.3.4:
a =
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
Code 5.3.5:
for i=0:10
a(i)=i+1;
end
Output 5.3.5:
??? Attempted to access a(0); index must be a positive integer or logical.
Code 5.3.6:
x=10;
disp(' i a');
disp(' ')
for i=-3:3
a=x*i;
disp([i a]);
end
Output 5.3.6:
i a
-3 -30
-2 -20
-1 -10
0 0
1 10
2 20
3 30
Code 5.3.7:
x=10;
disp(' i a')
disp(' ')
for i=-3:3
if i~=0
a=x/i;
disp([i a]);
end
end
Output 5.3.7:
i a
-3.0000 -3.3333
-2 -5
-1 -10
1 10
2 5
3.0000 3.3333
Code 5.3.8:
x=10;
disp(' i a')
disp(' ')
for i=-3:3
% if i~=0
a=x/i;
disp([i a]);
% end
end
Output 5.3.8:
i a
-3.0000 -3.3333
-2 -5
-1 -10
0 Inf
1 10
2 5
3.0000 3.3333
Code 5.4.1:
a = 1;
b = .25;
steps = 0;
while a < 10
a = a + a^b;
steps = steps + 1;
end
a
steps
Output 5.4.1:
a =
10.9475
steps =
7
Code 5.4.2:
goal = 100;
x = 1;
while x ~= 100
x^2
x = x + 2;
end
Code 5.4.3:
goal = 100;
x = 1;
steps = 1
while x ~= 100
x^2
x = x + 2;
steps = steps + 1;
if steps > 100
break
end
end
Output 5.4.3:
steps =
1
ans =
1
ans =
9
ans =
25
ans =
49
ans =
81
% ... output omitted
ans =
38025
ans =
38809
ans =
39601
Code 5.4.4:
clc
outputForTesting = true;
while true
candidate = [randperm(3) randperm(3) randperm(3)];
if candidate(3) ~= candidate(4) & ...
candidate(6) ~= candidate(7);
break
end
if outputForTesting
badcandidate = candidate
end
end
goodsequence = candidate
Output 5.4.4:
badcandidate =
1 2 3 3 2 1 3 1 2
badcandidate =
3 2 1 3 1 2 2 3 1
badcandidate =
3 2 1 1 3 2 1 3 2
badcandidate =
3 2 1 3 2 1 1 2 3
goodsequence =
2 3 1 2 3 1 2 1 3
Code 5.5.1:
% Part 1: Generate numbers using RANDN
clc
close all
clear
tic
r = randn(1000,1000);
SecondsToGenerateMillionRandom_Directly = toc
% Part 2: Generate numbers using FOR, without preallocation
clear r
tic
for ii = 1:1000
for jj = 1:1000
r(ii,jj) = randn(1,1);
end
t(ii) = toc;
end
SecondsToGenerateMillionRandom_Forloops = toc
% Part 2: Generate numbers using FOR, with preallocation
clear r
tic
r = zeros(1000,1000);
for ii = 1:1000
for jj = 1:1000
r(ii,jj) = randn(1,1);
tpa(ii) = toc;
end
end
SecondsToGenerateMillionRandom_Preallocated_ThenForLoops = toc
Output 5.5.1:
SecondsToGenerateMillionRandom_Directly =
0.0502
SecondsToGenerateMillionRandom_Forloops =
4.0844
SecondsToGenerateMillionRandom_Preallocated_ThenForLoops =
2.3898
Code 5.6.1:
a = [12 15 17 13 15 12 14];
b = ( a > 13 );
Output 5.6.1:
b =
0 1 1 0 1 0 1
Code 5.6.2:
a = [12 15 17 13 15 12 14];
b = ( a > 13 );
indices_of_good_values_in_a = find(b)
the_good_values_themselves = a(indices_of_good_values_in_a)
Output 5.6.2:
indices_of_good_values_in_a =
2 3 5 7
the_good_values_themselves =
15 17 15 14
Code 5.6.3:
a = [12 15 17 13 15 12 14];
the_good_values_instantly = a( a>13 )
Output 5.6.3:
the_good_values_instantly =
15 17 15 14
Code 5.6.4:
m1 = [
16 13 3 2
9 12 6 7
5 8 10 11
4 1 15 14];
m2 = [
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1];
cells_inwhich_m1_equals_m2 = (m1 == m2)
indices_of_the_equal_values = find(m1 == m2)
the_values_that_are_equal = m1(m1 == m2)
not_m1_equals_m2 = ~(m1 == m2)
m1_notequal_m2 = (m1 ~= m2)
Output 5.6.4:
cells_inwhich_m1m1_equals_m2 =
1 0 1 0
0 0 0 0
0 0 0 0
1 0 1 0
indices_of_the_equal_values =
1
4
9
12
the_values_that_are_equal =
16
4
3
15
not_m1_equals_m2 =
0 1 0 1
1 1 1 1
1 1 1 1
0 1 0 1
m1_notequal_m2 =
0 1 0 1
1 1 1 1
1 1 1 1
0 1 0 1
Code 5.6.5:
mymatrix = magic(3)
any_5_bycolumns = any(mymatrix == 5)
all_lessthan_or_equal_8_bycolumns = all(mymatrix <= 8)
any_5_inthe_wholematrix = any(mymatrix(:) == 5)
all_lessthan8_inthe_wholematrix = all(mymatrix(:) < 8)
Output 5.6.5:
mymatrix =
8 1 6
3 5 7
4 9 2
any_5_bycolumns =
0 1 0
all_lessthan_or_equal_8_bycolumns =
1 0 1
any_5_inthe_wholematrix =
1
all_lessthan8_inthe_wholematrix =
0
Code 5.7.1:
h=randperm(11)+10
h==12|h==16
find(h==12|h==16)
values_sought = h(find(h==12|h==16))
Output 5.7.1:
h =
16 11 17 14 21 19 15 18 13 20 12
ans =
1 0 0 0 0 0 0 0 0 0 1
ans =
1 11
values_sought =
16 12
Code 5.7.2:
z = randi(100,1,8)
z(z > 50) = 50
Output 5.7.2:
z =
82 91 13 92 64 10 28 55
z =
50 50 13 50 50 10 28 50
Code 5.7.3:
length(z(z < 25))
Output 5.7.3:
ans =
2
Code 5.7.4:
A = [ 1 4 6 3 8 6 5 9 2 7 5];
disp('The following elements of A are divisible by 3:')
for i = A(mod(A,3)==0)
disp(i)
end
Output 5.7.4:
The following elements of A are divisible by 3:
6
3
6
9
Code 5.8.1:
rng('shuffle')
clc
sequenceOf1sAnd2s = [ones(1,6) ones(1,6)*2];
done = false;
cycles = 0;
while not(done)
cycles = cycles + 1;
done = true;
sequenceOf1sAnd2s = sequenceOf1sAnd2s(randperm(12));
for i = 4:12
% Detect any runs of 1's or 2's
if sequenceOf1sAnd2s(i) == sequenceOf1sAnd2s(i-1)...
& sequenceOf1sAnd2s(i) == sequenceOf1sAnd2s(i-2)...
& sequenceOf1sAnd2s(i) == sequenceOf1sAnd2s(i-3)
done = false;
end
end
end
sequenceOf1sAnd2s
cycles
Output 5.8.1:
sequenceOf1sAnd2s =
2 1 2 1 2 2 2 1 1 1 2 1
cycles =
2
Code 5.8.2:
rng('shuffle')
clc
clear m
LSsize = 7;
% The first line of m is the integers 1:LSsize
m(1,:) = [1:LSsize];
% Each subsequent line is the preceding line, rotated by one element
for i = 2:LSsize
m(i,:) = m(i-1,[2:end,1]);
end
OriginalMatrix = m
% Permute rows, ...
m1 = m(randperm(LSsize),:);
RowsPermutedMatrix = m1
% ...then permute columns to make randomized Latin Square matrix
m2 = m1(:,randperm(LSsize));
LatinSquareMatrix = m2
Output 5.8.2:
OriginalMatrix =
1 2 3 4 5 6 7
2 3 4 5 6 7 1
3 4 5 6 7 1 2
4 5 6 7 1 2 3
5 6 7 1 2 3 4
6 7 1 2 3 4 5
7 1 2 3 4 5 6
RowsPermutedMatrix =
1 2 3 4 5 6 7
5 6 7 1 2 3 4
7 1 2 3 4 5 6
4 5 6 7 1 2 3
2 3 4 5 6 7 1
3 4 5 6 7 1 2
6 7 1 2 3 4 5
LatinSquareMatrix =
2 6 3 7 4 5 1
6 3 7 4 1 2 5
1 5 2 6 3 4 7
5 2 6 3 7 1 4
3 7 4 1 5 6 2
4 1 5 2 6 7 3
7 4 1 5 2 3 6
Problem 5.9.2:
RT_and_PC_Data = [
390 .45
347 .32
866 .98
549 .67
589 .72
641 .50
777 .77
702 .68
];
Problem 5.9.6:
a = 16; b = 0; c = -4;
a = 9; b = 6; c = 1;
a = 9; b = 0; c = 1;
Problem 5.9.11:
clc
clear
rng('default')
data(:,1) = randperm(300);
data(:,2) = randi(4,300,1) + 14;
data(:,3) = randi(20,300,1)/10 + 2;
checkvalues = mean(data)
% checkvalues should be:
% 150.5000 16.4533 2.9837
Solutions
% Solutions_Chapter_05
% 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 5, save this code as a
% MATLAB script file and run the program.
% Problem 5.9.1
% You want to show stimuli to a participant in a psychophysics experiment.
% The stimuli to be shown should have values of A^B,
% where A takes on the values of 1, 2, 3, and 4, and B takes on values of
% 1, 2, 3, and 4. Write a program to generate the 16 stimulus values.
clc
commandwindow
fprintf('\n\n %s\n\n','Output 5.9.1')
Stimuli = [];
setnumber = 0;
for Avalue = 1:4
for Bvalue = 1:4
setnumber = setnumber + 1;
Stimuli(setnumber) = Avalue^Bvalue;
end
end
Stimuli
% Now reorder the values into a random presentation order.
Stimuli = Stimuli(randperm(16))
% Problem 5.9.2
% The following matrix contains fictional data from a reaction time
% experiment. Each row contains the mean reaction time and proportion
% correct for a different participant. Use a for loop and an if statement
% to identify the participants who had mean reaction times greater than
% 500 ms and proportions correct greater than .65. The output should
% contain two matrices, called Identified_Participants and OK_Scores.
% The values in Identified_Participants should be the numbers of the
% participants fulfilling the criteria. The values in OK_Scores should
% be rows, each with two columns, one for reaction time and one for
% proportion correct.
fprintf('\n\n %s\n\n','Output 5.9.2')
RT_and_PC_Data = [
390 .45
347 .32
866 .98
549 .67
589 .72
641 .50
777 .77
702 .68
];
fprintf('Solution left to the student.\n\n');
% Problem 5.9.3
% Use logical comparisons instead of a for loop and an if statement
% to solve the last problem.
fprintf('\n\n %s\n\n','Output 5.9.3')
[ParticipantNumbers] = ...
(RT_and_PC_Data(:,1) > 500) & (RT_and_PC_Data(:,2) > .65)
IdentifiedParticipants = sum(ParticipantNumbers)
OK_Scores = (RT_and_PC_Data(ParticipantNumbers,1:2))
% Problem 5.9.4
%
% Find out how long it takes your computer to identify values greater
% than the overall mean in a 1000×1000 matrix of random numbers,
% using for and if statements. Also find out how long it takes your
% computer to identify values greater than the overall mean through
% instant if-ing. Because the matrix is large, you will want to suppress
% most other output.
fprintf('\n\n %s\n\n','Output 5.9.4')
fprintf('Solution left to the student.\n\n');
% Problem 5.9.5
%
% You are studying how quickly subjects can learn and you have
% found that in a particular experimental paradigm, the accuracy
% of a subject is typically a logarithmic function of the number
% of trials they have experienced. You have created an equation
% which characterizes their behavior very neatly and you now
% want to use your MATLAB expertise to predict how quickly
% subjects can learn the task. In your experiment, subjects
% have to choose between 4 different stimuli that are
% equiprobable, which means that their chance of guessing
% correctly is ¼, and learning the task allows them to improve
% their performance above this level.
%
% The equation you have discovered is:
%
% p_correct = base_rate + learning_rate*log(trial),
%
% where trial can take on integer values, learning_rate can
% be any real number between 0 and 1, base_rate equals ¼,
% and p_correct cannot exceed 1. Write a program that allows
% you to specify the learning_rate and criterion parameters
% and then computes for you how many trials it will take for
% subjects to reach that criterion using this equation.
fprintf('\n\n %s\n\n','Output 5.9.5')
base_rate = .25;
learning_rate = .2;
criterion = .95;
disp(' trial p-corr')
trial = 0;
p_correct = base_rate;
while p_correct < criterion
trial = trial + 1;
p_correct = min(base_rate + learning_rate * log(trial), 1.0);
disp([trial p_correct])
end
% Problem 5.9.6
%
% Solve for the root(s) of the quadratic equation ax2 + bx + c = 0. Use
% appropriate contingencies (if or switch statements) to report the
% different cases (two roots, one root, and no roots) depending on
% the values of the coefficients a, b, and c, and use disp to describe
% the results you report. By way of reminder, a quadratic equation has
% two roots, x1 and x2, unless b2 = 4ac, in which case there is just one
% root, and unless b2 < 4ac, in which case there are no roots (no real
% values of a, b, and c that solve the equation.
%
% x_1= (-b + sqrt(b^2-4ac))/2a and x_2= (-b - sqrt(b^2-4ac))/2a
%
% Try your program with at least these three sets of values:
%
% a = 16; b = 0; c = -4;
% a = 9; b = 6; c = 1;
% a = 9; b = 0; c = 1;
%
fprintf('\n\n %s\n\n','Output 5.9.6')
a = 16; b = 0; c = -4; %Substitute other values here...
% a = 9; b = 6; c = 1;
% a = 9; b = 0; c = 1;
root1 = (-b + sqrt(b^2-4*a*c))/(2*a)
root2 = (-b - sqrt(b^2-4*a*c))/(2*a)
discriminant = b^2 - 4 * a * c;
if discriminant < 0
disp('No real roots')
elseif discriminant == 0
disp('One real root; x =')
disp(root1)
else
disp('Two real roots; x =')
disp([root1 root2])
end
% Problem 5.9.7
% Apply the trimming technique of Code 5.7.4 to the output of Problem
% 4.12.8 to limit the range of your SAT scores to 200 <= SAT <= 800.
% Count the number of 200's and 800's in the data set after you have
% done so.
fprintf('\n\n %s\n\n','Output 5.9.7')
fprintf('Solution left to the student.\n\n');
% Problem 5.9.8
% Write a program to compute the standard error of a uniform distribution
% (use rand) that has n values (a value you specify for each run of the
% program). Build in a contingency so you divide the standard deviation
% by the square root of n if n is greater than or equal to 30, but you
% divide by the square root of n-1 if n is less than 30.
fprintf('\n\n %s\n\n','Output 5.9.8')
% Problem 5.9.9
% Create a 2×100 matrix whose first and second rows are the numbers
% 1 to 100. Then multiply columns 3, 6, 9, 12, and 15 by 3.
fprintf('\n\n %s\n\n','Output 5.9.9')
fprintf('Solution left to the student.\n\n');
% Problem 5.9.10
% Having solved problem 5.9.9, see if you can achieve the same thing by
% typing the numbers 1 and 100 only once and by never typing the numbers
% 6, 9 or 12. Hint: Who ever said an index can only be one number?
fprintf('\n\n %s\n\n','Output 5.9.10')
arow = [1:100];
myMatrix = [arow;arow];
myMatrix(:,[3:3:15]) = myMatrix(:,[3:3:15]) * 3
% Problem 5.9.11
%
% The following code will generate a set of student data in which
% column 1 is the student number,column 2 is an integer representing
% the class year ('15, '16, '17, or '18), and column 3 the student's
% grade point average (which ranges from 2.0 to 4.0). Since the random
% number generator is initialized at the beginning, you will get the
% same sequence as we did, and your checkvalues should agree with the
% values reported in the comment lines.
%
clear
rng('default')
data(:,1) = randperm(300);
data(:,2) = randi(4,300,1) + 14;
data(:,3) = randi(20,300,1)/10 + 2;
format short
checkvalues = mean(data)
% % checkvalues should be:
% % 150.5000 16.4533 2.9837
%
% Without printing out the data matrix, answer the following: What
% students (by student number) had a 4.0 average? Who are the seniors
% (class of '15) who will graduate with honors (GPA >= 3.5)? How many
% first-year students (class of '18) are likely to elect to be Psychology
% majors, as predicted by their GPA being greater than 3.0? What is the
% GPA of student #1 (the student with that student number, not necessarily
% the first student in the matrix). What is the standard deviation of the
% GPAs of second-year students (class of '17)?
fprintf('Solution left to the student.\n\n');
% Problem 5.9.12
%
% To make "truly random" numbers available to the scientific community,
% some years ago the RAND corporation published a list of a million random
% digits (RAND Corporation, 1955). (The volume is still available through
% Amazon's "print on demand" service. An Amazon reviewer self-identified
% as 'a curious reader' observed, "Such a terrific reference work! But with
% so many terrific random digits, it's a shame they didn't sort them, to
% make it easier to find the one you're looking for.") Using tic, toc,
% and randi, generate a million random digits in a 100×100×100 array,
% measuring how long it takes to generate the digits in three ways: using
% three nested for loops without pre-allocation of the 100×100×100 array;
% using three nested for loops with pre-allocation; and directly (be sure
% to clear the array at the beginning of each generation). Also compare the
% times for setting a 1×1000000 array to the value NaN, directly and using
% for loops with and without pre-allocation. (If this seems to take forever
% on your particular machine, interrupt your program with ctrl-c and
% try again with a smaller number.)
fprintf('\n\n %s\n\n','Output 5.9.12')
clear % Loops, No preallocation
tic
for i = 1:100
for j = 1:100
for k = 1:100
Digits1(i,j,k) = randi(10)-1;
end
end
end
TimeForLoopsNoPreallocation = toc
size(Digits1)
Digits1(1:50)
clear % Loops, Preallocation
Digits2(100,100,100) = 0;
tic
for i = 1:100
for j = 1:100
for k = 1:100
Digits2(i,j,k) = randi(10)-1;
end
end
end
TimeForLoopsWithPreallocation = toc
size(Digits2)
Digits2(1:50)
% Direct
clear
tic
Digits3 = randi(10,100,100,100) - 1;
TimeForDirect = toc
size(Digits3)
Digits3(1:50)
clear
tic
for i = 1:10^7
Digits4(i) = NaN;
end
TimeforNansLoop = toc
clear
tic
Digits5(1:10^7) = NaN;
TimeforNansDirect = toc
% Problem 5.9.13
%
% Adam, Beth, Charlie, and Deb share an apartment. The dishes need to be
% washed every evening, and the residents agree to follow a rotating
% schedule, starting with Adam on the first. Write a program to print
% the date and the responsible resident for a particular day of the month.
% Who is to do the dishes on the 13th? Begin your program with today = 13,
% but make it so it would work if that first line specified any day of the
% month up to 31.
%
% Since no months except February have a multiple of four days, Adam
% would get the short end of this deal if he were the first to go each
% month. How might you address this dilemma, other than negotiating a
% small compensatory rent reduction for Adam? Hint: Doing so would require
% you to modify just one command in your working program at the beginning of
% each month.
fprintf('\n\n %s\n\n','Output 5.9.13')
monthoffset = 3; %Value for February
%Offset has the value for a particular month for one year:
% January 0
% February 3
% March 3 % except in leap years!
% April 2
% May 0
% June 3
% July 1
% August 0
% September 3
% October 1
% November 0
% December 2
fprintf('Solution left to the student.\n\n');
% After you have the program working, test your program for all possible
% days of the month, by omitting the first line (today = 13) and wrapping
% a for loop around your program:
disp(' TEST OF ALL DAYS')
monthoffset = 0;
for today = 1:31
today
switch mod(today+monthoffset,4)
case 1
disp('Adam')
case 2
disp('Beth')
case 3
disp('Charlie')
case 0
disp('Deb')
end
end;
Output 5.9.1
Stimuli =
Columns 1 through 13
1 1 1 1 2 4 8 16 3 9 27 81 4
Columns 14 through 16
16 64 256
Stimuli =
Columns 1 through 13
2 1 4 81 4 1 16 256 27 9 3 16 1
Columns 14 through 16
8 64 1
Output 5.9.2
Solution left to the student.
Output 5.9.3
ParticipantNumbers =
0
0
1
1
1
0
1
1
IdentifiedParticipants =
5
OK_Scores =
866.0000 0.9800
549.0000 0.6700
589.0000 0.7200
777.0000 0.7700
702.0000 0.6800
Output 5.9.4
Solution left to the student.
Output 5.9.5
trial p-corr
1.0000 0.2500
2.0000 0.3886
3.0000 0.4697
4.0000 0.5273
5.0000 0.5719
6.0000 0.6084
7.0000 0.6392
8.0000 0.6659
9.0000 0.6894
10.0000 0.7105
11.0000 0.7296
12.0000 0.7470
13.0000 0.7630
14.0000 0.7778
15.0000 0.7916
16.0000 0.8045
17.0000 0.8166
18.0000 0.8281
19.0000 0.8389
20.0000 0.8491
21.0000 0.8589
22.0000 0.8682
23.0000 0.8771
24.0000 0.8856
25.0000 0.8938
26.0000 0.9016
27.0000 0.9092
28.0000 0.9164
29.0000 0.9235
30.0000 0.9302
31.0000 0.9368
32.0000 0.9431
33.0000 0.9493
34.0000 0.9553
Output 5.9.6
root1 =
0.5000
root2 =
-0.5000
Two real roots; x =
0.5000 -0.5000
Output 5.9.7
Solution left to the student.
Output 5.9.8
Solution left to the student.
Output 5.9.9
Solution left to the student.
Output 5.9.10
myMatrix =
Columns 1 through 13
1 2 9 4 5 18 7 8 27 10 11 36 13
1 2 9 4 5 18 7 8 27 10 11 36 13
Columns 14 through 26
14 45 16 17 18 19 20 21 22 23 24 25 26
14 45 16 17 18 19 20 21 22 23 24 25 26
Columns 27 through 39
27 28 29 30 31 32 33 34 35 36 37 38 39
27 28 29 30 31 32 33 34 35 36 37 38 39
Columns 40 through 52
40 41 42 43 44 45 46 47 48 49 50 51 52
40 41 42 43 44 45 46 47 48 49 50 51 52
Columns 53 through 65
53 54 55 56 57 58 59 60 61 62 63 64 65
53 54 55 56 57 58 59 60 61 62 63 64 65
Columns 66 through 78
66 67 68 69 70 71 72 73 74 75 76 77 78
66 67 68 69 70 71 72 73 74 75 76 77 78
Columns 79 through 91
79 80 81 82 83 84 85 86 87 88 89 90 91
79 80 81 82 83 84 85 86 87 88 89 90 91
Columns 92 through 100
92 93 94 95 96 97 98 99 100
92 93 94 95 96 97 98 99 100
checkvalues =
150.5000 16.4533 2.9837
Solution left to the student.
Output 5.9.12
TimeForLoopsNoPreallocation =
2.8307
ans =
100 100 100
ans =
Columns 1 through 13
9 5 0 1 3 8 3 1 3 6 8 9 4
Columns 14 through 26
2 7 4 1 8 3 1 3 8 3 9 9 6
Columns 27 through 39
8 1 0 9 3 3 8 0 5 1 7 7 6
Columns 40 through 50
6 4 3 1 9 3 1 4 8 0 6
TimeForLoopsWithPreallocation =
2.5024
ans =
100 100 100
ans =
Columns 1 through 13
2 6 1 6 3 5 8 3 9 0 6 6 8
Columns 14 through 26
9 1 6 8 9 7 7 5 6 6 0 4 6
Columns 27 through 39
1 3 4 6 2 3 6 2 5 1 8 2 1
Columns 40 through 50
7 5 2 2 9 6 0 3 0 8 8
TimeForDirect =
0.0181
ans =
100 100 100
ans =
Columns 1 through 13
8 5 6 9 9 9 2 7 8 0 5 7 0
Columns 14 through 26
8 6 1 6 3 0 5 1 9 7 7 8 2
Columns 27 through 39
2 1 7 9 1 0 8 5 1 9 3 9 2
Columns 40 through 50
2 2 2 0 9 6 1 3 5 2 3
TimeforNansLoop =
49.0937
TimeforNansDirect =
0.0818
Output 5.9.13
Solution left to the student.
TEST OF ALL DAYS
today =
1
Adam
today =
2
Beth
today =
3
Charlie
today =
4
Deb
today =
5
Adam
today =
6
Beth
today =
7
Charlie
today =
8
Deb
today =
9
Adam
today =
10
Beth
today =
11
Charlie
today =
12
Deb
today =
13
Adam
today =
14
Beth
today =
15
Charlie
today =
16
Deb
today =
17
Adam
today =
18
Beth
today =
19
Charlie
today =
20
Deb
today =
21
Adam
today =
22
Beth
today =
23
Charlie
today =
24
Deb
today =
25
Adam
today =
26
Beth
today =
27
Charlie
today =
28
Deb
today =
29
Adam
today =
30
Beth
today =
31
Charlie