… | |
… | |
2 | |
2 | |
3 | sub _parserImplicitEquation_init {}; # don't reload this file |
3 | sub _parserImplicitEquation_init {}; # don't reload this file |
4 | |
4 | |
5 | =head1 DESCRIPTION |
5 | =head1 DESCRIPTION |
6 | |
6 | |
7 | ###################################################################### |
7 | ###################################################################### |
8 | # |
8 | # |
9 | # This is a MathObject class that implements an answer checker for |
9 | # This is a MathObject class that implements an answer checker for |
10 | # implicitly defined equations. The checker looks for the zeros of |
10 | # implicitly defined equations. The checker looks for the zeros of |
11 | # the equation and tests that the student and professor equations |
11 | # the equation and tests that the student and professor equations |
12 | # both have the same solutions. |
12 | # both have the same solutions. |
13 | # |
13 | # |
14 | # This type of check is very subtle, and there are important issues |
14 | # This type of check is very subtle, and there are important issues |
15 | # that you may have to take into account. The solutions to the |
15 | # that you may have to take into account. The solutions to the |
16 | # equations are found numerically, and so they will not be exact; |
16 | # equations are found numerically, and so they will not be exact; |
17 | # that means that there are tolerances that may need to be adjusted |
17 | # that means that there are tolerances that may need to be adjusted |
18 | # for your particular equation. Also, it is always possible for the |
18 | # for your particular equation. Also, it is always possible for the |
19 | # student to represent the function in a form that will exceed those |
19 | # student to represent the function in a form that will exceed those |
20 | # tolerances, and so be marked as incorrect. The answer checker |
20 | # tolerances, and so be marked as incorrect. The answer checker |
21 | # attempts to set the parameters based on the values of the functions |
21 | # attempts to set the parameters based on the values of the functions |
22 | # involved, but this may not work perfectly. |
22 | # involved, but this may not work perfectly. |
23 | # |
23 | # |
24 | # The method used to locate the solutions of A=B is by finding zeros |
24 | # The method used to locate the solutions of A=B is by finding zeros |
25 | # of A-B, and it requires this function to take on both positive and |
25 | # of A-B, and it requires this function to take on both positive and |
26 | # negative values (that is, it can only find transverse intersections |
26 | # negative values (that is, it can only find transverse intersections |
27 | # of the surface A-B=0 and the plane at height 0). For example, even |
27 | # of the surface A-B=0 and the plane at height 0). For example, even |
28 | # though the solutions of (x^2+y^1-1)^2=0 form a circle, the |
28 | # though the solutions of (x^2+y^1-1)^2=0 form a circle, the |
29 | # algorithm will fail to find any solutions for this equation. |
29 | # algorithm will fail to find any solutions for this equation. |
30 | # |
30 | # |
31 | # In order to locate the zeros, you may need to change the limits so |
31 | # In order to locate the zeros, you may need to change the limits so |
32 | # that they include regions where the function is both positive and |
32 | # that they include regions where the function is both positive and |
33 | # negative (see below). The algorithm will avoid discontinuities, so |
33 | # negative (see below). The algorithm will avoid discontinuities, so |
34 | # you can specify things like x-y=1/(x+y) rather than x^2-y^2=1. |
34 | # you can specify things like x-y=1/(x+y) rather than x^2-y^2=1. |
35 | # |
35 | # |
36 | # Because the solutions are found using a random search, it is |
36 | # Because the solutions are found using a random search, it is |
37 | # possible the randomly chosen starting points don't locate enough |
37 | # possible the randomly chosen starting points don't locate enough |
38 | # zeros for the function, in which case the check will fail. This |
38 | # zeros for the function, in which case the check will fail. This |
39 | # can happen for both the professor's function and the student's, |
39 | # can happen for both the professor's function and the student's, |
40 | # since zeros are found for both. This means that a correct answer |
40 | # since zeros are found for both. This means that a correct answer |
41 | # can sometimes be marked incorrect depending on the random points |
41 | # can sometimes be marked incorrect depending on the random points |
42 | # chosen initially. These points also affect the values selected for |
42 | # chosen initially. These points also affect the values selected for |
43 | # the tolerances used to determine when a function's value is zero, |
43 | # the tolerances used to determine when a function's value is zero, |
44 | # and so can affect whether the student's function is marked as |
44 | # and so can affect whether the student's function is marked as |
45 | # correct or not. |
45 | # correct or not. |
46 | # |
46 | # |
47 | # If an equation has several components or branches, it is possible |
47 | # If an equation has several components or branches, it is possible |
48 | # that the random location of solutions will not find zeros on some |
48 | # that the random location of solutions will not find zeros on some |
49 | # of the branches, and so might incorrectly mark as correct an |
49 | # of the branches, and so might incorrectly mark as correct an |
50 | # equation that only is zero on one of the components. For example, |
50 | # equation that only is zero on one of the components. For example, |
51 | # x^2-y^2=0 has solutions along the lines y=x and y=-x, so it is |
51 | # x^2-y^2=0 has solutions along the lines y=x and y=-x, so it is |
52 | # possible that x-y=0 or x+y=0 will be marked as correct if the |
52 | # possible that x-y=0 or x+y=0 will be marked as correct if the |
53 | # random points are unluckily chosen. One way to reduce this problem |
53 | # random points are unluckily chosen. One way to reduce this problem |
54 | # is to increase the number of solutions that are required (by |
54 | # is to increase the number of solutions that are required (by |
55 | # setting the ImplicitPoints flag in the Context). Another is to |
55 | # setting the ImplicitPoints flag in the Context). Another is to |
56 | # specify the solutions yourself, so that you are sure there are |
56 | # specify the solutions yourself, so that you are sure there are |
57 | # points on each component. |
57 | # points on each component. |
58 | # |
58 | # |
59 | # These problems should be rare, and the values for the various |
59 | # These problems should be rare, and the values for the various |
60 | # parameters have been set in an attempt to minimize the possibility |
60 | # parameters have been set in an attempt to minimize the possibility |
61 | # of these errors, but they can occur, and you should be aware of |
61 | # of these errors, but they can occur, and you should be aware of |
62 | # them, and their possible solutions. |
62 | # them, and their possible solutions. |
63 | # |
63 | # |
64 | # |
64 | # |
65 | # Usage examples: |
65 | # Usage examples: |
66 | # |
66 | # |
67 | # Context("ImplicitEquation"); |
67 | # Context("ImplicitEquation"); |
68 | # $f = ImplicitEquation("x^2 = cos(y)"); |
68 | # $f = ImplicitEquation("x^2 = cos(y)"); |
69 | # $f = ImplicitEquation("x^2 - 2y^2 = 5",limits=>[[-3,3],[-2,2]]); |
69 | # $f = ImplicitEquation("x^2 - 2y^2 = 5",limits=>[[-3,3],[-2,2]]); |
70 | # $f = ImplicitEquation("x=1/y",tolerance=>.0001); |
70 | # $f = ImplicitEquation("x=1/y",tolerance=>.0001); |
71 | # |
71 | # |
72 | # Then use |
72 | # Then use |
73 | # |
73 | # |
74 | # ANS($f->cmp); |
74 | # ANS($f->cmp); |
75 | # |
75 | # |
76 | # to get the answer checker for $f. |
76 | # to get the answer checker for $f. |
77 | # |
77 | # |
78 | # There are a number of Context flags that control the answer checker. |
78 | # There are a number of Context flags that control the answer checker. |
79 | # These include: |
79 | # These include: |
80 | # |
80 | # |
81 | # ImplicitPoints => 7 (the number of solutions to test) |
81 | # ImplicitPoints => 7 (the number of solutions to test) |
82 | # ImplicitTolerance => 1E-6 (relative tolerance value for when |
82 | # ImplicitTolerance => 1E-6 (relative tolerance value for when |
83 | # the tested function is zero) |
83 | # the tested function is zero) |
84 | # ImplicitAbsoluteMinTolerance => 1E-3 (the minimum tolerance allowed) |
84 | # ImplicitAbsoluteMinTolerance => 1E-3 (the minimum tolerance allowed) |
85 | # ImplicitAbsoluteMaxTolerance => 1E-3 (the maximum tolerance allowed) |
85 | # ImplicitAbsoluteMaxTolerance => 1E-3 (the maximum tolerance allowed) |
86 | # ImplicitPointTolerance => 1E-9 (relative tolerance for how close |
86 | # ImplicitPointTolerance => 1E-9 (relative tolerance for how close |
87 | # the solution point must be to an |
87 | # the solution point must be to an |
88 | # actual solution) |
88 | # actual solution) |
89 | # BisectionTolerance => .01 (extra factor used for the tolerance |
89 | # BisectionTolerance => .01 (extra factor used for the tolerance |
90 | # when finding the solutions) |
90 | # when finding the solutions) |
91 | # BisectionCutoff => 40 (maximum number of bisections to |
91 | # BisectionCutoff => 40 (maximum number of bisections to |
92 | # perform when looking for a solution) |
92 | # perform when looking for a solution) |
93 | # |
93 | # |
94 | # You may set any of these using Context()->flags->set(...). |
94 | # You may set any of these using Context()->flags->set(...). |
95 | # |
95 | # |
96 | # In addition to the Context flags, you can set some values within |
96 | # In addition to the Context flags, you can set some values within |
97 | # the ImplicitEquation itself: |
97 | # the ImplicitEquation itself: |
98 | # |
98 | # |
99 | # tolerance (the absolute tolerance for zeros of the function) |
99 | # tolerance (the absolute tolerance for zeros of the function) |
100 | # bisect_tolerance (the tolerance used when searching for zeros) |
100 | # bisect_tolerance (the tolerance used when searching for zeros) |
101 | # point_tolerance (the absolute tolerance for how close to an |
101 | # point_tolerance (the absolute tolerance for how close to an |
102 | # actual solution the located solution must be) |
102 | # actual solution the located solution must be) |
103 | # limits (the domain to use for the function; see the |
103 | # limits (the domain to use for the function; see the |
104 | # documentation for the Formula object) |
104 | # documentation for the Formula object) |
105 | # solutions (a reference to an array of references to arrays |
105 | # solutions (a reference to an array of references to arrays |
106 | # that contain the coordinates of the points |
106 | # that contain the coordinates of the points |
107 | # that are the solutions of the equation) |
107 | # that are the solutions of the equation) |
108 | # |
108 | # |
109 | # These can be set in the in the ImplicitEquation() call that creates |
109 | # These can be set in the in the ImplicitEquation() call that creates |
110 | # the object, as in the examples below: |
110 | # the object, as in the examples below: |
111 | # |
111 | # |
112 | # For example: |
112 | # For example: |
113 | # |
113 | # |
114 | # $f = ImplicitEquation("x^2-y^2=0", |
114 | # $f = ImplicitEquation("x^2-y^2=0", |
115 | # solutions => [[0,0],[1,1],[-1,1],[-1,-1],[1,-1]], |
115 | # solutions => [[0,0],[1,1],[-1,1],[-1,-1],[1,-1]], |
116 | # tolerance => .001 |
116 | # tolerance => .001 |
117 | # ); |
117 | # ); |
118 | # |
118 | # |
119 | # |
119 | # |
120 | # $f = ImplicitEquation("xy=5",limits=>[-3,3]); |
120 | # $f = ImplicitEquation("xy=5",limits=>[-3,3]); |
121 | # |
121 | # |
122 | # The limits value can be set globally within the Context, if you wish, |
122 | # The limits value can be set globally within the Context, if you wish, |
123 | # and the others can be controlled by the Context flags discussed |
123 | # and the others can be controlled by the Context flags discussed |
124 | # above. |
124 | # above. |
125 | # |
125 | # |
126 | ###################################################################### |
126 | ###################################################################### |
127 | |
127 | |
128 | =cut |
128 | =cut |
129 | |
129 | |
130 | # |
130 | # |
131 | # Set up the context for ImplicitEquations and activate it |
131 | # Set up the context for ImplicitEquations and activate it |