--- loncom/homework/functionplotresponse.pm 2010/11/13 22:30:28 1.38 +++ loncom/homework/functionplotresponse.pm 2010/11/20 00:14:44 1.42 @@ -1,7 +1,7 @@ # LearningOnline Network with CAPA # option list style responses # -# $Id: functionplotresponse.pm,v 1.38 2010/11/13 22:30:28 www Exp $ +# $Id: functionplotresponse.pm,v 1.42 2010/11/20 00:14:44 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -47,7 +47,7 @@ sub geogebra_startcode { my ($id)=@_; return (< + codebase="/adm/geogebra/" width="722" height="447" MAYSCRIPT> ENDSTARTCODE } @@ -61,7 +61,7 @@ sub geogebra_endcode { # sub geogebra_spline_program { return (< + ENDSPLINEPROGRAM } @@ -74,9 +74,8 @@ sub geogebra_default_parameters { - - - + + @@ -159,16 +158,40 @@ sub update_register { } # -# Set a coordinate variable +# Set a point coordinate variable # -sub set_coordinate { +sub set_point_coordinate { my ($id,$variable,$x,$y)=@_; return (<'. + ['2','Second derivative'], + ['-1','Integral']],$token).'
'. &Apache::edit::text_arg('(Initial) x-value:','xinitial', $token,'8'). &Apache::edit::select_or_text_arg('(Initial) x-value label:','xinitiallabel', @@ -566,8 +629,8 @@ sub get_answer_from_form_fields { sub cubic_hermite { my ($t,$p1,$s1,$p2,$s2)=@_; - return (2.*$t*$t*$t-3.*$t*$t+1.)*$p1 + 5.*($t*$t*$t-2.*$t*$t+$t)*($s1-$p1)+ - (-2.*$t*$t*$t+3.*$t*$t) *$p2 + 5.*($t*$t*$t-$t*$t) *($s2-$p2); + return (2.*$t*$t*$t-3.*$t*$t+1.)*$p1 + 3.*($t*$t*$t-2.*$t*$t+$t)*($s1-$p1)+ + (-2.*$t*$t*$t+3.*$t*$t) *$p2 + 3.*($t*$t*$t-$t*$t) *($s2-$p2); } # @@ -576,8 +639,8 @@ sub cubic_hermite { sub ddt_cubic_hermite { my ($t,$p1,$s1,$p2,$s2)=@_; - return (6.*$t*$t-6.*$t) *$p1 + 5.*(3.*$t*$t-4.*$t+1.)*($s1-$p1)+ - (-6.*$t*$t+6.*$t)*$p2 + 5.*(3.*$t*$t-2.*$t) *($s2-$p2); + return (6.*$t*$t-6.*$t) *$p1 + 3.*(3.*$t*$t-4.*$t+1.)*($s1-$p1)+ + (-6.*$t*$t+6.*$t)*$p2 + 3.*(3.*$t*$t-2.*$t) *($s2-$p2); } # @@ -586,8 +649,8 @@ sub ddt_cubic_hermite { sub d2dt2_cubic_hermite { my ($t,$p1,$s1,$p2,$s2)=@_; - return (12.*$t-6.) *$p1 + 5.*(6.*$t-4.)*($s1-$p1)+ - (-12.*$t+6.)*$p2 + 5.*(6.*$t-2.)*($s2-$p2); + return (12.*$t-6.) *$p1 + 3.*(6.*$t-4.)*($s1-$p1)+ + (-12.*$t+6.)*$p2 + 3.*(6.*$t-2.)*($s2-$p2); } # @@ -699,22 +762,7 @@ sub start_functionplotresponse { &decode_previous_answer($Apache::lonhomework::history{"resource.$partid.$id.submission"}); # Parameters of - my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval); - $xmin=(defined($xmin)?$xmin:-10); - my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval); - $xmax=(defined($xmax)?$xmax:10); - my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval); - $ymin=(defined($ymin)?$ymin:-10); - my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval); - $ymax=(defined($ymax)?$ymax:10); - if ($xmax<=$xmin) { - &Apache::lonxml::warning('Maximum x-value needs to be larger than minimum value.'); - $xmax=$xmin+20; - } - if ($ymax<=$ymin) { - &Apache::lonxml::warning('Maximum y-value needs to be larger than minimum value.'); - $ymax=$ymin+20; - } + my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval); my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false'); my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false'); my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false'); @@ -848,7 +896,7 @@ sub functionplotrulecheck { =split(/\:/,$rule); $percent=($percent>0?$percent:5); &addlog("================="); - &addlog("Rule $label for ".('function itself','first derivative','second derivative')[$derivative]." $relationship $value"); + &addlog("Rule $label for ".($derivative<0?'integral':('function itself','first derivative','second derivative')[$derivative])." $relationship $value"); my $li=0; my $lh=400; @@ -905,14 +953,37 @@ sub functionplotrulecheck { $tol=4.*$tol/($xmax-$xmin); } elsif ($derivative==1) { $tol=2.*$tol/($xmax-$xmin); + } elsif ($derivative==-1) { + $tol=$tol*($xmax-$xmin)/2.; } } - for (my $i=$li; $i<=$lh; $i++) { + my $integral=0; + my $binwidth=($xmax-$xmin)/400.; + if (($derivative<0) && (!$findupper)) { +# definite integral, calculate over whole length + &addlog("Calculating definite integral"); + for (my $i=$li; $i<=$lh; $i++) { + $integral+=$Apache::functionplotresponse::func[$i]*$binwidth; + } + unless (&compare_rel($relationship,$value,$integral,$tol)) { + &addlog("Actual integral ".(defined($integral)?$integral:'undef').", expected $value, tolerance $tol"); + &addlog("Rule $label failed."); + my $hintlabel=$label; + $hintlabel=~s/^R//; + push(@Apache::functionplotresponse::failedrules,$hintlabel); + &addlog("Set hint condition $hintlabel"); + return 0; + } + } else { + for (my $i=$li; $i<=$lh; $i++) { my $val; if ($derivative==2) { $val=$Apache::functionplotresponse::d2funcdx2[$i]; } elsif ($derivative==1) { $val=$Apache::functionplotresponse::dfuncdx[$i]; + } elsif ($derivative==-1) { + $integral+=$Apache::functionplotresponse::func[$i]*$binwidth; + $val=$integral; } else { $val=$Apache::functionplotresponse::func[$i]; } @@ -946,6 +1017,7 @@ sub functionplotrulecheck { return 0; } } + } } &addlog("Rule $label passed."); return 1; @@ -981,14 +1053,7 @@ sub end_functionplotruleset { # # Actually grade # - my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval,-2); - $xmin=(defined($xmin)?$xmin:-10); - my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval,-2); - $xmax=(defined($xmax)?$xmax:10); - my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval,-2); - $ymin=(defined($ymin)?$ymin:-10); - my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval,-2); - $ymax=(defined($ymax)?$ymax:10); + my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2); my $ad=''; undef %Apache::functionplotresponse::functionplotrulelabels; @@ -1068,8 +1133,9 @@ sub end_functionplotelements { $result=&Apache::edit::end_table(); } elsif ($target eq 'web') { # Now is the time to render all of the stored splines + my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2); foreach my $label (keys(%Apache::functionplotresponse::splineorder)) { - $result.=&generate_spline($internalid,$label); + $result.=&generate_spline($internalid,$label,$xmin,$xmax,$ymin,$ymax); } # close the init script $result.=&end_init_script(); @@ -1094,6 +1160,25 @@ sub end_functionplotelements { return $result; } +sub boundaries { + my ($parstack,$safeeval,$level)=@_; + my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval,$level); + $xmin=(defined($xmin)?$xmin:-10); + my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval,$level); + $xmax=(defined($xmax)?$xmax:10); + my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval,$level); + $ymin=(defined($ymin)?$ymin:-10); + my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval,$level); + $ymax=(defined($ymax)?$ymax:10); + if ($xmax<=$xmin) { + $xmax=$xmin+20; + } + if ($ymax<=$ymin) { + $ymax=$ymin+20; + } + return ($xmin,$xmax,$ymin,$ymax); +} + sub start_functionplotelements { my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_; my $result=''; @@ -1113,20 +1198,7 @@ sub start_functionplotelements { .&Apache::edit::start_spanning_row() ."\n"; } elsif ($target eq 'web') { - my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval,-2); - $xmin=(defined($xmin)?$xmin:-10); - my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval,-2); - $xmax=(defined($xmax)?$xmax:10); - my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval,-2); - $ymin=(defined($ymin)?$ymin:-10); - my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval,-2); - $ymax=(defined($ymax)?$ymax:10); - if ($xmax<=$xmin) { - $xmax=$xmin+20; - } - if ($ymax<=$ymin) { - $ymax=$ymin+20; - } + my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2); my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false'); my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false'); my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');