--- loncom/homework/functionplotresponse.pm 2010/11/19 17:20:44 1.40
+++ loncom/homework/functionplotresponse.pm 2010/11/23 23:55:01 1.46
@@ -1,7 +1,7 @@
# LearningOnline Network with CAPA
# option list style responses
#
-# $Id: functionplotresponse.pm,v 1.40 2010/11/19 17:20:44 www Exp $
+# $Id: functionplotresponse.pm,v 1.46 2010/11/23 23:55:01 www Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -61,7 +61,7 @@ sub geogebra_endcode {
#
sub geogebra_spline_program {
return (<
+
ENDSPLINEPROGRAM
}
@@ -295,40 +295,68 @@ ENDAXESSCRIPT
}
sub axes_label {
- my ($id,$xlabel,$ylabel)=@_;
+ my ($id,$xmin,$xmax,$ymin,$ymax,$xlabel,$ylabel)=@_;
unless ($xlabel || $ylabel) { return ''; }
my $return='document.ggbApplet_'.$id.'.evalCommand("topRight=Corner[3]");';
if ($xlabel) {
+ if (($ymin<0) && ($ymax>0)) {
$return.=(<0)) {
$return.=(<'.
- &Apache::edit::text_arg('(Initial) x-value:','xinitial',
+ ['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',
- [['start','Start of Plot']],$token,'8').'
'.
+ &Apache::edit::select_or_text_arg('Initial x-value label:','xinitiallabel',
+ [['start','Start of Plot'],
+ ['end','End of Plot']],$token,'8').'
'.
- &Apache::edit::text_arg('Optional final x-value for ranges:','xfinal',
+ &Apache::edit::text_arg('Final x-value (optional):','xfinal',
$token,'8').
- &Apache::edit::select_or_text_arg('Optional final x-value label:','xfinallabel',
+ &Apache::edit::select_or_text_arg('Final x-value label (optional):','xfinallabel',
[['end','End of Plot']],$token,'8').'
'.
- &Apache::edit::text_arg('Optional minimum length for range:','minimumlength',
+ &Apache::edit::text_arg('Minimum length for range (optional):','minimumlength',
$token,'8').
- &Apache::edit::text_arg('Optional maximum length for range:','maximumlength',
+ &Apache::edit::text_arg('Maximum length for range (optional):','maximumlength',
$token,'8').'
'.
&Apache::edit::select_or_text_arg(&mt('Relationship:'),'relationship',
[['eq','equal'],
@@ -553,7 +592,7 @@ sub start_spline {
&Apache::edit::text_arg('Index:','index',
$token,'4').' '.
&Apache::edit::select_arg('Order:','order',
- ['2','3','4','5','6','7','8','9'],$token).' '.
+ ['2','3','4','5','6','7','8'],$token).' '.
&Apache::edit::text_arg('Initial x-value:','initx',
$token,'4').' '.
&Apache::edit::text_arg('Initial y-value:','inity',
@@ -620,8 +659,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);
}
#
@@ -630,8 +669,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);
}
#
@@ -640,8 +679,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);
}
#
@@ -753,22 +792,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');
@@ -902,7 +926,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;
@@ -959,14 +983,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];
}
@@ -1000,6 +1047,7 @@ sub functionplotrulecheck {
return 0;
}
}
+ }
}
&addlog("Rule $label passed.");
return 1;
@@ -1035,14 +1083,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;
@@ -1122,20 +1163,7 @@ 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=&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);
foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
$result.=&generate_spline($internalid,$label,$xmin,$xmax,$ymin,$ymax);
}
@@ -1162,6 +1190,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='';
@@ -1181,20 +1228,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');
@@ -1208,7 +1242,7 @@ sub start_functionplotelements {
$result.=&start_init_script($internalid);
# put the axis commands inside
$result.=&axes_script($internalid,$xmin,$xmax,$ymin,$ymax,$xaxisvisible,$yaxisvisible,$gridvisible);
- $result.=&axes_label($internalid,$xlabel,$ylabel);
+ $result.=&axes_label($internalid,$xmin,$xmax,$ymin,$ymax,$xlabel,$ylabel);
# init script is left open
}
return $result;