Difference between revisions of "MatchingDynamicGraphs"

From WeBWorK_wiki
Jump to navigation Jump to search
(add historical tag and give links to newer problems.)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
{{historical}}
  +
  +
<p style="font-size: 120%;font-weight:bold">This problem has been replaced with [https://openwebwork.github.io/pg-docs/sample-problems/Misc/MatchingGraphs.html a newer version of this problem]</p>
  +
  +
 
<h2>Matching Problems With Dynamic Graphic Images</h2>
 
<h2>Matching Problems With Dynamic Graphic Images</h2>
   
Line 69: Line 74:
   
 
# make graphs for each
 
# make graphs for each
@gra = ();
+
@gr = ();
 
foreach my $i (0..3) {
 
foreach my $i (0..3) {
$gra[$i] = init_graph(-1,-1,10,($a+$b),'axes'=>[0,0],'grid'=>[1,1]);
+
$gr[$i] = init_graph(-1,-1,10,($a+$b),'axes'=>[0,0],'grid'=>[1,1]);
 
foreach my $j (1..10) {
 
foreach my $j (1..10) {
$gra[$i]->stamps(closed_circle($j, sn($i, $j), 'blue') );
+
$gr[$i]->stamps(closed_circle($j, sn($i, $j), 'blue') );
 
}
 
}
 
}
 
}
   
 
@QA = ();
 
@QA = ();
foreach my $i (0..3) { push( @QA, "\($seq[$i]\)", $gra[$i] ); }
+
foreach my $i (0..3) { push( @QA, "\($seq[$i]\)", $gr[$i] ); }
   
 
$ml = new_image_match_list(
 
$ml = new_image_match_list(
Line 100: Line 105:
 
<b>Setup:</b>
 
<b>Setup:</b>
 
We define a few sequences and a subroutine for calculating them. (We could have used MathObjects formulas to do this, but decided not to.)
 
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 <code>QA</code> of question and answer pairs, where each question is a function and each answer is a graph. Notice that the array <code>QA</code> contains the graph objects <code>$gra[?]</code>, <b>not</b> the image files generated if we had used <code>image(insertGraph($gra[?],...))</code>.
+
Then, we define an array of graphs and add the plots of the sequences to them. We create an array <code>QA</code> of question and answer pairs, where each question is a function and each answer is a graph. Notice that the array <code>QA</code> contains the graph objects <code>$gr[?]</code>, <b>not</b> the image files generated if we had used <code>image(insertGraph($gr[?],...))</code>.
 
</p>
 
</p>
 
<p>
 
<p>
We create a <code>new_image_match_list</code> and specify its settings. We use a <code>pop_up_list</code> for students to select the correct answer. We pass the array of questions and answers <code>QA</code> to the matching list using <code>$ml-&gt;qa(@QA);</code>.
+
We create a <code>new_image_match_list</code> and specify its settings. We use a <code>pop_up_list</code> for students to select the correct answer. We pass the array of questions and answers <code>@QA</code> to the matching list using <code>$ml-&gt;qa(@QA);</code>.
 
</p>
 
</p>
 
</td>
 
</td>
Line 117: Line 122:
 
ColumnTable(
 
ColumnTable(
 
"Match the graphs with the corresponding formulas.".
 
"Match the graphs with the corresponding formulas.".
$ml->print_q(), # comma!
+
$ml->print_q() # no period!
  +
, # comma!
 
$BCENTER.
 
$BCENTER.
 
$ml->print_a().
 
$ml->print_a().
 
$BR.
 
$BR.
 
"(Click on a graph to enlarge it)".
 
"(Click on a graph to enlarge it)".
$ECENTER, # comma!
+
$ECENTER # no period!
  +
, # comma!
 
indent => 0, separation => 30, valign => "TOP"
 
indent => 0, separation => 30, valign => "TOP"
 
)
 
)

Latest revision as of 11:34, 16 July 2023

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