File:  [LON-CAPA] / loncom / homework / templates / Rnumerical.problem
Revision 1.1: download - view: text, annotated - select for diffs
Sat Apr 18 23:43:54 2009 UTC (15 years, 2 months ago) by www
Branches: MAIN
CVS tags: bz5969, HEAD, BZ5971-printing-apage, BZ5434-fox
* Some more blacklisted stuff
* Eliminate debugging code
* Templates for using R

    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
    8: $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);");
    9: # separate into an x and a y vector
   10: @datatable=split(/\s+/gs,$data);
   11: for ($i=0;$i<2*$n;$i+=2) {
   12:     push(@x,$datatable[$i]);
   13:     push(@y,$datatable[$i+1]);
   14: }
   15: $datax=join(',',@x);
   16: $datay=join(',',@y);
   17: # calculate the right answer using R
   18: ($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);"));
   19: $answer="$slope*x+$intercept";
   20: </script>
   21: <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">
   22:     <curve linestyle="points" linetype="solid" color="x000000" pointtype="3" limit="closed" pointsize="2" linewidth="1">
   23:         <data>@x</data>
   24:         <data>@y</data>
   25:     </curve>
   26:     
   27: </gnuplot>
   28: 
   29: <startouttext />
   30: Consider the data set
   31: <pre>$data</pre>
   32: 
   33: Give a linear function approximating the data.<br />
   34: <tt>y(x)=</tt>
   35: <endouttext />
   36: <formularesponse answer="$answer" samples="x@1:$n#20">
   37:     <responseparam name="tol" default="2%" description="Numerical Tolerance" type="tolerance" />
   38:     <textline size="25" readonly="no" />
   39:     
   40: </formularesponse>
   41: </problem>

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>