Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 324 lines (269 sloc) 13.72 kB
fea4d79 @jruberg moved todo in even_and_odd_functions so doctype is at the beginning o…
jruberg authored
1 <!DOCTYPE html>
cde9e9f @jeresig Re-run the exercises through the updated cleaner.
jeresig authored
2 <!-- TDOO: remove <var>s in graphie data-eachs -->
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
3 <html data-require="math rational-expressions graphie">
cde9e9f @jeresig Re-run the exercises through the updated cleaner.
jeresig authored
4 <head>
0f0c630 @cbhl Clean up the exercise files using the new cleaning utility. (II)
cbhl authored
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
6 <title>Even and odd functions</title>
ac1415e @spicyj Use requirejs for module loading
spicyj authored
7 <script data-main="../local-only/main.js" src="../local-only/require.js"></script>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
8 <script type="text/javascript">
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
9
10 function makeEquation(type) {
11 var funcs = {
12 "Even": [
13 new KhanUtil.Term(1),
14 new KhanUtil.Term(1, {'x': 2}),
15 new KhanUtil.Term(1, {'x': 4})
16 ],
17 "Odd": [
18 new KhanUtil.Term(1, 'x'),
19 new KhanUtil.Term(1, {'x': 3}),
20 new KhanUtil.Term(1, {'x': 5})
21 ]
22 };
23
24 var equation = new KhanUtil.RationalExpression([]);
25
26 if (type === "Even" || type === "Odd") {
27 var f = KhanUtil.shuffle(funcs[type]);
28
29 for (var i=0; i<KhanUtil.randRange(1, 3); i++) {
30 var term = f[i].multiply(KhanUtil.randRangeNonZero(-5, 5));
31 equation.terms.push(term);
32 }
33 } else {
34 var f1 = KhanUtil.shuffle(funcs["Even"]);
35 var f2 = KhanUtil.shuffle(funcs["Odd"]);
36 var term1 = f1[0].multiply(KhanUtil.randRangeNonZero(-5, 5));
37 var term2 = f2[0].multiply(KhanUtil.randRangeNonZero(-5, 5));
38 equation.terms.push(term1);
39 equation.terms.push(term2);
40 }
41
42 return equation;
43 };
44
166863f @petercollingridge Make question more specific
petercollingridge authored
45 function getNegativeFunction(f) {
46 var terms1 = [];
47 var terms2 = [];
48
49 // Copy own terms
50 for (var i = 0; i < f.terms.length; i++) {
51 var term = f.terms[i];
52 var variables2 = { "(\\blue{x})": term.variables.x };
53 var coefficient = (term.variables.x % 2 === 1) ? -term.coefficient : term.coefficient;
54 var variables1 = {};
55 var variables2 = {};
56
57 if (term.variables.x) {
58 variables1["(\\blue{-x})"] = term.variables.x;
59 variables2["\\blue{x}"] = term.variables.x;
60 }
61
62 terms1.push([term.coefficient, variables1]);
63 terms2.push([coefficient, variables2]);
64 }
65
66 var negativeF1 = new KhanUtil.RationalExpression(terms1);
67 var negativeF2 = new KhanUtil.RationalExpression(terms2);
68
69 return [negativeF1, negativeF2];
70 };
71
c0c0f25 @petercollingridge Simplify code
petercollingridge authored
72 function makeFunc(type) {
73 var slice;
74
75 var funcs = {
76 "Even": [
77 function(x) { return 1; },
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
78 function(x) { return 3 * Math.cos(x); },
c0c0f25 @petercollingridge Simplify code
petercollingridge authored
79 Math.abs,
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
80 function(x) { return 0.25 * x * x; },
81 function(x) { return 0.05 * x * x * x * x; }
82 ],
c0c0f25 @petercollingridge Simplify code
petercollingridge authored
83 "Odd": [
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
84 function(x) { return 3 * Math.sin(x); },
c0c0f25 @petercollingridge Simplify code
petercollingridge authored
85 function(x) { return x; },
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
86 function(x) { return 0.1 * x * x * x; },
87 function(x) { return 0.01 * x * x * x * x * x; }
88 ]
89 };
c0c0f25 @petercollingridge Simplify code
petercollingridge authored
90
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
91 var k1 = KhanUtil.randRangeNonZero(-3, 3);
92 var k2 = KhanUtil.randRange(-3, 3);
93 var k3 = KhanUtil.randRange(-3, 3);
94 var k4;
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
95
96 // Choose four even functions or four odd functions if we want a function
97 // that is even or odd respectively.
c0c0f25 @petercollingridge Simplify code
petercollingridge authored
98 if (type === "Even" || type === "Odd") {
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
99 k4 = KhanUtil.randRange(-3, 3);
c0c0f25 @petercollingridge Simplify code
petercollingridge authored
100 slice = KhanUtil.shuffle(funcs[type]).slice(0, 4);
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
101
102 // Choose two even and two odd functions if we want a function that
103 // is neither even nor odd.
104 } else {
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
105 k4 = KhanUtil.randRangeNonZero(-3, 3);
c0c0f25 @petercollingridge Simplify code
petercollingridge authored
106 slice = KhanUtil.shuffle(funcs['Even']).slice(0, 2).concat(
107 KhanUtil.shuffle(funcs['Odd']).slice(0, 2));
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
108 }
109
ba68d67 @petercollingridge Start improving hints
petercollingridge authored
110 var f = function(x) {
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
111 return slice[0](x) * k1 + slice[1](x) * k2 + slice[2](x) * k3 + slice[3](x) * k4;
ba68d67 @petercollingridge Start improving hints
petercollingridge authored
112 };
113
114 // Scale function so that f(x) is between -10 and +10 for x = -10 to +10
115 var s;
116 for (s=0; s<10; s++) {
117 if (Math.abs(f(s)) > 10) {
118 break;
119 }
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
120 }
121
ba68d67 @petercollingridge Start improving hints
petercollingridge authored
122 return function(x) { return f(x * s / 10); };
123 };
124
125 // Get the x-coordinates that we will use to show the function's value in hints.
126 // Filter out any points where the function is offscreen or both the pos and neg values are too close to zero.
127 function getUseablePoints(func, type) {
128 var arr = $.grep([1,2,3,4,5,6,7,8,9,10], function(i) {
129 return (Math.abs(func(i)) > 1 || Math.abs(func(-i)) > 1) && Math.abs(func(i)) < 10 && Math.abs(func(-i)) < 10;
130 })
131
132 // Filter further to remove any coordinate that would look kind of like it was even or odd.
133 // Also filter out 10 because that makes displaying things easier.
134 if (type === 'Neither') {
135 arr = $.grep(arr, function(i) {
136 return Math.abs(Math.abs(func(i)) - Math.abs(func(-i))) > 0.5 && Math.abs(i) < 10;
137 })
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
138 }
ba68d67 @petercollingridge Start improving hints
petercollingridge authored
139 return arr;
140 };
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
141 </script>
c65b7a3 @jruberg new exercise -- even and odd functionss
jruberg authored
142 </head>
143 <body>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
144 <div class="exercise">
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
145
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
146 <div class="vars">
2517248 @mauk81 localize solution, fix #78372
mauk81 authored
147 <var id="SOL_KEY, SOL_TEXT">randFromArray([["Even", $._("Even")], ["Odd", $._("Odd")], ["Neither", $._("Neither")]])</var>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
148 </div>
149
150 <div class="problems">
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
151
152 <div id="graph">
7a36a67 @mauk81 ensure function with 'usable points', fix #71147
mauk81 authored
153 <div class="vars" data-ensure="X_VALUES.length">
2517248 @mauk81 localize solution, fix #78372
mauk81 authored
154 <var id="FUNC">makeFunc(SOL_KEY)</var>
155 <var id="X_VALUES">getUseablePoints(FUNC, SOL_KEY)</var>
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
156 <var id="PT">randFromArray(X_VALUES)</var>
157 </div>
158
166863f @petercollingridge Make question more specific
petercollingridge authored
159 <p class="question">
160 According to the visible range of the graph, is <code>f(x)</code> even, odd, or neither?
161 </p>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
162 <div class="problem">
163 <p><code>f(x)</code> is graphed below.</p>
29be961 @alopatin Rearrange answer area content within some exercises
alopatin authored
164 <p class="render-answer-area-here"></p>
0f0c630 @cbhl Clean up the exercise files using the new cleaning utility. (II)
cbhl authored
165 <div class="graphie" id="graph">
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
166 graphInit({
167 range: 10,
168 scale: 20,
169 tickStep: 1,
e9807ff @jeresig Clean up the exercise files using the new cleaning utility.
jeresig authored
170 axisArrows: "&lt;-&gt;"
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
171 });
172
173 style({
ba68d67 @petercollingridge Start improving hints
petercollingridge authored
174 stroke: BLUE
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
175 });
176
ba68d67 @petercollingridge Start improving hints
petercollingridge authored
177 plot(function(x) {
178 return FUNC(x);
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
179 }, [-10, 10]);
180
181 style({
ba68d67 @petercollingridge Start improving hints
petercollingridge authored
182 stroke: RED
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
183 });
184 </div>
185 </div>
186
166863f @petercollingridge Make question more specific
petercollingridge authored
187 <div class="hints">
188 <div><p>
189 <div>A function is even if <code>f(x) = f(-x)</code> for all values of <code>x</code>.</div>
190 <div>A function is odd if <code>f(x) = -f(-x)</code> for all values of <code>x</code>.</div>
191 </p></div>
192
2517248 @mauk81 localize solution, fix #78372
mauk81 authored
193 <div data-if="SOL_KEY === 'Neither'" data-unwrap="">
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
194 <div>
195 <div class="graphie" data-update="graph">
196 style(
197 { strokeWidth: 2 },
198 function() {
199 path([[ PT, 0], [ PT, FUNC(PT)]]);
200 path([[-PT, 0], [-PT, FUNC(-PT)]]);
201 }
202 );
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
203
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
204 style(
205 { strokeDasharray: "." },
206 function() {
207 path([[0, FUNC(PT)], [PT, FUNC(PT)]]);
208 path([[0, FUNC(-PT)], [-PT, FUNC(-PT)]]);
209 }
210 );
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
211
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
212 </div>
c0c0f25 @petercollingridge Simplify code
petercollingridge authored
213
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
214 <p>
e85452d @petercollingridge Colour hints for graph exercise
petercollingridge authored
215 <div><code>\qquad f(\blue{<var>PT</var>}) \approx \red{<var>roundTo(1, FUNC(PT))</var>}</code></div>
216 <div><code>\qquad f(\blue{<var>-PT</var>}) \approx \red{<var>roundTo(1, FUNC(-PT))</var>}</code></div>
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
217 </p>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
218
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
219 </div>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
220
e85452d @petercollingridge Colour hints for graph exercise
petercollingridge authored
221 <p><code>f(\blue{<var>PT</var>}) \neq f(\blue{<var>-PT</var>})</code>, so <code>f(x)</code> is not even.</p>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
222
e85452d @petercollingridge Colour hints for graph exercise
petercollingridge authored
223 <p><code>f(\blue{<var>PT</var>}) \neq -f(\blue{<var>-PT</var>})</code>, so <code>f(x)</code> is not odd.</p>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
224
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
225 <p>Therefore <code>f(x)</code> is neither.</p>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
226
227 </div>
228
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
229 <div data-else="" data-unwrap="">
7fb5237 @petercollingridge Improve hints
petercollingridge authored
230 <div data-each="X_VALUES.slice(0,2) as x" data-unwrap="">
231 <div>
232 <div class="graphie" data-update="graph">
233 style(
234 { strokeWidth: 2 },
235 function() {
236 path([[ x, 0], [ x, FUNC(x)]]);
237 path([[-x, 0], [-x, FUNC(-x)]]);
238 }
239 );
240
241 style(
242 { strokeDasharray: "." },
243 function() {
244 path([[0, FUNC(x)], [x, FUNC(x)]]);
245 path([[0, FUNC(-x)], [-x, FUNC(-x)]]);
246 }
247 );
248 </div>
249
250 <p>
e85452d @petercollingridge Colour hints for graph exercise
petercollingridge authored
251 <div><code>\qquad f(\blue{<var>x</var>}) \approx \red{<var>roundTo(1, FUNC(x))</var>}</code></div>
252 <div><code>\qquad f(\blue{<var>-x</var>}) \approx \red{<var>roundTo(1, FUNC(-x))</var>}</code></div>
7fb5237 @petercollingridge Improve hints
petercollingridge authored
253 </p>
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
254 </div>
255 </div>
256
2517248 @mauk81 localize solution, fix #78372
mauk81 authored
257 <div data-if="SOL_KEY === 'Odd'">
7fb5237 @petercollingridge Improve hints
petercollingridge authored
258 <p><code>f(x)</code> is odd because <code>f(x) = -f(-x)</code>. (For all x values, not just the ones we checked!)</p>
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
259 </div>
260
261 <div data-else="">
7fb5237 @petercollingridge Improve hints
petercollingridge authored
262 <p><code>f(x)</code> is even because <code>f(x) = f(-x)</code>. (For all x values, not just the ones we checked!)</p>
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
263 </div>
264
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
265 </div>
266
267 </div>
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
268 </div>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
269
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
270 <div id="equation">
271 <div class="vars">
2517248 @mauk81 localize solution, fix #78372
mauk81 authored
272 <var id="FUNC">makeEquation(SOL_KEY)</var>
166863f @petercollingridge Make question more specific
petercollingridge authored
273 <var id="NEG_FUNC1, NEG_FUNC2">getNegativeFunction(FUNC)</var>
7fb5237 @petercollingridge Improve hints
petercollingridge authored
274 <var id="CALLABLE_FUNC">function(x){ return FUNC.evaluate(x); }</var>
2517248 @mauk81 localize solution, fix #78372
mauk81 authored
275 <var id="X_VALUES">getUseablePoints(CALLABLE_FUNC, SOL_KEY)</var>
166863f @petercollingridge Make question more specific
petercollingridge authored
276 <var id="SHOW_EVALUATION">FUNC.terms.length !== 1 || FUNC.terms[0].variableString !== ''</var>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
277 </div>
c85dd02 @jruberg cleaned up even_and_odd_functions hints with data-unwrap and also unw…
jruberg authored
278
166863f @petercollingridge Make question more specific
petercollingridge authored
279 <p class="question">Is <code>f(x)</code> even, odd, or neither?</p>
29be961 @alopatin Rearrange answer area content within some exercises
alopatin authored
280 <p class="problem"><code>f(x) = <var>FUNC</var></code></p>
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
281
166863f @petercollingridge Make question more specific
petercollingridge authored
282 <div class="hints">
283 <div><p>
284 <div>A function is even if <code>f(-x) = f(x)</code> for all values of <code>x</code>.</div>
285 <div>A function is odd if <code>f(-x) = -f(x)</code> for all values of <code>x</code>.</div>
286 </p></div>
7fb5237 @petercollingridge Improve hints
petercollingridge authored
287
166863f @petercollingridge Make question more specific
petercollingridge authored
288 <p>What is <code>f(\blue{-x})</code>?</p>
7fb5237 @petercollingridge Improve hints
petercollingridge authored
289
166863f @petercollingridge Make question more specific
petercollingridge authored
290 <p><code>f(\blue{-x}) = <var>NEG_FUNC1</var></code>
291 <p data-if="SHOW_EVALUATION"><code>f(\blue{-x}) = <var>NEG_FUNC2</var></code>
7fb5237 @petercollingridge Improve hints
petercollingridge authored
292
166863f @petercollingridge Make question more specific
petercollingridge authored
293 <div data-if="SOL_KEY === 'Odd'" data-unwrap="">
294 <p><code>f(\blue{-x}) = -(<var>FUNC</var>)</code></p>
295 <p><code>f(\blue{-x}) = -f(x)</code></p>
296 <p>Therefore, <code>f(x)</code> is odd.</p>
297 </div>
298 <div data-else-if="SOL_KEY === 'Even'" data-unwrap="">
299 <p><code>f(\blue{-x}) = f(x)</code></p>
300 <p>Therefore, <code>f(x)</code> is even.</p>
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
301 </div>
7fb5237 @petercollingridge Improve hints
petercollingridge authored
302 <div data-else="" data-unwrap="">
166863f @petercollingridge Make question more specific
petercollingridge authored
303 <p>
304 <code>f(-x) \neq f(x)</code>, since the signs of the terms with odd powers are different.
305 <code>f(-x) \neq -f(x)</code> since the signs of the terms with even powers are the same.
7fb5237 @petercollingridge Improve hints
petercollingridge authored
306 </p>
166863f @petercollingridge Make question more specific
petercollingridge authored
307 <p>Therefore, <code>f(x)</code> is neither odd nor even.</p>
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
308 </div>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
309 </div>
c85dd02 @jruberg cleaned up even_and_odd_functions hints with data-unwrap and also unw…
jruberg authored
310
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
311 </div>
312
313 </div>
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
314
2517248 @mauk81 localize solution, fix #78372
mauk81 authored
315 <p class="solution"><var>SOL_TEXT</var></p>
c592e37 @petercollingridge Add an exercise type showing equation not graph
petercollingridge authored
316 <ul class="choices" data-category="true">
317 <li>Even</li>
318 <li>Odd</li>
319 <li>Neither</li>
320 </ul>
4e4cb9b @beneater lint: tabs->spaces and jQuery->$ for exercises
beneater authored
321 </div>
cde9e9f @jeresig Re-run the exercises through the updated cleaner.
jeresig authored
322 </body>
323 </html>
Something went wrong with that request. Please try again.