-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodingProblem10.pg
More file actions
156 lines (132 loc) · 5.17 KB
/
CodingProblem10.pg
File metadata and controls
156 lines (132 loc) · 5.17 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
##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",
"parserPopUp.pl"
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
# How many attempts before a hint?
$showHint = 5;
#Allow the student to generate a new (re-randomized) problem
#AFTER they have submitted a correct answer.
ProblemRandomize(onlyAfterDue=>0);
##############################################################
$r = random(0.1,0.5,0.01);
$b = random(-100,100,0.1);
$a0 = random(2,20,1);
$dt = random(0.01,0.1,0.01);
$tmax = random(10,20,0.1);
$popup1 = PopUp(["?","y","t","y(t)","y(n)","y(n+1)","dt","r","b"],"y(n)");
$popup2 = PopUp(["?","y","t","y(t)","y(n)","y(n+1)","dt","r","b"],"dt");
$popup3 = PopUp(["?","y","t","y(t)","y(n)","y(n+1)","dt","r","b"],"y(n)");
$popup4 = PopUp(["?","y","t","y(t)","y(n)","y(n+1)","dt","r","b"],"y");
$ae = -$b / ($r);
$mc1a = new_multiple_choice();
$mc1a -> qa ("Is this equilibrium stable or unstable?","unstable");
$mc1a ->extra("stable");
$C1 = $a0-$ae;
$C2 = $ae;
$popup5 = PopUp(["?","t","r","b","C1","C2","dt","tmax","y(t)","y(n)","y","yexact","y(0)","y(0)+b/r","y_exact","y(1)","y(1)+b/r","b/r","-b/r"],"y(1)+b/r");
$popup6 = PopUp(["?","t","r","b","C1","C2","dt","tmax","y(t)","y(n)","y","yexact","y(0)","y(0)+b/r","y_exact","y(1)","y(1)+b/r","b/r","-b/r"],"-b/r");
$popup7 = PopUp(["?","t","r","b","C1","C2","dt","tmax","y(t)","y(n)","y","yexact","y(0)","y(0)+b/r","y_exact","y(1)","y(1)+b/r","b/r","-b/r"],"C1");
$popup8 = PopUp(["?","t","r","b","C1","C2","dt","tmax","y(t)","y(n)","y","yexact","y(0)","y(0)+b/r","y(1)","y(1)+b/r","b/r","-b/r"],"r");
$popup9 = PopUp(["?","t","r","b","C1","C2","dt","tmax","y(t)","y(n)","y","yexact","y(0)","y(0)+b/r","y(1)","y(1)+b/r","b/r","-b/r"],"t");
$popup10 = PopUp(["?","t","r","b","C1","C2","dt","tmax","y(t)","y(n)","y","yexact","y(0)","y(0)+b/r","y(1)","y(1)+b/r","b/r","-b/r"],"C2");
$popup11 = PopUp(["?","t","r","b","C1","C2","dt","tmax","y(t)","y(n)","y","yexact","y(0)","y(0)+b/r","y(1)","y(1)+b/r","b/r","-b/r"],"t");
$popup12 = PopUp(["?","t","r","b","C1","C2","dt","tmax","y(t)","y(n)","y","yexact","y(0)","y(0)+b/r","y(1)","y(1)+b/r","b/r","-b/r"],"yexact");
##############################################################
#
# Text
#
#
Context()->texStrings;
BEGIN_TEXT
Consider the linear first order non-homogeneous differential equation \( y'(t) = $r y(t) + $b\) with initial condition \( y(0) = $a0 \). Fill in the blanks in the MATLAB code that will create a plot of an Euler approximation to this differential equation with \(\Delta t = $dt\) on the interval \(0 \leq t \leq $tmax\).
$PAR
clear; clc; clf; $BR
dt = \{ans_rule(5)\}; $BR
tmax = \{ans_rule(5)\}; $BR
t = 0 : dt : tmax; $BR
r = \{ans_rule(5)\}; $BR
b = \{ans_rule(5)\}; $BR
y = \{ans_rule(5)\}; $BR
for n = 1:length(t)-1 $BR
y(n+1) = \{$popup1->menu()\} + \{$popup2->menu()\} *( r * \{$popup3->menu()\}+b ); $BR
end $BR
plot(t,\{$popup4->menu()\})
$BR
Now run your code in MATLAB to be sure that it produces a plot of the approximate solution.
$PAR
Next we will do some mathematical analysis of the above differential equation. $BR
(a) Find the equilibrium value for this differential equation. $BR
\(y_{eq} = \) \{ans_rule(10)\}
$BR
(b) \{$mc1a -> print_q \}
$BR
\{$mc1a -> print_a\}
$BR
(c) What is the analytic solution to the differential equation? $BR
\(y(t) = \) \{ans_rule(20)\}
$PAR
Finally, we are going to plot the analytic solution on top of the Euler approximation. Fill in the blanks of the additional code needed for MATLAB to create this plot (in red) on top of the previous plot.
$PAR
hold on $BR
C1 = \{$popup5->menu()\}; $BR
C2 = \{$popup6->menu()\}; $BR
yexact = \{$popup7->menu()\} * exp( \{$popup8->menu()\} * \{$popup9->menu()\} ) + \{$popup10->menu()\}; $BR
plot( \{$popup11->menu()\} , \{$popup12->menu()\} ,'r')
$PAR
Conclude this problem by making sure that your code runs and by comparing the graphs of the exact and approximate solutions. (Save your code so you can use it later!)
END_TEXT
ANS(num_cmp($dt,tol=>0.5));
ANS(num_cmp($tmax,tol=>0.5));
ANS(num_cmp($r,tol=>0.5));
ANS(num_cmp($b,tol=>0.5));
ANS(num_cmp($a0,tol=>0.5));
ANS( $popup1->cmp());
ANS( $popup2->cmp());
ANS( $popup3->cmp());
ANS( $popup4->cmp());
ANS(num_cmp($ae,tol=>0.01));
ANS(str_cmp( $mc1a->correct_ans ) ) ;
ANS(function_cmp("($a0-$ae)*exp($r*t) + $ae",'t'));
ANS( $popup5->cmp());
ANS( $popup6->cmp());
ANS( $popup7->cmp());
ANS( $popup8->cmp());
ANS( $popup9->cmp());
ANS( $popup10->cmp());
ANS( $popup11->cmp());
ANS( $popup12->cmp());
##############################################################
#
# Answers
#
#
ENDDOCUMENT();