MatchingDynamicGraphs

From WeBWorK_wiki
Jump to navigation Jump to search
This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

This problem has been replaced with a newer version of this problem


Matching Problems With Dynamic Graphic Images


This PG code shows how make a matching problem that uses dynamically generated images.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();
loadMacros(
"PGstandard.pl",
"PGgraphmacros.pl",
"PGunion.pl",
"imageChoice.pl",
);

TEXT(beginproblem());
$refreshCachedImages = 1;

Initialization: We need to include the macros file imageChoice.pl and a few others.

$a = random(1,4,1);
$b = random(1,4,1);

# four sequences
@seq = ();
$seq[0] = "s_n = $a - $b/n";
$seq[1] = "s_n = $a + (-1)^n/n";
$seq[2] = "s_n = $a/n";
$seq[3] = "s_n = $a + $b/n";

# handy routine for generating sequence values
sub sn { 
    my ( $snum, $nval ) = @_;
    if ( $snum == 0 )    { return( $a - $b/$nval ); }
    elsif ( $snum == 1 ) { return( $a + ((-1)**$nval)/$nval ); }
    elsif ( $snum == 2 ) { return( $a/$nval ); }
    elsif ( $snum == 3 ) { return( $a + $b/$nval ); }
    else { return(1); } # shouldn't ever happen; bail out
}

# make graphs for each
@gr = ();
foreach my $i (0..3) {
    $gr[$i] = init_graph(-1,-1,10,($a+$b),'axes'=>[0,0],'grid'=>[1,1]);
    foreach my $j (1..10) {
	$gr[$i]->stamps(closed_circle($j, sn($i, $j), 'blue') );
    }
}

@QA = ();
foreach my $i (0..3) { push( @QA, "\($seq[$i]\)",  $gr[$i] ); }

$ml = new_image_match_list(
  link => 1,                #  do not link to separate image
  size => [200,200],        #  image size in pixels
  tex_size => 450,          #  tex size in precent times 10
  columns => 2,             #  number of columns
  separation => 20,         #  separation between image columns 
);

$ml->rf_print_q(~~&pop_up_list_print_q); # use pop-up-lists
$ml->ra_pop_up_list([ No_answer=>"?", A=>"A", B=>"B", C=>"C", D=>"D"] );

$ml->qa(@QA);               #  set the questions and answers
$ml->choose(4);             #  select 4 of them
#$ml->choose_extra(0);      #  and show the other 1

Setup: We define a few sequences and a subroutine for calculating them. (We could have used MathObjects formulas to do this, but decided not to.) Then, we define an array of graphs and add the plots of the sequences to them. We create an array QA of question and answer pairs, where each question is a function and each answer is a graph. Notice that the array QA contains the graph objects $gr[?], not the image files generated if we had used image(insertGraph($gr[?],...)).

We create a new_image_match_list and specify its settings. We use a pop_up_list for students to select the correct answer. We pass the array of questions and answers @QA to the matching list using $ml->qa(@QA);.

BEGIN_TEXT
\{
ColumnTable(
"Match the graphs with the corresponding formulas.".
$ml->print_q() # no period!
, # comma!
$BCENTER.
$ml->print_a().
$BR.
"(Click on a graph to enlarge it)".
$ECENTER # no period!
, # comma!
indent => 0, separation => 30, valign => "TOP"
)
\}
END_TEXT

Main Text: We use a ColumnTable(columnA,columnB,options) to display the text and images side-by-side.

install_problem_grader(~~&std_problem_grader);
$showPartialCorrectAnswers = 0;

ANS(str_cmp($ml->ra_correct_ans));

foreach my $i (0..3) {
  $a[$i] = $ml->ra_correct_ans->[$i];
}

BEGIN_SOLUTION
$PAR SOLUTION $PAR
The formulas match as follows: 
1 matches graph $a[0], 
2 matches graph $a[1], 
3 matches graph $a[2], and
4 matches graph $a[3].
END_SOLUTION

ENDDOCUMENT();

Answer Evaluation: The answer evaluation is as expected. We can access the correct answers, store them in a variable, and use them in the solution via $ml->ra_correct_ans->[?]

Problem Techniques Index