version 1.1, 2009/04/18 23:43:54
|
version 1.3, 2009/07/06 17:12:47
|
Line 1
|
Line 1
|
<problem> |
<problem> |
|
|
<script type="loncapa/perl"> |
<script type="loncapa/perl"> |
$seed=&random(1,500,1); |
$seed=&random(1,500,1); |
$n=&random(15,25,1); |
$n=&random(15,25,1); |
$offset=&random(2,5,0.1); |
$offset=&random(2,5,0.1); |
$slope=&random(0.6,2.5,0.1); |
$slope=&random(0.6,2.5,0.1); |
# construct a data set using R |
# construct a data set using R |
$data=&cas('R',"set.seed($seed);x<-1:$n;w<-1+sqrt(x)/2;data.frame(x=x,y=$offset+$slope*x+rnorm(x)*w);"); |
# dump is for debugging, print to screen to see data structure |
# separate into an x and a y vector |
($data,$dump)=&cas_hashref('R',"set.seed($seed);x<-1:$n;w<-1+sqrt(x)/2;data.frame(x=x,y=$offset+$slope*x+rnorm(x)*w);"); |
@datatable=split(/\s+/gs,$data); |
@x=&cas_hashref_array($data,'x'); |
for ($i=0;$i<2*$n;$i+=2) { |
@y=&cas_hashref_array($data,'y'); |
push(@x,$datatable[$i]); |
|
push(@y,$datatable[$i+1]); |
|
} |
|
$datax=join(',',@x); |
$datax=join(',',@x); |
$datay=join(',',@y); |
$datay=join(',',@y); |
# calculate the right answer using R |
# calculate the right answer using R |
($intercept,$slope)=split(/\s+/,&cas('R',"x<-c($datax);y<-c($datay);dataset<-data.frame(x=x,y=y);fm<-lm(y~x,data=dataset);array(coef(fm),dim=2);")); |
($answerdata,$dump)=&cas_hashref('R',"x<-c($datax);y<-c($datay);dataset<-data.frame(x=x,y=y);fm<-lm(y~x,data=dataset);"); |
$answer="$slope*x+$intercept"; |
$answer=&cas_hashref_entry($answerdata,'coefficients','x').'*x+'.&cas_hashref_entry($answerdata,'coefficients','(Intercept)'); |
</script> |
</script> |
<gnuplot width="400" solid="0" plotcolor="monochrome" gridlayer="off" bmargin="default" font="9" alttag="dynamically generated plot" bgcolor="xffffff" texfont="22" transparent="off" plottype="Cartesian" rmargin="default" gridtype="Cartesian" minor_ticscale="0.5" fontface="sans-serif" grid="on" align="right" texwidth="93" height="300" border="on" samples="100" fgcolor="x000000" major_ticscale="1" tmargin="default" lmargin="default" fillstyle="empty"> |
|
|
<startouttext /> |
|
Consider the plotted data set.<br /> |
|
<endouttext /> |
|
|
|
<gnuplot width="400" solid="0" plotcolor="monochrome" gridlayer="off" bmargin="default" font="9" alttag="dynamically generated plot" bgcolor="xffffff" texfont="22" transparent="off" plottype="Cartesian" rmargin="default" gridtype="Cartesian" minor_ticscale="0.5" fontface="sans-serif" grid="on" align="left" texwidth="93" height="300" border="on" samples="100" fgcolor="x000000" major_ticscale="1" tmargin="default" lmargin="default" fillstyle="empty"> |
<curve linestyle="points" linetype="solid" color="x000000" pointtype="3" limit="closed" pointsize="2" linewidth="1"> |
<curve linestyle="points" linetype="solid" color="x000000" pointtype="3" limit="closed" pointsize="2" linewidth="1"> |
<data>@x</data> |
<data>@x</data> |
<data>@y</data> |
<data>@y</data> |
</curve> |
</curve> |
|
|
</gnuplot> |
</gnuplot> |
|
|
<startouttext /> |
<startouttext /> |
Consider the data set |
<br /> |
<pre>$data</pre> |
|
|
|
Give a linear function approximating the data.<br /> |
Give a linear function approximating the data.<br /> |
<tt>y(x)=</tt> |
<tt>y(x)=</tt> |
<endouttext /> |
<endouttext /> |
<formularesponse answer="$answer" samples="x@1:$n#20"> |
|
|
<formularesponse id="11" answer="$answer" samples="x@1:$n#20"> |
<responseparam name="tol" default="2%" description="Numerical Tolerance" type="tolerance" /> |
<responseparam name="tol" default="2%" description="Numerical Tolerance" type="tolerance" /> |
<textline size="25" readonly="no" /> |
<textline size="25" readonly="no" /> |
|
|
</formularesponse> |
</formularesponse> |
|
|
</problem> |
</problem> |