|
| 1 | +# WeBWorK problem written by Chris Hughes, 2013 |
| 2 | +# Portland Community College |
| 3 | +# |
| 4 | +# Template: |
| 5 | +# Solve the following quadratic equation |
| 6 | +# |
| 7 | +# ac x^2 = - (ad + bc) x - bd |
| 8 | +# |
| 9 | +# This equation factors nicely |
| 10 | +# |
| 11 | +# (ax + b)(cx + d) = 0 |
| 12 | +# |
| 13 | +# If you need to use the square root symbol, |
| 14 | +# as in x=sqrt{17}, type it like: *sqrt(17)* |
| 15 | +# |
| 16 | +# a is integer on [2,5], c = 1 |
| 17 | +# |
| 18 | +# b = d are both positive. |
| 19 | +# |
| 20 | +# Last updated: Hughes 8/20/13 |
| 21 | +# |
| 22 | +# ENDDESCRIPTION |
| 23 | + |
| 24 | +# Modified for Piedmont placement test |
| 25 | +# (smaller numbers, no sqrt note) |
| 26 | +# by Doug Torrance (2025-05-08) |
| 27 | + |
| 28 | +## DBCCSS('A-REI.4.b') |
| 29 | +## DBsubject('Algebra') |
| 30 | +## DBchapter('Basic Algebra') |
| 31 | +## DBsection('equations', 'Simplification') |
| 32 | +## KEYWORDS('solve','quadratic','equation','factor','zero product principle','fraction') |
| 33 | +## Author('Alex Jordan, Carl Yao, Chris Hughes') |
| 34 | +## Institution('PCC') |
| 35 | + |
| 36 | + |
| 37 | +############################################## |
| 38 | +DOCUMENT(); |
| 39 | +loadMacros( |
| 40 | + "PGstandard.pl", |
| 41 | + "MathObjects.pl", |
| 42 | + "parserAssignment.pl", |
| 43 | + "answerHints.pl", |
| 44 | + "PGML.pl", |
| 45 | + "contextFraction.pl", |
| 46 | + "PCCmacros.pl", |
| 47 | + "PGcourse.pl", |
| 48 | +); |
| 49 | + |
| 50 | +############################################## |
| 51 | + |
| 52 | +Context("Numeric"); |
| 53 | +# globally set the reductions one time |
| 54 | +# # so that we can then merely call ->reduce; |
| 55 | +Context()->noreduce('(-x)-y','(-x)+y'); |
| 56 | + |
| 57 | +$var = "x"; |
| 58 | +$a=10; |
| 59 | +$b=$a; |
| 60 | +$c=$a; |
| 61 | +$d=$a; |
| 62 | + |
| 63 | +while(gcd($a,$b)!=1 or gcd($c,$d)!=1 or (abs($a*$d+$b*$d)>20) or abs($b * $d) > 20) |
| 64 | +{ |
| 65 | + $a = random(2,5,1); |
| 66 | + $b = random(1,12,1); |
| 67 | + $c = 1; |
| 68 | + $d = random(2,10,1); |
| 69 | +} |
| 70 | + |
| 71 | +$lhs = Formula("$a*$c*$var^2")->reduce->reduce; |
| 72 | +$rhs = Formula("-($b*$c+$a*$d)*$var-($b*$d)")->reduce->reduce; |
| 73 | +$questionFormula = Formula("$a*$c*$var^2+($b*$c+$a*$d)*$var+($b*$d)")->reduce->reduce; |
| 74 | +$questionFormula1 = Formula("($a*$var+$b)($c*$var+$d)")->reduce; |
| 75 | + |
| 76 | +Context("LimitedFraction")->flags->set( |
| 77 | + reduceFractions => 0, |
| 78 | + showMixedNumbers=>0, |
| 79 | + showExtraParens=>0 ); |
| 80 | +parser::Assignment->Allow; |
| 81 | +Context()->operators->redefine(',',using=>',',from=>'Numeric'); |
| 82 | +Context()->operators->redefine('or',using=>',',from=>'Numeric'); |
| 83 | +Context()->operators->set( |
| 84 | + ','=>{string=>' or ',TeX=>'\hbox{ or }'}, |
| 85 | + 'or'=>{string=>' or ',TeX=>'\hbox{ or }'} |
| 86 | +); |
| 87 | +Context()->lists->set(List => {separator => " or "}); |
| 88 | +Context()->{error}{msg}{"Function 'sqrt' is not allowed in this context"} |
| 89 | + = "Please simplify your answer further"; |
| 90 | +Context()->{error}{msg}{"Can't use '*' in this context"} |
| 91 | + = "Please simplify your answer further"; |
| 92 | +Context()->{error}{msg}{"Can't use '+' in this context"} |
| 93 | + = "Please simplify your answer further"; |
| 94 | +Context()->{error}{msg}{"Can't use '-' in this context"} |
| 95 | + = "Please simplify your answer further"; |
| 96 | + |
| 97 | +# add solution strings to context- this means that if |
| 98 | +# students enter these (and they are not correct), then |
| 99 | +# WW will not give a Context warning |
| 100 | +Context()->strings->add("no real solutions"=>{}, |
| 101 | + "no real solution"=>{alias=>'no real solutions'}, |
| 102 | + "none"=>{alias=>'no real solutions'}, |
| 103 | + ); |
| 104 | + |
| 105 | +$soln1 = Fraction(-$b,$a); |
| 106 | +$soln2 = Fraction(-$d,$c); |
| 107 | +$ans = Compute("$var = $soln1, $var = $soln2"); |
| 108 | + |
| 109 | +############################################## |
| 110 | + |
| 111 | +TEXT(beginproblem()); |
| 112 | +BEGIN_PGML |
| 113 | +Solve the equation. |
| 114 | + |
| 115 | + [` [$lhs]=[$rhs]`] |
| 116 | + |
| 117 | + [__________________________] |
| 118 | + |
| 119 | +[@KeyboardInstructions("Enter multiple answers separated by commas, as in [|x=1, x=-1|]*.")@]** |
| 120 | +END_PGML |
| 121 | + |
| 122 | +############################################## |
| 123 | +$showPartialCorrectAnswers = 1; |
| 124 | +ANS($ans->cmp( |
| 125 | + entry_type => "a solution", |
| 126 | + checker => sub { |
| 127 | + my ($correct,$student,$ans,$nth,$value) = @_; |
| 128 | + if ($correct->type eq "Assignment") { |
| 129 | + my ($svar,$sfrac) = $student->value; # get the variable and fraction |
| 130 | + #return 0 unless Value::classMatch($sfrac,'Fraction') && $sfrac->isReduced; |
| 131 | + if(Value::classMatch($sfrac,'Fraction')) |
| 132 | + { |
| 133 | + return 0 unless $sfrac->isReduced; |
| 134 | + } |
| 135 | + } |
| 136 | + return $correct == $student; |
| 137 | + }, |
| 138 | + extra => sub { |
| 139 | + my ($student,$ansHash,$nth,$value) = @_; |
| 140 | + if($student eq "no real solutions") |
| 141 | + { |
| 142 | + $student->context->setError("This equation does have some solutions- try using the square root method","",undef,undef,$Value::CMP_WARNING) |
| 143 | + unless $ans->{isPreview}; |
| 144 | + return; |
| 145 | + } |
| 146 | + if ($student->type ne "Assignment" && $ansHash->{student_formula}->type ne "Assignment") { |
| 147 | + $student->context->setError("Your $nth solution should be written $var = $US$US$US","",undef,undef,$Value::CMP_WARNING) |
| 148 | + unless $ans->{isPreview}; |
| 149 | + return; |
| 150 | + } |
| 151 | + my ($svar,$sfrac) = $student->value; # get the variable and fraction |
| 152 | + if (Value::classMatch($sfrac,'Fraction') && !$sfrac->isReduced) { |
| 153 | + $student->context->setError("Your $nth $value is not reduced","",undef,undef,$Value::CMP_WARNING) |
| 154 | + unless $ans->{isPreview}; |
| 155 | + return; |
| 156 | + } |
| 157 | + return Value::Real->typeMatch($student); |
| 158 | + } |
| 159 | +)->withPostFilter(AnswerHints( |
| 160 | + ["$var=$soln1","$var=$soln2"] => "Are you sure you have all the solutions?", |
| 161 | + [$soln1,$soln2] => ["Your solution is a correct one, but you should write $var = $US$US$US<br>Are you sure you have all the solutions?",replaceMessage=>1], |
| 162 | + ["$soln1,$soln2","$soln2,$soln1"] => ["Your solutions are correct, but you should write $var = $US$US$US",replaceMessage=>1], |
| 163 | +))); |
| 164 | + |
| 165 | +############################################## |
| 166 | + |
| 167 | +$A = $a*$c; |
| 168 | +$B = $b*$c+$a*$d; |
| 169 | +$C = $b*$d; |
| 170 | + |
| 171 | +BEGIN_PGML_SOLUTION |
| 172 | +There are a few ways to solve quadratic equations- the easiest way to solve this particular |
| 173 | +type of problem is to use the _zero product principle_. |
| 174 | + |
| 175 | + [` |
| 176 | + \begin{aligned} |
| 177 | + [$lhs]=[$rhs] &\Rightarrow [$questionFormula]=0 \\ |
| 178 | + &\Rightarrow [$questionFormula1]=0 \\ |
| 179 | + & \Rightarrow [$ans] |
| 180 | + \end{aligned} |
| 181 | + `] |
| 182 | + |
| 183 | +This quadratic equation has two distinct, real solutions. |
| 184 | + |
| 185 | +The solutions can be checked by substituting them into the original equation- this is left as an exercise. |
| 186 | +END_PGML_SOLUTION |
| 187 | + |
| 188 | +############################################## |
| 189 | + |
| 190 | +ENDDOCUMENT(); |
| 191 | + |
0 commit comments