Difference between revisions of "TrigFunctionsInDegrees"

From WeBWorK_wiki
Jump to navigation Jump to search
(New page: <h2>Trigonometric Functions in Degrees in Student Answers</h2> <!-- Header for these sections -- no modification needed --> <p style="background-color:#eeeeee;border:black solid 1px;p...)
 
Line 54: Line 54:
 
sub cos {
 
sub cos {
 
shift; my $x = shift;
 
shift; my $x = shift;
return CORE::cos($x*3.14159265358979323846264338327950288/180);
+
return CORE::cos($x*3.14159265358979/180);
 
}
 
}
 
package main;
 
package main;
 
# Make it work on formulas as well as numbers
 
# Make it work on formulas as well as numbers
#sub cos {Parser::Function->call('cos',@_)} # if uncommented, this line will generate error messages
+
# if uncommented, this next line will generate error messages
  +
#sub cos {Parser::Function->call('cos',@_)}
 
# Add the new functions to the Context
 
# Add the new functions to the Context
 
Context()->functions->add( cos => {class => 'NewFunc', TeX => '\cos'}, );
 
Context()->functions->add( cos => {class => 'NewFunc', TeX => '\cos'}, );
Line 70: Line 70:
 
sub sin {
 
sub sin {
 
shift; my $x = shift;
 
shift; my $x = shift;
return CORE::sin($x*3.14159265358979323846264338327950288/180);
+
return CORE::sin($x*3.14159265358979/180);
 
}
 
}
 
package main;
 
package main;
 
# Make it work on formulas as well as numbers
 
# Make it work on formulas as well as numbers
#sub sin {Parser::Function->call('sin',@_)} # if uncommented, this line will generate error messages
+
# if uncommented, this next line will generate error messages
  +
#sub sin {Parser::Function->call('sin',@_)}
 
# Add the new functions to the Context
 
# Add the new functions to the Context
 
Context()->functions->add( sin => {class => 'NewFunc', TeX => '\sin'}, );
 
Context()->functions->add( sin => {class => 'NewFunc', TeX => '\sin'}, );
Line 86: Line 86:
 
sub tan {
 
sub tan {
 
shift; my $x = shift;
 
shift; my $x = shift;
return CORE::sin($x*3.14159265358979323846264338327950288/180)/CORE::cos($x*3.14159265358979323846264338327950288/180);
+
return CORE::sin($x*3.14159265358979/180)/CORE::cos($x*3.14159265358979/180);
 
}
 
}
 
package main;
 
package main;
Line 102: Line 102:
 
sub sec {
 
sub sec {
 
shift; my $x = shift;
 
shift; my $x = shift;
return 1.0/CORE::cos($x*3.14159265358979323846264338327950288/180);
+
return 1.0/CORE::cos($x*3.14159265358979/180);
 
}
 
}
 
package main;
 
package main;
Line 118: Line 118:
 
sub csc {
 
sub csc {
 
shift; my $x = shift;
 
shift; my $x = shift;
return 1.0/CORE::sin($x*3.14159265358979323846264338327950288/180);
+
return 1.0/CORE::sin($x*3.14159265358979/180);
 
}
 
}
 
package main;
 
package main;
Line 134: Line 134:
 
sub cot {
 
sub cot {
 
shift; my $x = shift;
 
shift; my $x = shift;
return CORE::cos($x*3.14159265358979323846264338327950288/180)/CORE::sin($x*3.14159265358979323846264338327950288/180);
+
return CORE::cos($x*3.14159265358979/180)/CORE::sin($x*3.14159265358979/180);
 
}
 
}
 
package main;
 
package main;
Line 157: Line 157:
 
# function from reals to reals
 
# function from reals to reals
 
our @ISA = qw(Parser::Function::numeric);
 
our @ISA = qw(Parser::Function::numeric);
sub acos {CORE::atan2(CORE::sqrt(1-$_[1]*$_[1]),$_[1])*180/3.14159265358979323846264338327950288}
+
sub acos {CORE::atan2(CORE::sqrt(1-$_[1]*$_[1]),$_[1])*180/3.14159265358979}
 
package main;
 
package main;
 
# Make it work on formulas as well as numbers
 
# Make it work on formulas as well as numbers
Line 170: Line 170:
 
# function from reals to reals
 
# function from reals to reals
 
our @ISA = qw(Parser::Function::numeric);
 
our @ISA = qw(Parser::Function::numeric);
sub asin {CORE::atan2($_[1],CORE::sqrt(1-$_[1]*$_[1]))*180/3.14159265358979323846264338327950288}
+
sub asin {CORE::atan2($_[1],CORE::sqrt(1-$_[1]*$_[1]))*180/3.14159265358979}
 
package main;
 
package main;
 
# Make it work on formulas as well as numbers
 
# Make it work on formulas as well as numbers
Line 183: Line 183:
 
# function from reals to reals
 
# function from reals to reals
 
our @ISA = qw(Parser::Function::numeric);
 
our @ISA = qw(Parser::Function::numeric);
sub atan {CORE::atan2($_[1],1)*180/3.14159265358979323846264338327950288}
+
sub atan {CORE::atan2($_[1],1)*180/3.14159265358979}
 
package main;
 
package main;
 
# Make it work on formulas as well as numbers
 
# Make it work on formulas as well as numbers
Line 196: Line 196:
 
# function from reals to reals
 
# function from reals to reals
 
our @ISA = qw(Parser::Function::numeric);
 
our @ISA = qw(Parser::Function::numeric);
sub asec {CORE::atan2($_[1],CORE::sqrt(1-$_[1]*$_[1]))*180/3.14159265358979323846264338327950288}
+
sub asec {CORE::atan2($_[1],CORE::sqrt(1-$_[1]*$_[1]))*180/3.14159265358979}
 
#sub asec {acos($_[0],1.0/$_[1])}
 
#sub asec {acos($_[0],1.0/$_[1])}
 
package main;
 
package main;
Line 210: Line 210:
 
# function from reals to reals
 
# function from reals to reals
 
our @ISA = qw(Parser::Function::numeric);
 
our @ISA = qw(Parser::Function::numeric);
sub acsc {CORE::atan2(CORE::sqrt(1-$_[1]*$_[1]),$_[1])*180/3.14159265358979323846264338327950288}
+
sub acsc {CORE::atan2(CORE::sqrt(1-$_[1]*$_[1]),$_[1])*180/3.14159265358979}
 
#sub acsc {asin($_[0],1.0/$_[1])}
 
#sub acsc {asin($_[0],1.0/$_[1])}
 
package main;
 
package main;
Line 224: Line 224:
 
# function from reals to reals
 
# function from reals to reals
 
our @ISA = qw(Parser::Function::numeric);
 
our @ISA = qw(Parser::Function::numeric);
sub acot {CORE::atan2(1,$_[1])*180/3.14159265358979323846264338327950288}
+
sub acot {CORE::atan2(1,$_[1])*180/3.14159265358979}
 
package main;
 
package main;
 
# Make it work on formulas as well as numbers
 
# Make it work on formulas as well as numbers

Revision as of 02:38, 21 March 2010

Trigonometric Functions in Degrees in Student Answers


This PG code shows how to redefine trigonometric functions to evaluate in degrees rather than radians. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Problem Techniques Index

PG problem file Explanation
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
);

Initialization: We need to use MathObjects answer evaluators.

Context("Numeric");

##############################################
#  Begin trig functions in degrees

Context()->functions->remove("cos");
package NewFunc;
# this next line makes the function a 
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
sub cos {
  shift; my $x = shift;
  return CORE::cos($x*3.14159265358979/180);
}
package main;
# Make it work on formulas as well as numbers
# if uncommented, this next line will generate error messages
#sub cos {Parser::Function->call('cos',@_)} 
#  Add the new functions to the Context
Context()->functions->add( cos => {class => 'NewFunc', TeX => '\cos'}, );


Context()->functions->remove("sin");
package NewFunc;
# this next line makes the function a 
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
sub sin {
  shift; my $x = shift;
  return CORE::sin($x*3.14159265358979/180);
}
package main;
# Make it work on formulas as well as numbers
# if uncommented, this next line will generate error messages
#sub sin {Parser::Function->call('sin',@_)} 
#  Add the new functions to the Context
Context()->functions->add( sin => {class => 'NewFunc', TeX => '\sin'}, );


Context()->functions->remove("tan");
package NewFunc;
# this next line makes the function a 
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
sub tan {
  shift; my $x = shift;
  return CORE::sin($x*3.14159265358979/180)/CORE::cos($x*3.14159265358979/180);
}
package main;
# Make it work on formulas as well as numbers
sub tan {Parser::Function->call('tan',@_)}
#  Add the new functions to the Context
Context()->functions->add( tan => {class => 'NewFunc', TeX => '\tan'}, );


Context()->functions->remove("sec");
package NewFunc;
# this next line makes the function a 
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
sub sec {
  shift; my $x = shift;
  return 1.0/CORE::cos($x*3.14159265358979/180);
}
package main;
# Make it work on formulas as well as numbers
sub sec {Parser::Function->call('sec',@_)} 
#  Add the new functions to the Context
Context()->functions->add( sec => {class => 'NewFunc', TeX => '\sec'}, );


Context()->functions->remove("csc");
package NewFunc;
# this next line makes the function a 
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
sub csc {
  shift; my $x = shift;
  return 1.0/CORE::sin($x*3.14159265358979/180);
}
package main;
# Make it work on formulas as well as numbers
sub csc {Parser::Function->call('csc',@_)} 
#  Add the new functions to the Context
Context()->functions->add( csc => {class => 'NewFunc', TeX => '\csc'}, );


Context()->functions->remove("cot");
package NewFunc;
# this next line makes the function a 
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
sub cot {
  shift; my $x = shift;
  return CORE::cos($x*3.14159265358979/180)/CORE::sin($x*3.14159265358979/180);
}
package main;
# Make it work on formulas as well as numbers
sub cot {Parser::Function->call('cot',@_)}
#  Add the new functions to the Context
Context()->functions->add( cot => {class => 'NewFunc', TeX => '\cot'}, );



#sub asin {CORE::atan2($_[1],CORE::sqrt(1-$_[1]*$_[1]))}
#sub acos {CORE::atan2(CORE::sqrt(1-$_[1]*$_[1]),$_[1])}
#sub atan {CORE::atan2($_[1],1)}
#sub acot {CORE::atan2(1,$_[1])}
#sub asec {acos($_[0],1.0/$_[1])}
#sub acsc {asin($_[0],1.0/$_[1])}


Context()->functions->remove("acos");
package NewFunc;
# this next line makes the function a 
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
sub acos {CORE::atan2(CORE::sqrt(1-$_[1]*$_[1]),$_[1])*180/3.14159265358979}
package main;
# Make it work on formulas as well as numbers
sub acos {Parser::Function->call('acos',@_)}
#  Add the new functions to the Context
Context()->functions->add( acos => {class => 'NewFunc', TeX => '\arccos'}, );


Context()->functions->remove("asin");
package NewFunc;
# this next line makes the function a 
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
sub asin {CORE::atan2($_[1],CORE::sqrt(1-$_[1]*$_[1]))*180/3.14159265358979}
package main;
# Make it work on formulas as well as numbers
sub asin {Parser::Function->call('asin',@_)}
#  Add the new functions to the Context
Context()->functions->add( asin => {class => 'NewFunc', TeX => '\arcsin'}, );


Context()->functions->remove("atan");
package NewFunc;
# this next line makes the function a 
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
sub atan {CORE::atan2($_[1],1)*180/3.14159265358979}
package main;
# Make it work on formulas as well as numbers
sub atan {Parser::Function->call('atan',@_)}
#  Add the new functions to the Context
Context()->functions->add( atan => {class => 'NewFunc', TeX => '\arctan'}, );


Context()->functions->remove("asec");
package NewFunc;
# this next line makes the function a 
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
sub asec {CORE::atan2($_[1],CORE::sqrt(1-$_[1]*$_[1]))*180/3.14159265358979}
#sub asec {acos($_[0],1.0/$_[1])}
package main;
# Make it work on formulas as well as numbers
sub asec {Parser::Function->call('asec',@_)}
#  Add the new functions to the Context
Context()->functions->add( asec => {class => 'NewFunc', TeX => '\arcsec'}, );


Context()->functions->remove("acsc");
package NewFunc;
# this next line makes the function a 
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
sub acsc {CORE::atan2(CORE::sqrt(1-$_[1]*$_[1]),$_[1])*180/3.14159265358979}
#sub acsc {asin($_[0],1.0/$_[1])}
package main;
# Make it work on formulas as well as numbers
sub acsc {Parser::Function->call('acsc',@_)}
#  Add the new functions to the Context
Context()->functions->add( acsc => {class => 'NewFunc', TeX => '\arccsc'}, );


Context()->functions->remove("acot");
package NewFunc;
# this next line makes the function a 
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
sub acot {CORE::atan2(1,$_[1])*180/3.14159265358979}
package main;
# Make it work on formulas as well as numbers
sub acot {Parser::Function->call('acot',@_)}
#  Add the new functions to the Context
Context()->functions->add( acot => {class => 'NewFunc', TeX => '\arccot'}, );


#  End trig functions in degrees
###################################################


Setup: WeBWorK's default is to evaluate trig functions in radians. Radians ought to be used whenever possible. Sometimes, degrees are unavoidable (e.g., when your students are first learning trig and haven't yet been taught radians). The code in this section redefines the standard trig functions to be in degrees, both in any formulas that appear later in the PG code and in any formulas that students enter in answer blanks.

These redefined functions allow students to enter inverse functions using syntax such as atan(x) and arctan(x), but not tan^(-1)(x). You may want to warn your students about this caveat in the main text section of the problem.

BEGIN_TEXT
Enter \( \sin(30) \): \{ ans_rule(20) \}
$BR
Enter \( \arcsin(1/2) \): \{ ans_rule(20) \}
$BR
Caution: Enter \( \arcsin(x) \) as \( \arcsin(x) \) or
\( \mathrm{asin}(x) \), but not \( \sin^{-1}(x) \).
END_TEXT

Main Text: The problem text section of the file is as we'd expect.

ANS( Real(0.5)->cmp() );
ANS( Compute("pi/6")->cmp() );

COMMENT("Redefines trig functions to be in degrees (not radians)");
ENDDOCUMENT();

Answer Evaluation: As is the answer. We should include a comment that warns other instructors that trig functions are in degrees instead of radians.

Problem Techniques Index