Even and odd functions: custom answer checkers, formulas, the substitute method and messages.

From WeBWorK_wiki
Revision as of 18:44, 7 May 2022 by Rhcpkole (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This problem will teach you how to check for basic partial credit. It will introduce the Formula object and its substitute method. You will also learn how to give feedback to the student based on their response. The code below contains many comments to help you understand what is going on. You should run it and try to see how it works. There are exercises at the end to help you expand your knowledge.

If you get stuck in the code below it might be useful to read the following pages:

  1. Context
  2. Math Objects
  3. Formula
  4. Custom Answer Checkers
  5. Answer Hash
# In this problem the student is asked to provide an even and odd function of their choice.
# If they provide the correct parity they get 100% per answer.
# If they provide the opposite parity they get 75% per answer.
# If they provide a function that is neither they get 0% per answer blank.
# Helpful messages are provided to guide the student in case they make mistakes.

########################################################################

DOCUMENT();      

loadMacros(
   "PGstandard.pl",     # Standard macros for PG language
   "MathObjects.pl",	# Standard objects like real numbers, functions etc.
);

# Print problem number and point value (weight) for the problem
TEXT(beginproblem());

# Show which answers are correct and which ones are incorrect
$showPartialCorrectAnswers = 1;

##############################################################
#
#  Setup
#
#


Context("Numeric");	# Contains almost everything you need for real variable calculus

# Possible correct answers.
$evenFunc = Formula("x^2");
$oddFunc = Formula("x");

##############################################################
#
#  Text
#
#

Context()->texStrings;
BEGIN_TEXT
This is a problem on even and odd functions.
You should provide one even function and one odd function below.
We use the variable \(x\) in this problem.
$BR

$BCENTER
Enter an $BBOLD even $EBOLD function here: \( f(x) = \) \{$evenFunc->ans_rule\}
$ECENTER

$BCENTER
Enter an $BBOLD odd $EBOLD function here: \( g(x) = \) \{$oddFunc->ans_rule\}
$ECENTER


END_TEXT
Context()->normalStrings;

##############################################################
#
#  Answers
#
#

# Subroutine that is passed to the answer checker.
# It gives points and feedback according to the problem description at the top of this file.
$checkEven = sub {
	my ($correctFunc,$studentFunc,$ansHash) = @_;	# Get correct answer, student answer and the answer hash.
	
	# Check if the student entered an even function and respond accordingly.
	# If the function is even it must be equal to $possibleEvenFunc.
	$possibleEvenFunc = $studentFunc->substitute(x => "-x");
	
	# Check if the function is even.
	if ($studentFunc == $possibleEvenFunc){
		# The function is even. 
		# Congratulate the student.
		$ansHash->{ans_message} = "Great job!";
		
		#Give the student full points and exit.
		return 1.0;								
	} else {
		# The function is not even.
		# Give feedback to the student.
		$ansHash->{ans_message} = "It seems your function is not even.";
	}
	
	
	# Check if the student entered an odd function and respond accordingly.
	# First replace x with -x.
	my $possibleOddFunc = $studentFunc->substitute(x => "-x");			
	
	# Then negate. If the student function is odd it will equal $possibleOddFunc.
	$possibleOddFunc = -$possibleOddFunc;	
	
	# Check if the function is odd.
	if ($studentFunc == $possibleOddFunc){
		# The function is odd.
		# Tell the student that it is odd, but also keep the fact that the function is not even as well.
		$ansHash->{ans_message} = $ansHash->{ans_message} . " Your function is odd though and you get 75% for this work.";
		
		# Give student 75% and exit.
		return 0.75;
	} else {
		# The function is not odd and also not even at this point. 
		# Give feedback to the student about this.
		$ansHash->{ans_message} = $ansHash->{ans_message} . " It seems your function is not odd as well.";
		$ansHash->{ans_message} = $ansHash->{ans_message} . " Recall that a function is even if it is the same when you reflect it about the y-axis.";
	}
	
	# The student didn't earn any credit.
	return 0.0;
};

# Do a similar thing for the odd function case.
$checkOdd = sub {
	my ($correctFunc,$studentFunc,$ansHash) = @_;	# Get correct answer, student answer and the answer hash.
	
	my $possibleOddFunc = $studentFunc->substitute(x => "-x");			
	$possibleOddFunc = -$possibleOddFunc;
	if ($studentFunc == $possibleOddFunc){
		$ansHash->{ans_message} = "Great job!";
		return 1.0;								
	} else {
		$ansHash->{ans_message} = "It seems your function is not odd.";
	}
	
	
	$possibleEvenFunc = $studentFunc->substitute(x => "-x");	
	if ($studentFunc == $possibleEvenFunc){
		$ansHash->{ans_message} = $ansHash->{ans_message} . " Your function is even though and you get 75% for this work.";
		return 0.75;
	} else {
		$ansHash->{ans_message} = $ansHash->{ans_message} . " It seems your function is not even as well.";
		$ansHash->{ans_message} = $ansHash->{ans_message} . " Recall that a function is odd if it is the same when you reflect it about the y-axis and then about the x-axis.";
	}
	
	return 0.0;
};

ANS($evenFunc->cmp(checker => $checkEven));
ANS($oddFunc->cmp(checker => $checkOdd));


ENDDOCUMENT();        

Exercises:

  1. Write an instructor solution to the problem above.
  2. Modify the problem above to check for partial credit if the student uses the variable X instead of x.
  3. Modify the problem above such that you combine $checkEven and $checkOdd into one general function instead of repeating some code.
  4. Modify the problem you wrote for the second exercise to minimize code repetition.