-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhileLoops.pg
More file actions
183 lines (158 loc) · 5.41 KB
/
WhileLoops.pg
File metadata and controls
183 lines (158 loc) · 5.41 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
##DESCRIPTION
## This problem is meant to have students write brief pieces of code to solve problems that have a specific numerical answer.
##ENDDESCRIPTION
##KEYWORDS('programming', 'code')
## DBsubject('Programming')
## DBchapter('')
## DBsection('')
## Date('6/7/2016')
## Author('')
## Institution('')
## TitleText1('')
## EditionText1('')
## AuthorText1('')
## Section1('')
## Problem1('')
########################################################################
DOCUMENT();
loadMacros(
"PG.pl",
"PGauxiliaryFunctions.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"MathObjects.pl",
"problemRandomize.pl",
"PGanswermacros.pl",
"PGgraphmacros.pl",
"PGnumericalmacros.pl",
"PGstatisticsmacros.pl",
"answerHints.pl"
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
# How many attempts before a hint?
$showHint = 10;
#Allow the student to generate a new (re-randomized) problem
#AFTER they have submitted a correct answer.
ProblemRandomize(onlyAfterDue=>0);
##############################################################
$mc = new_multiple_choice();
$mc -> qa(
"What does this code do?",
"Plots the points in the sequence \( a_{n+1} = a_n - 0.5 a_n + 5 \) until the value of the point is within 0.01 of the equilibrium value."
);
$mc -> extra(
"Plots the points in the sequence \( a_{n+1} = a_n - 0.5 a_n + 5 \) until they reach the equilibrium value.",
"Plots the points in the sequence \( a_{n+1} = a_n - 0.5 a_n + 5 \) until the value of the point is within 0.1 of the equilibrium value.",
"Plots the points in the sequence \( a_{n+1} = a_n - 0.5 a_n + 5 \) until the value of the point is reaches 100.",
"Plots the points in the sequence \( a_{n+1} = a_n - 0.5 a_n + 5 \) until the value of the point is reaches 5."
);
$mc -> makeLast("This code doesn't work (hint: this is not the answer)"
);
$r = random(0.5,1.5,0.01);
$b = random(3,7,1);
$A0 = random(5,25,1);
$N = random(15000,17000,1);
$A = $A0;
$AOld = $A0;
$Sum = $A0;
do {
$AOld = $A;
$A = $AOld + $r * $AOld + $b;
$OldSum = $Sum;
$Sum = $OldSum + $A;
} until ($Sum > $N);
##############################################################
#
# Text
#
#
Context()->texStrings;
BEGIN_TEXT
$PAR
So far in the programming exercises we have explored "for" loops and "if then else" statements. While these are both powerful tools, they are not the only ones available in the MATLAB programming language. In this exercise we will introduce and begin playing with the "while" loop.
$PAR
We use a "while" loop to repeat a process or computation while some statement is true. As soon as the statement becomes false we stop the loop. For example, consider the following problem:$BR
We would like to add the multiples of 2 starting from 2 and ending when the sum is less than 1000. Here is the code:
$PAR
clear; clc; $BR
Sum = 0; $BR
Counter = 1; $BR
while Sum < 1000 $BR
OldSum = Sum; $BR
Sum = Sum + 2*Counter; $BR
$TAB Counter = Counter + 1; $BR
end $BR
OldSum $BR
Let's go through this code line by line: $PAR
clear; clc; $BR
This line clears the working memory in MATLAB and clears the command line. $PAR
Sum = 0; $BR
Counter = 1; $BR
These lines initialize the sum (which will be the running total) and a counter that acts as a dummy variable. $PAR
while Sum < 1000 $BR
OldSum = Sum; $BR
Sum = OldSum + 2*Counter; $BR
$TAB Counter = Counter + 1; $BR
end $BR
These lines do most of the work. The "while" starts a loop and checks if the "Sum" variable is less than 1000. If it is then we enter the loop. $BR
Once in the loop we store the previous running total to "OldSum" and then add twice the counter to it. Next, we increment the counter by 1. $PAR
OldSum $BR
Finally, we print the "OldSum" variable that shows the sum just before the condition "Sum < 1000" was met.
$PAR
$PAR
$BBOLD Now consider this code: $EBOLD $BR
clear; clc; $BR
a(1) = 100; $BR
n=1; $BR
equilibrium = 10; $BR
while abs( a(n) - equilibrium ) > 0.01 $BR
a(n+1) = a(n) - 0.5 * a(n) + 5; $BR
n = n+1; $BR
end $BR
plot( 1:n , a , 'b*') $BR
\{ $mc -> print_q() \} $BR
\{ $mc -> print_a() \} $PAR
$PAR
$BBOLD And now for a coding challenge: $EBOLD $BR
Consider the sequence: \( a_{n+1} = a_n + 0.5 a_n + 2 \). If we start the sequence at 20 and start summing terms until the sum exceeds 100 we get $BR
\( a_0 = 20, \quad a_1 = 32, \quad a_2 = 50, \quad a_3 = 77, \ldots \) $BR
and so the running sum would be $BR
\( 20, 52, 102, 179, \ldots. \)
Hence, we would stop summing at 52. $PAR
Write MATLAB program that considers the sequence \( a_{n+1} = a_n + $r a_n + $b \) starting with $A0 and calculates a running sum total until the sum exceeds $N.
$BR
Sum = \{ans_rule(15)\}
$PAR
$BCENTER
$BBOLD
Warning to the code writer:
$EBOLD
$ECENTER
It is possible in a "while" loop to create code that runs forever. One modification that I prefer is to put a check into the while loop to make sure that the counter doesn't exceed a certain value. For example
$PAR
Sum = 0; $BR
Counter = 1; $BR
while Sum < 1000 $BR
OldSum = Sum; $BR
Sum = Sum + 2*Counter; $BR
$TAB Counter = Counter + 1; $BR
if Counter > 1000000 $BR
break $BR
end $BR
end $BR
OldSum $BR
$PAR
If your code happens to be running forever in MATLAB you can first click into the command window and then press "control c". This should stop your runaway code.
END_TEXT
BEGIN_HINT
You can do it!
END_HINT
ANS( radio_cmp( $mc->correct_ans() ) );
ANS(num_cmp($OldSum,,tol=>.5));
##############################################################
#
# Answers
#
#
ENDDOCUMENT();