PREP 2014 Question Authoring - Archived

removing multiple variables - context vector

removing multiple variables - context vector

by Joel Trussell -
Number of replies: 1
I'm using formulas within the vector context to define difference equations - e.g., $f1 = $f2, where $f1 is the AR part and $f2 is the MA part. In DSP this is usually written
    y(​n) = a1*y(n-1) + ... aN*y(n-N) + b0*x(​n) + ... + bM*x(n-M). 
In the vector context, x, y, z are standard variables. So I remove them, add the variable n, and define dummy functions x(​n), y(​n). The problem is that the equation comes outyn = and not y(​n) =, The x(​n) seems fine. I've tried various modifications with various errors, but nothing works. I can change y(​n) to q(​n) and that works, but I really need yno to agree with standard texts.

code below

DOCUMENT();        # This should be the first executable line in the problem.

loadMacros(
   "PGstandard.pl",     # Standard macros for PG language
   "MathObjects.pl",
   "AnswerFormatHelp.pl",
   "parserFunction.pl",
   "PGcourse.pl",
);

TEXT(&beginproblem);
$showPartialCorrectAnswers = 1;

Context("Vector");
Context()->variables->remove(x); # only variables should be n 
Context()->variables->add(n=> 'Real');  
# how to remove y and z from Vector context?


# Component values here
parserFunction("q(​n)" => "n"); # dummy function
parserFunction("x(​n)" => "n"); # dummy function

$Na1 = random(3,6,1);
do {$Nb1 = random(3,6,1);} until ($Nb1 != $Na1);
$Nmax = 6;
for ($k =1;$k <= $Nmax;$k++) {$a1[$k]   =0};   
for ($k =1;$k <= $Nmax;$k++) {$b1[$k]   =0};   

$a1[0]    = non_zero_random(-9,9,1);   # a0
for ($k =1;$k <= $Na1;$k++) {$a1[$k]   = random(-5,5,1)};   

$b1[0]    = non_zero_random(-9,9,1);   # b0
for ($k =1;$k <= $Nb1;$k++) {$b1[$k]   = non_zero_random(-5,5,1)};   

for ($k =1;$k <= $Nmax;$k++) {$a1ans[$k]   =0};   
for ($k =1;$k <= $Nmax;$k++) {$b1ans[$k]   =0};   
$a1ans[0]   = 1;
for ($k =1;$k <= $Na1;$k++) {$a1ans[$k]   = -$a1[$k]/$a1[0]};   
for ($k =0;$k <= $Nb1;$k++) {$b1ans[$k]   = $b1[$k]/$a1[0]};   

$f1 = Formula("$a1[0]*y(​n) + $a1[1]*y(n-1) + $a1[2]*y(n-2) + $a1[3]*y(n-3) +
     $a1[4]*y(n-4) + $a1[5]*y(n-5) + $a1[6]*y(n-6) ")->reduce;
$f2 = Formula("$b1[0]*x(​n) + $b1[1]*x(n-1) + $b1[2]*x(n-2) + $b1[3]*x(n-3) +
      $b1[4]*x(n-4) + $b1[5]*x(n-5) + $b1[6]*x(n-6) ")->reduce;

$Na2 = 1;
$Nb2 = random(4,6,1);
for ($k =1;$k <= $Nmax;$k++) {$a2[$k]   =0};   
for ($k =1;$k <= $Nmax;$k++) {$b2[$k]   =0};   
$b2[0]    = non_zero_random(-9,9,1);   # b0
for ($k =1;$k <= $Nb2;$k++) {$b2[$k]   = random(-4,4,1)};   
$Nmax = 6;

$f3 = Formula("y(​n)")->reduce;
$f4 = Formula("$b2[0]*x(​n) + $b2[1]*x(n-1) + $b2[2]*x(n-2) + $b2[3]*x(n-3) +
      $b2[4]*x(n-4) + $b2[5]*x(n-5) + $b2[6]*x(n-6) ")->reduce;

$a2ans[0]   = 1;
for ($k =1;$k <= $Na1;$k++) {$a2ans[$k]   = -$a2[$k]};   
for ($k =0;$k <= $Nb1;$k++) {$b2ans[$k]   = $b1[$k]};   


Context()->texStrings;
TEXT(EV2(<<EOT));
\{image("direct_form_2.jpg")\} $BR
This Problem is related to Problem 2.46 in the text.
$PAR
The problem is to determine the coefficients for the Direct Form 
II (figure above). For this problem, we will assume the maximum 
delay is $Nmax. You will need to find all coefficients. There are 
likely to be several zero coefficients, if the  orders of the AR and 
MA parts are not equal. 

$PAR
Give the coefficients of Direct Form II for  the difference equation , 
$PAR
a)  \( $f1 = \) $BR
\($f2\)

$PAR
For the coefficients, enter as vectors, \( {\bf a} = <a_0, a_1, ..., a_6>\) $BR
and \( {\bf b} = <b_0, b_1, ..., b_6>\) $BR
Note that some of the coefficients may be zero$BR
\({\bf a}  =\) \{ans_rule(40)\}  \{ AnswerFormatHelp("vectors") \} $BR
\({\bf b} = \) \{ans_rule(40)\}  \{ AnswerFormatHelp("vectors") \} 

$PAR
b)  \( $f3 = $f4\)

$PAR
For the coefficients, enter as vectors, \( {\bf a} = <a_0, a_1, ..., a_6>\) $BR
and \( {\bf b} = <b_0, b_1, ..., b_6>\) $BR
Note that some of the coefficients may be zero$BR
\({\bf a}  =\) \{ans_rule(40)\}  \{ AnswerFormatHelp("vectors") \} $BR
\({\bf b} = \) \{ans_rule(40)\}  \{ AnswerFormatHelp("vectors") \} 

EOT



ANS(Vector(@a1ans)->cmp());
ANS(Vector(@b1ans)->cmp());
ANS(Vector(@a2ans)->cmp());
ANS(Vector(@b2ans)->cmp());

SOLUTION(EV3(<<'END_SOLUTION'));
$PAR
$BBOLD  SOLUTION $EBOLD
$PAR
tmp holder

END_SOLUTION


ENDDOCUMENT();        # This should be the last executable line in the problem.
In reply to Joel Trussell

Re: removing multiple variables - context vector

by Davide Cervone -
One way to remove the variables from the context is
    Context()->variables->remove('x','y','z');
Another would be
    Context()->variables->clear();
But if you are adding new variables, the easier is to use are() rather than add(), which first removes all the existing variables and adds the new ones. So replace
    Context()->variables->remove(x); # only variables should be n 
    Context()->variables->add(n=> 'Real');  
    # how to remove y and z from Vector context?
with
    Context()->variables->are(n => 'Real');
There are several other things you probably want to change as well. First, you probably want to disable a coupe of reduction rules that reorder or regroup the terms of your expressions. You can use
    Context()->noreduce("(-x)+y","(-x)-y");
to do that. This should keep your terms in the same order you gave them originally.

Next, it looks like you have used a pretty old problem as a starting point, so have some old-style code that should be updated.

Change

    TEXT(&beginproblem);
to
    TEXT(beginproblem());
Change
    TEXT(EV2(<<EOT));
    ...
    EOT
to
   BEGIN_TEXT
   ...
   END_TEXT
and
    SOLUTION(EV3(<<'END_SOLUTION'));
to
   BEGIN_SOLUTION
and that will being you more up to date.

Davide