WeBWorK Problems

generate file with data for students to analyze?

Re: generate file with data for students to analyze?

by George Jennings -
Number of replies: 0
Thanks Felipe and Arnie,

Felipe's flash looks great but daunting to learn so I chickened out and cooked up something in javascript that seems to work pretty well. It displays a window with a tab-separated table, then students can copy and paste it into a spreadsheet. Separating with tabs seems to be the key to get it to work with
Internet Explorer. Thank, Arnie, for that suggestion.

Here is a demo that shows how it works in a very simple problem.

DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
);

## Set-up

Context("Numeric");
# intercept, slope
$b = random(2,5,.1);
$m = random(3,7);
# create fake noisy linear data (x[i],y[i]), i=0,1,...
$count = 10;
$sumx = 0; $sumy = 0;
for($i=0;$i<$count;$i++){<br /> $x[$i] = $i+random(-.4,.5,.1);<br /> $y[$i] = $b + $m*( $x[$i]+random(-.4,.5,.1) );<br /> # remove extra digits possibly caused by binary arithmetic<br /> $x[$i] = sprintf("%.1f",$x[$i]); <br /> $y[$i] = sprintf("%.1f",$y[$i]); <br /> # one line in the tab-separated data table<br /> $jsDat[$i] = '"'.join('\t',$x[$i],$y[$i]).'"';<br /> $sumx += $x[$i];<br /> $sumy += $y[$i];<br />}

# stringify table for passing to javascript
$jsDatStr = join(",",@jsDat); # data for javascript

# javascript for opening data window

HEADER_TEXT(<<EOS);
<script Xlanguage="javascript" type="text/javascript">
<!--//
function openDataWindow(){<br /> var xyRow=new Array($jsDatStr);<br /> var i=0;<br /> dataWindow=window.open('','','width=200,height=300');<br /> dataWindow.document.writeln("<pre>");<br /> dataWindow.document.writeln("x\ty");<br /> dataWindow.document.writeln("---\t---");<br /> for (i=0;i<$count;i++){<br /> dataWindow.document.writeln(xyRow[i]);<br /> }
dataWindow.document.writeln("</pre>");
}
//-->
</script>
EOS

# answers

$meanx = Compute("$sumx/$count");
$meany = Compute("$sumy/$count");

## Main text

TEXT(beginproblem());
Context()->texStrings;
BEGIN_TEXT
Here are some fake data.
\{ begintable(12) \}
\{ row( "x", @x)\}
\{ row( "y", @y)\}
\{ endtable() \}
Open the \{htmlLink("javascript:openDataWindow();","easy copy")\} window,
copy-and-paste the data from the window into a spreadsheet or other application, then use the application to find the mean of \(x\) and the mean of \(y\).
$PAR
\(\bar{x}\) = \{ans_rule(10)\} $SPACE \(\bar{y}\) = \{ans_rule(10)\}
END_TEXT
Context()->normalStrings;

## Answer evaluation

ANS($meanx->cmp());
ANS($meany->cmp());

ENDDOCUMENT();

--George