Annotation of loncom/homework/templates/Rnumerical.problem, revision 1.2
1.1 www 1: <problem>
2: <script type="loncapa/perl">
3: $seed=&random(1,500,1);
4: $n=&random(15,25,1);
5: $offset=&random(2,5,0.1);
6: $slope=&random(0.6,2.5,0.1);
7: # construct a data set using R
1.2 ! www 8: # dump is for debugging, print to screen to see data structure
! 9: ($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);");
! 10: @x=&cas_hashref_array($data,'x');
! 11: @y=&cas_hashref_array($data,'y');
1.1 www 12: $datax=join(',',@x);
13: $datay=join(',',@y);
14: # calculate the right answer using R
1.2 ! www 15: ($answerdata,$dump)=&cas_hashref('R',"x<-c($datax);y<-c($datay);dataset<-data.frame(x=x,y=y);fm<-lm(y~x,data=dataset);");
! 16: $answer=&cas_hashref_entry($answerdata,'coefficients','x').'*x+'.&cas_hashref_entry($answerdata,'coefficients','(Intercept)');
1.1 www 17: </script>
1.2 ! www 18:
! 19: <startouttext />
! 20: Consider the plotted data set.<br />
! 21: <endouttext />
! 22: <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">
1.1 www 23: <curve linestyle="points" linetype="solid" color="x000000" pointtype="3" limit="closed" pointsize="2" linewidth="1">
24: <data>@x</data>
25: <data>@y</data>
26: </curve>
27:
28: </gnuplot>
29:
30: <startouttext />
1.2 ! www 31: <br />
1.1 www 32: Give a linear function approximating the data.<br />
33: <tt>y(x)=</tt>
34: <endouttext />
1.2 ! www 35: <formularesponse id="11" answer="$answer" samples="x@1:$n#20">
1.1 www 36: <responseparam name="tol" default="2%" description="Numerical Tolerance" type="tolerance" />
37: <textline size="25" readonly="no" />
38:
39: </formularesponse>
40: </problem>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>