-
-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathlinear-nonhomog_euler.pg
More file actions
399 lines (327 loc) · 11.7 KB
/
linear-nonhomog_euler.pg
File metadata and controls
399 lines (327 loc) · 11.7 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
## DESCRIPTION
## Systems of ODEs: Nonhomogeneous Systems
## ENDDESCRIPTION
## DBsubject(Differential equations)
## DBchapter(Higher order differential equations)
## DBsection(Variation of parameters)
## Date(24/11/2015)
## Institution(METU-NCC)
## Author(Benjamin Walter)
##################################
# Initialization
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGmatrixmacros.pl",
"parserMultiAnswer.pl",
"answerHints.pl",
"extraAnswerEvaluators.pl", # needed for equation_cmp
);
#####################################
# Setup
Context("Numeric");
Context()->variables->{namePattern} = qr/[a-z][a-z0-9_]*'*/i;
Context()->variables->are(
t=>["Real", limits=>[1,3]],
n=>"Real",
y=>"Real", "y'"=>"Real", "y''"=>"Real",
"c1"=>"Real", "c2"=>"Real"
);
Context()->flags->set(
zeroLevel=>0.001,
limits=>[0,2],
zeroLevelTol=>0.001
);
############################
# setup differential equation
my $a1, $a2, $b1, $b2, $c, $d; # roots are a1/b1 and a2/b2
my @coeffs;
$b1 = 1; # random(1,3,1);
$b2 = 1;
$a1 = non_zero_random($b1-4, 4-$b1, 1)*$b1
+ (($b1 == 1) ? 0 : non_zero_random(1-$b1,$b1-1,1));
do { $a2 = non_zero_random(-3,3,1); }
until ( $a2/$b2 != $a1/$b1 );
$c = non_zero_random(-3,3,1); $d = random(-3,3,1);
@coef = (Formula("$b1*$b2"),
Formula("$b1*$b2-$a1*$b2-$a2*$b1"),
Formula("$a1*$a2"));
$L = Formula("$coef[0]*t^2*y'' + $coef[1]*t*y' + $coef[2]*y")->reduce; # DE
$g = Formula("$coef[0]*$c*t^3 + $coef[0]*$d*t^2")->reduce; # forcing function
$real_g = Formula("$c*t+$d")->reduce;
#############################
# answer evaluators
# A -- accept any multiple of eqn (rounding error => only small multiples)
$eqn = Formula("(t^n)*($b1*n-$a1)*($b2*n-$a2)")->reduce;
$eqn_hint1 = Formula("$coef[0]*t^2*(n*(n-1)*t^(n-2)) + $coef[1]*t*(n*t^(n-1)) + $coef[2]*(t^n)")->reduce;
$eqn_hint2 = Formula("t^n($coef[0]*n*(n-1) + $coef[1]*n + $coef[2])")->reduce;
$eqn_hint25 = Formula("($coef[0]*n*(n-1) + $coef[1]*n + $coef[2])")->reduce;
$eqn_hint3 = Formula("$coef[0]*n^2 + ($coef[1]-$coef[0])*n + $coef[2]")->reduce;
$eqn_hint4 = Formula("($b1*n-$a1)*($b2*n-$a2)")->reduce;
# B -- fundamental solutions and wronskian
$y1 = Formula("t^($a1/$b1)")->reduce;
$y2 = Formula("t^($a2/$b2)")->reduce;
my $Wcoef = Formula("(($a2*$b1-$a1*$b2)/($b1*$b2))");
my $Wronskian = Formula("$Wcoef*t^(($a2*$b1+$a1*$b2-$b1*$b2)/($b1*$b2))")->reduce;
my @S_fund = (Formula("$y1"),
Formula("$y2")); # record student solutions
my $S_wron = Formula("$Wronskian"); # record student wronskian
$multiansB = MultiAnswer($y1, $y2, $Wronskian)->with(
singleResult => 1,
checkTypes => 0,
allowBlankAnswers => 1,
format =>
"<table border='0' cellspacing='0'>
<tr><td>\(y_1 = \) %s </td></tr>
<tr><td>\(y_2 = \) %s </td></tr>
<tr><td>\(W = \) %s </td></tr>
</table>",
tex_format =>
"\begin{array}{l} y_1 = %s \\ y_2 = %s \\ W = %s
\end{array}",
checker => sub {
my ($correct, $student, $answerHash ) = @_;
my @c = @{$correct};
my @s = @{$student};
my $msg = ""; my @flag = (0,0,0); my @S=(0,0);
if ($c[0]->typeMatch($s[0]) &&
$c[1]->typeMatch($s[1])) {
if ((Formula("$s[0]")->usesOneOf("c1","c2") ||
Formula("$s[1]")->usesOneOf("c1","c2")) &&
!(Formula("$s[2]")->usesOneOf("c1","c2"))) {
Value::Error("If \(y_1\) and \(y_2\) use \(c_1\) and \(c_2\), then so does \(W(y_1,y_2)\)!${BR}(Easier if \(y_1\) and \(y_2\) don't have \(c_1\) and \(c_2\)...)");
}
@S = ( Formula("$s[0]"),
Formula("$s[1]") );
my @dS = ( Formula("$s[0]")->D('t'),
Formula("$s[1]")->D('t') );
my @ddS = ( Formula("$s[0]")->D('t','t'),
Formula("$s[1]")->D('t','t') );
$S_wron = Formula("$S[0]*$dS[1] - $S[1]*$dS[0]")->reduce;
if ($S_wron == Formula("0")) {
Value::Error("Fundmental solutions not independent!");
}
my @L = ( Formula("$coef[0]*t^2*$ddS[0] + $coef[1]*t*$dS[0] + $coef[2]*$S[0]")->reduce,
Formula("$coef[0]*t^2*$ddS[1] + $coef[1]*t*$dS[1] + $coef[2]*$S[1]")->reduce );
if ($L[0] == Formula("0")) { $flag[0] = 1; $msg = "\(y_1\) is fine$BR"; }
else { $msg = "\(y_1\) is not a fundamental solution$BR"; }
if ($L[1] == Formula("0")) { $flag[1] = 1; $msg = $msg . "\(y_2\) is fine$BR"; }
else { $msg = $msg . "\(y_2\) is not a fundamental solution$BR"; }
}
if (($flag[0]*$flag[1]) == 1) {
$S_fund[0] = Formula("$s[0]");
$S_fund[1] = Formula("$s[1]");
}
if ($c[2]->typeMatch($s[2]) &&
($S_wron == Formula("$s[2]"))) { $flag[2]=1; $S_wron = Formula("$s[2]")->reduce; }
else { Value::Error($msg . " Wronskian is incorrect!"); }
if ($flag[0]*$flag[1]*$flag[2] == 1) { return 1; }
else { if (length($msg) > 1) { Value::Error($msg); } }
return 0;
}
);
# C - int fund * g / wronskian dt
my $iFgt1, $iFgt2, $iFgt3, $iFgt4;
$iFgt1=(($a2/$b2 == 3) ? Formula("ln|t|") : Formula("($b2/(3*$b2-$a2))*t^((3*$b2-$a2)/$b2)"));
$iFgt2=(($a2/$b2 == 2) ? Formula("ln|t|") : Formula("($b2/(2*$b2-$a2))*t^((2*$b2-$a2)/$b2)"));
$iFgt3=(($a1/$b1 == 3) ? Formula("ln|t|") : Formula("($b1/(3*$b1-$a1))*t^((3*$b1-$a1)/$b1)"));
$iFgt4=(($a1/$b1 == 2) ? Formula("ln|t|") : Formula("($b1/(2*$b1-$a1))*t^((2*$b1-$a1)/$b1)"));
my @iFg = ( Formula("($c*$iFgt1+$d*$iFgt2)/($Wcoef)")->reduce,
Formula("($c*$iFgt3+$d*$iFgt4)/($Wcoef)")->reduce );
$multiansD = MultiAnswer($iFg[0], $iFg[1])->with(
singleResult => 1,
checkTypes => 0,
allowBlankAnswers => 1,
format =>
"<table border='0' cellspacing='0'>
<tr><td>\(\int \frac{y_1 g}{W} dt = \) %s </td></tr>
<tr><td>\(\int \frac{y_2 g}{W} dt = \) %s </td></tr>
</table>",
tex_format =>
"\begin{array}{l}
\displaystyle \int \frac{y_1 g}{W} dt = %s \\
\displaystyle \int \frac{y_2 g}{W} dt = %s
\end{array}",
checker => sub {
my ($correct, $student, $answerHash ) = @_;
my @c = @{$correct};
my @s = @{$student};
my $msg = ""; my @flag = (0,0);
if ($c[0]->typeMatch($s[0]) &&
$c[1]->typeMatch($s[1])) {
my @dS = (Formula("$s[0]")->D('t'),
Formula("$s[1]")->D('t'));
if (Formula("$dS[0]*$S_wron") == Formula("$S_fund[0]*$real_g")) {
$flag[0] = 1;
$msg = "First integral is correct $BR";
} else {
$msg = "First integral is incorrect! $BR";
} if (Formula("$dS[1]*$S_wron") == Formula("$S_fund[1]*$real_g")) {
$flag[1] = 1;
$msg = $msg . "Second integral is correct";
} else {
my $tmp1 = Formula("$s[1]")->D('t')->reduce;
my $tmp2 = Formula("$sFw[1]*$g")->reduce;
$msg = $msg . "Second integral is incorrect!";
}
}
if ($flag[0]*$flag[1] == 1) { return 1; }
else { if (length($msg)>1) { Value::Error($msg); } }
return 0;
}
);
# D - general solution
$gen_soln = Formula("($iFg[0]+c2)*$y2 - ($iFg[1]+c1)*$y1")->reduce;
$multiansE = MultiAnswer($gen_soln)->with(
singleResult => 1,
checkTypes => 1,
allowBlankAnswers => 0,
checker => sub{
my ($correct, $student, $answerHash ) = @_;
my @s = @{$student};
my $msg = ""; my @flag = (0,0,0);
my @S_h = (Formula("$s[0]")->D("c1"),
Formula("$s[0]")->D("c2"));
my @dS_h = ($S_h[0]->D('t'),
$S_h[1]->D('t'));
my @ddS_h = ($S_h[0]->D('t','t'),
$S_h[1]->D('t','t'));
if (Formula("$S_h[0]*$dS_h[1]") == Formula("$S_h[1]*$dS_h[0]")) {
Value::Error("Not the most general answer...");
}
if (Formula("$coef[0]*t^2*$ddS_h[0] + $coef[1]*t*$dS_h[0] + $coef[2]*$S_h[0]") == Formula("0")) {
$msg = "\(y_1\) is okay$BR";
$flag[0] = 1;
} else {
$msg = "\(y_1\) is wrong$BR";
}
if (Formula("$coef[0]*t^2*$ddS_h[1] + $coef[1]*t*$dS_h[1] + $coef[2]*$S_h[1]") == Formula("0")) {
$msg = $msg . "\(y_2\) is okay$BR";
$flag[1] = 1;
} else {
$msg = $msg . "\(y_2\) is wrong$BR";
}
my $S_p = Formula("$s[0]")->substitute("c1"=>"0","c2"=>"0");
my $dS_p = $S_p->D('t');
my $ddS_p = $S_p->D('t','t');
my $P = Formula("$coef[0]*t^2*$ddS_p + $coef[1]*t*$dS_p + $coef[2]*$S_p");
if ($P == Formula("$g")) {
$msg = $msg . "Particular solution is okay";
$flag[2] = 1;
} else {
$msg = $msg . "Not a solution to the DE!";
}
if ($flag[0]*$flag[1]*$flag[2] != 1) {
if (length($msg) > 1) { Value::Error($msg); }
} else {
return 1;
}
return 0;
}
);
#####################################
# Main text
Context()->texStrings;
BEGIN_TEXT
$Message
$PAR
In this problem you will use variation of parameters to solve the nonhomogeneous equation
\[ $L = $g \]
$PAR
$HR
$PAR
$BBOLD A. $EBOLD
Plug \(y=t^n\) into the associated ${BBOLD}homogeneous${EBOLD} equation (with
"\(0\)" instead of "\($g\)") to get an equation with only \(t\) and \(n\).
\{ mbox([
"\(\quad\)",
ans_rule(40),
]) \}
(Note: Do not cancel out the \(t\), or webwork won't accept your answer!)
$PAR
$BBOLD B. $EBOLD
Solve the equation above for \(n\) (use \(t\neq 0\) to cancel out the \(t\)).
$BR $SPACE $SPACE
You should get two values for \(n\), which give two fundamental solutions
of the form \(y = t^n\).
\{ mbox([
"\( \quad \phantom{\mathrm{W}(y_1,\,)} y_1 = \)",
$multiansB->ans_rule(10),
"\(\qquad y_2 = \)",
$multiansB->ans_rule(10)
]) \}
\{ mbox([
"\(\quad \mathrm{W}\bigl(y_1, y_2\bigr) = \)",
$multiansB->ans_rule(20)
]) \}
$PAR
$BBOLD C. $EBOLD
To use variation of parameters, the linear differential equation must be written in
standard form \(y'' + py' + qy = g\).
$BR $SPACE $SPACE
What is the function \(g\)?
\{ mbox ([
"\( \qquad g(t) = \)",
ans_rule(10)
]) \}
$PAR
$BBOLD D. $EBOLD
Compute the following integrals.
\{ mbox ([
"\( \quad \displaystyle \int \frac{y_1 g}{W} dt = \)",
$multiansD->ans_rule(35)
]) \}
\{ mbox ([
"\( \quad \displaystyle \int \frac{y_2 g}{W} dt = \)",
$multiansD->ans_rule(35)
]) \}
$PAR
$BBOLD E. $EBOLD
Write the general solution.
(Use ${BTT}c1${ETT} and ${BTT}c2${ETT} for \(c_1\) and \(c_2\)).
\{ mbox ([
"\( \quad y = \)",
$multiansE->ans_rule(70)
]) \}
END_TEXT
$showHint = 3;
BEGIN_TEXT
$HR
$PAR
If you don't get this in $showHint tries, you can get a hint to help you
find the fundamental solutions.
END_TEXT
BEGIN_HINT
$PAR
$SPACE $SPACE \(\bullet\) The first two parts go as follows:
\[ \begin{array}{lrl}
\text{homogeneous } \rlap{\text{equation:}}
& $L &= \ \ 0 \\
\text{plug in }y=t^n: \quad
& $eqn_hint1 &= \ \ 0 \\
\text{factor out }t^n:
& $eqn_hint2 &= \ \ 0 \\
\text{cancel }t^n:
& $eqn_hint25 &= \ \ 0 \\
\text{simplify}:
& $eqn_hint3 &= \ \ 0 \\
\text{factor}:
& $eqn_hint4 &= \ \ 0 \\
\end{array} \]
\[ \text{fundamental solutions:}
\quad y_1 = $y1, \quad y_2 = $y2 \]
$PAR
$SPACE $SPACE \(\bullet\) You are on your own for the rest. ;)
END_HINT
Context()->normalStrings;
#####################################
# Answer evaluation
$showPartialCorrectAnswers = 1;
ANS( equation_cmp("$eqn = 0", vars=>['t','n']) );
ANS( $multiansB->cmp() );
ANS( $real_g->cmp() );
ANS( $multiansD->cmp() );
ANS( $multiansE->cmp() );
COMMENT("MathObject version. Randomized -- currently integer powers (rational powers don't check right).");
ENDDOCUMENT();