--- loncom/homework/functionplotresponse.pm 2010/11/19 20:03:13 1.41 +++ loncom/homework/functionplotresponse.pm 2010/11/24 02:53:13 1.47 @@ -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.47 2010/11/24 02:53:13 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -61,7 +61,7 @@ sub geogebra_endcode { # sub geogebra_spline_program { return (< + ENDSPLINEPROGRAM } @@ -295,28 +295,42 @@ 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 +601,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 +668,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 +678,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 +688,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); } # @@ -795,12 +835,15 @@ sub start_functionplotresponse { &Apache::edit::select_arg('y-axis visible:','yaxisvisible', ['yes','no'],$token).'
'. &Apache::edit::select_arg('Grid visible:','gridvisible', - ['yes','no'],$token). + ['yes','no'],$token).'
'. + &Apache::edit::text_arg('Background plot(s) for answer (function:xmin:xmax,function:xmin:xmax,...):', + 'answerdisplay',$token,'50'); + &Apache::edit::end_row().&Apache::edit::start_spanning_row(); } elsif ($target eq 'modified') { my $constructtag=&Apache::edit::get_new_args($token,$parstack, $safeeval,'xlabel','xmin','xmax','ylabel','ymin','ymax', - 'xaxisvisible','yaxisvisible','gridvisible'); + 'xaxisvisible','yaxisvisible','gridvisible','answerdisplay'); if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); } } elsif ($target eq 'meta') { @@ -895,7 +938,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 +995,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 +1059,7 @@ sub functionplotrulecheck { return 0; } } + } } &addlog("Rule $label passed."); return 1; @@ -1107,8 +1174,24 @@ sub end_functionplotelements { if ($target eq 'edit' ) { $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); + +# Are we in show answer mode? + my $showanswer=&Apache::response::show_answer(); + if ($showanswer) { +# Render answerdisplay + my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval,-2); + if ($answerdisplay=~/\S/s) { + foreach my $plot (split(/\s*\,\s*/,$answerdisplay)) { + my ($func,$xl,$xh)=split(/\s*\:\s*/,$plot); + if ((!defined($xl)) || ($xl eq '')) { $xl=$xmin; } + if ((!defined($xh)) || ($xh eq '')) { $xh=$xmax; } + $result.=&plot_script($internalid,$func,1,'','00aa00',$xl,$xh,6); + } + } + } + +# Now is the time to render all of the stored splines foreach my $label (keys(%Apache::functionplotresponse::splineorder)) { $result.=&generate_spline($internalid,$label,$xmin,$xmax,$ymin,$ymax); } @@ -1187,7 +1270,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;