PREP 2015 Question Authoring - Archived

Using 'if' in a Solution

Using 'if' in a Solution

by Daniele Arcara -
Number of replies: 5
I am working on a geometric series problem, and I wanted to vary the number where the series count begins. In other words, instead of doing the series from 'n=0', I want to do it from 'n=1' or 'n=2'. However, I also wanted the number to change between 1 and 2 randomly, so that students working together would not quite have it so easy.

In the code, this is simple, and I just put an 'if' statement to change the answer accordingly. However, how do I write my solution? The first part of the solution is the same, but then, at the end, I would change the last line depending on whether the series started at 'n=1' or 'n=2'. Is there a way to do that? Some sort of statement that writes one thing in the solution if n=1, and another thing if n=2.

Here is my current code:




$sum0=1/(1 - $a/$b);
$sum1=$sum0 - 1;
$sum2=$sum1 - $a/$b;
if ($c == 1)
  { $answer = $sum1; }
  { $answer = $sum2; }


Determine whether the following series is convergent or divergent.
If it is convergent, enter the sum of the series.
If it is divergent, enter "divergent" (without quotes).

[` \displaystyle \sum_{n= [$c] }^{\infty} \frac{[$a]^n}{[$b]^n} = `] [_____________]{$answer}



The series simplifies as [` \displaystyle \sum_{n= [$c] }^{\infty} \frac{[$a]^n}{[$b]^n} = \sum_{n= [$c] }^{\infty} \left( \frac{[$a]}{[$b]} \right)^n `].
Since the absolute value of the ratio [` \displaystyle \frac{[$a]}{[$b]} `] is less than 1, the series is convergent.

The full sum starting at [` n=0 `] is [` \displaystyle \sum_{n=0}^{\infty} \left( \frac{[$a]}{[$b]} \right)^n = \frac1{\displaystyle 1 - \frac{[$a]}{[$b]} } `].
Therefore, [` \displaystyle \sum_{n= [$c] }^{\infty} \left( \frac{[$a]}{[$b]} \right)^n `] ….. TO BE FINISHED …..


In reply to Daniele Arcara

Re: Using 'if' in a Solution

by Valerio De Angelis -
it may be easier to write the series as a function of the starting point,
sum = (a/b)^c /(1-a/b)
Then you can use also other values for c, and you  don't have to write the if statement.

In reply to Valerio De Angelis

Re: Using 'if' in a Solution

by Gavin LaRose -
Hi Daniele,

Another option is to have the text for the two different cases in a variable, for example:

  if ( $case1 ) {
      $solution_text =<<EOS
  Text for the solution in case 1.
  It is very grand.
  } else {
      $solution_text =<<EOS
  Text for the solution in case 2.
  It is very fabulous.
  The solution is in this case clearly [$solution_text].

In this case I've used the "here" text to include multi-line variables without having to do multiple concatenations. One could just do a regular variable assignment as well:

  if ( $case1 ) {
      $solution_text = "This is the solution for case 1";
  } else {
      $solution_text = "This is the solution for case 2";


In reply to Gavin LaRose

Re: Using 'if' in a Solution

by Davide Cervone -
Gavin's solution is very nice. One additional comment is that if your $solution_text includes PGML commands to be processed (like mathematics), you should use [$solution_text]** with two stars when you insert it into your solution text. That will ask PGML to continue to process the value of the substitution for additional PGML commands.
In reply to Davide Cervone

Re: Using 'if' in a Solution

by Daniele Arcara -
Thank you, Davide. My solution did have some math in it, and I had gotten around it by defining $solution_text1, $solution_math1, $solution_text2, and so on…

This is a lot simpler, though!