--- loncom/homework/functionplotresponse.pm 2010/11/19 20:03:13 1.41 +++ 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.41 2010/11/19 20:03:13 www Exp $ +# $Id: functionplotresponse.pm,v 1.42 2010/11/20 00:14:44 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -61,7 +61,7 @@ sub geogebra_endcode { # sub geogebra_spline_program { return (< + ENDSPLINEPROGRAM } @@ -441,7 +441,7 @@ sub start_functionplotrule { $relationship='eq'; } my $derivative=&Apache::lonxml::get_param('derivativeorder',$parstack,$safeeval); - unless (($derivative==0) || ($derivative==1) || ($derivative==2)) { + unless (($derivative==-1) || ($derivative==0) || ($derivative==1) || ($derivative==2)) { &Apache::lonxml::warning(&mt('Rule derivative not defined.')); $derivative=0; } @@ -465,7 +465,8 @@ sub start_functionplotrule { &Apache::edit::select_arg(&mt('Function:'),'derivativeorder', [['0','Function itself'], ['1','First derivative'], - ['2','Second derivative']],$token).'
'. + ['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', @@ -628,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); } # @@ -638,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); } # @@ -648,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); } # @@ -895,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; @@ -952,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]; } @@ -993,6 +1017,7 @@ sub functionplotrulecheck { return 0; } } + } } &addlog("Rule $label passed."); return 1;