--- loncom/homework/functionplotresponse.pm 2010/11/19 20:03:13 1.41
+++ loncom/homework/functionplotresponse.pm 2010/11/23 23:14:20 1.45
@@ -1,7 +1,7 @@
# LearningOnline Network with CAPA
# option list style responses
#
-# $Id: functionplotresponse.pm,v 1.41 2010/11/19 20:03:13 www Exp $
+# $Id: functionplotresponse.pm,v 1.45 2010/11/23 23:14:20 www Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -61,7 +61,7 @@ sub geogebra_endcode {
#
sub geogebra_spline_program {
return (<
+
ENDSPLINEPROGRAM
}
@@ -295,22 +295,36 @@ 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'],
@@ -561,7 +577,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',
@@ -628,8 +644,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);
}
#
@@ -638,8 +654,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);
}
#
@@ -648,8 +664,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);
}
#
@@ -895,7 +911,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;
@@ -952,14 +968,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];
}
@@ -993,6 +1032,7 @@ sub functionplotrulecheck {
return 0;
}
}
+ }
}
&addlog("Rule $label passed.");
return 1;
@@ -1187,7 +1227,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;