-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEulerMATLAB2.pg
More file actions
128 lines (110 loc) · 3.75 KB
/
EulerMATLAB2.pg
File metadata and controls
128 lines (110 loc) · 3.75 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
##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);
##############################################################
$Test = random(100,300,1);
$Y0 = random(5,80,1);
$eq1 = 0;
$Y = $Y0;
$r = random(0.2,0.5,0.01);
$M = random(1000,2000,5);
$dt = list_random(0.00001,0.00005,0.00025);
$DT = 10**5*$dt;
$n = 1;
do {
$n = $n + 1;
$Y = $Y + $dt * $r * $Y * ( 1 - $Y / $M);
} until ($Y > $Test);
$Step = $n;
$mc = new_multiple_choice();
$mc -> qa("Is the smaller of the two equilibria stable or unstable? Use your Euler code to test your conjecture before you submit an answer.","Unstable");
$mc -> extra("Stable");
$mc2 = new_multiple_choice();
$mc2 -> qa("Is the larger of the two equilibria stable or unstable? Use your Euler code to test your conjecture before you submit an answer.","Stable");
$mc2 -> extra("Unstable");
##############################################################
#
# Text
#
#
Context()->texStrings;
BEGIN_TEXT
$PAR
Consider the logistic differential equation $BR
$BCENTER
\( y'(t) = r \cdot y(t) \cdot \left( 1 - \frac{y(t)}{M} \right). \)
$ECENTER
$BR
Recall that in this equation, \(r\) is a growth rate and \(M\) is a carrying capacity. This differential equation is especially useful when modeling populations that are growing under limited resources.
$PAR
$dt $BR
$DT
$PAR
In this problem you have a few tasks. For each of these tasks take \( r= $r \) and \( M = $M \). $BR
(a) Build an Euler approximation to the solution of the differential equation using \( y(0) = $Y0\). You will want to do this with MATLAB because we need to use \( \Delta t = $DT \times 10^{-5} \) (With a time step that small you really shouldn't want to do this in Excel!). ... I'll wait for you to build the Euler approximation ... It might be useful to recall that the Euler approximation for this differential equation is $BR
$BCENTER
\( y_{n+1} = y_n + \Delta t \cdot r \cdot y_n \cdot \left( 1 - \frac{y_n}{M} \right) \)
$ECENTER
$BR
(Note: your time step might be expressed in scientific notation above)
$PAR
(b) Now that you have an Euler approximation, modify your code to find the very first step number, \(n\), where \(y_n\) exceeds $Test.
$PAR
Step Number = \{ans_rule(15)\}
$PAR
(c) There are two equilibrium values for this differential equation. $BR
The smallest equilibrium value is: \{ ans_rule(10) \} $BR
The largest equilibrium value is: \{ ans_rule(10) \} $PAR
(d) \{ $mc -> print_q() \} $BR
\{ $mc -> print_a() \} $PAR
(e) \{ $mc2 -> print_q() \} $BR
\{ $mc2 -> print_a() \} $PAR
END_TEXT
BEGIN_HINT
You will likely need a while loop or at least an if-then statement for this problem.
END_HINT
ANS(num_cmp($Step,,tol=>1.5));
ANS(num_cmp($eq1,,tol=>0.1));
ANS(num_cmp($M,,tol=>0.1));
ANS( radio_cmp( $mc->correct_ans() ) );
ANS( radio_cmp( $mc2->correct_ans() ) );
##############################################################
#
# Answers
#
#
ENDDOCUMENT();