This repository has been archived by the owner on May 11, 2021. It is now read-only.
/
views_of_a_function.html
388 lines (358 loc) · 17.3 KB
/
views_of_a_function.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<!DOCTYPE html>
<html data-require="math math-format graphie interactive">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Views of a function</title>
<script data-main="../local-only/main.js" src="../local-only/require.js"></script>
<script type="text/javascript">
chooseXValues = function(func, scale) {
var xs = [];
_.each(_.range(-9, 10), function(x) {
var y = func(x);
if (Math.abs(y) > 9) {
return;
}
if (Math.abs(y - Math.round(y * scale) / scale) < 0.1) {
xs.push(x);
}
});
return xs;
};
generatePiecemealFunction = function() {
var n = KhanUtil.randRange(2, 3);
var xs = [-11];
for (var i = 0; i < n; i++) {
xs.push(KhanUtil.randRange(xs[i] + 3, 11 - (n - i) * 3));
}
xs.push(11);
var ys = [];
for (var i = 0; i < xs.length; i++) {
ys.push(KhanUtil.randRange(-9, 9));
}
var grads = [];
for (var i = 1; i < xs.length; i++) {
grads.push((ys[i] - ys[i - 1]) / (xs[i] - xs[i - 1]));
}
return function(x) {
for (var i = 0; i < grads.length; i++) {
if (x <= xs[i + 1]) {
return (x - xs[i]) * grads[i] + ys[i];
}
}
};
};
</script>
<style type="text/css">
.ttable {
text-align: right;
}
.ttable tr {
height: 23px;
}
.ttable .label {
text-align: left;
}
.ttable td {
padding: 5px 10px;
}
.ttable .first-row {
border-top: 2px solid #ccc;
background-color: #ededed;
}
#answer_area .ttable input {
width: 60px;
}
</style>
</head>
<body>
<div class="exercise">
<div class="vars" data-ensure="X_EXAMPLES.length === 3">
<var id="A">randRangeNonZero(-3, 3)</var>
<var id="B">randRangeNonZero(-3, 3)</var>
<var id="C">randRangeNonZero(-3, 3)</var>
<var id="F">randFromArray(["f", "g", "h"])</var>
<var id="X,Y">randFromArray([["t", "u"], ["v", "w"], ["x", "y"]])</var>
<var id="N">rand(5)</var>
<var id="FUNC">[
generatePiecemealFunction(),
function(x) { return B * x + C; },
function(x) { return B / 2 * x * x + A * x + C; },
function(x) { return B * sin(C * x / Math.PI) + A; },
function(x) { return B * pow(Math.E, C * x / 3) + A; }
][N]</var>
<var id="X_EXAMPLES">(function() {
var xs = chooseXValues(FUNC, 1);
if (xs.length >= 3) {
return sortNumbers(shuffle(xs, 3));
} else {
xs = chooseXValues(FUNC, 2);
return sortNumbers(shuffle(xs, 3));
}
})()</var>
<var id="Y_EXAMPLES">_.map(X_EXAMPLES, FUNC)</var>
<var id="Y_EXAMPLES_R">_.map(Y_EXAMPLES, function(y) { return round(2 * y) / 2; })</var>
</div>
<div class="problems">
<div id="graph-to-table">
<div class="vars">
<var id="CORRECTOFFSET">0.5</var>
</div>
<p class="question">
Choose any three points on the following graph of <code><var>Y</var> = <var>F</var>(<var>X</var>)</code>,
and fill the table so each column represents a point.
</p>
<p>
<em>If necessary, round your answers to the nearest <code><var>CORRECTOFFSET</var></code>.</em>
</p>
<div class="problem">
<p class="render-answer-area-here"></p>
<div class="graphie" id="main">
graphInit({
range: 10,
scale: 20,
tickStep: 1,
labelStep: 1,
unityLabels: false,
labelFormat: function(s) {
return "\\small{" + s + "}";
},
axisArrows: "<->",
axisLabels: [X, Y]
});
plot(FUNC, [-10, 10], { stroke: BLUE });
</div>
</div>
<div class="solution" data-type="custom">
<div class="instruction">
<table class="ttable">
<tbody>
<tr class="first-row">
<td class="label"><code><var>X</var></code></td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td class="label"><code><var>F</var>(<var>X</var>)</code></td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
</tbody>
</table>
</div>
<div class="guess">
(function(){
var guess = [];
var rows = $(".ttable tr");
for (var i = 1; i < 4; i++) {
var x = $(rows[0].children[i]).find("input").val();
var y = $(rows[1].children[i]).find("input").val();
guess.push([x, y]);
}
return guess;
})()
</div>
<div class="validator-function">
var attempted = 0;
var correct = 0;
var xs = [];
var i = 0;
var validatePoint = Khan.answerTypes.predicate.createValidatorFunctional(function(x, maxError) {
var validateY = Khan.answerTypes.predicate.createValidatorFunctional(function(y, maxError) {
if (abs(FUNC(x) - y) < maxError) {
correct += 1;
xs.push(x);
return true;
}
}, {forms: 'decimal', inexact: true, maxError: maxError});
var result = validateY(guess[i][1]);
return result.correct;
}, {forms: 'decimal', inexact: true, maxError: CORRECTOFFSET});
for (i = 0; i < 3; ++i) {
if ($.trim(guess[i][0]) !== "" &&
$.trim(guess[i][1]) !== "") {
attempted += 1;
validatePoint(guess[i][0]);
}
}
if (attempted === 0) {
return "";
}
if (attempted < 3) {
return i18n._("You must enter three points");
}
xs = KhanUtil.sortNumbers(xs);
var different = 1;
for (var i = 0; i < correct - 1; ++i) {
if (xs[i + 1] - xs[i] >= 0.5) {
different += 1;
}
}
return different === attempted;
</div>
</div>
<div class="hints">
<div>
<p>
We can start by choosing three <em>input</em> values.
For example, let's use the input values <code><var>X_EXAMPLES[0]</var></code>,
<code><var>X_EXAMPLES[1]</var></code>, and <code><var>X_EXAMPLES[2]</var></code>.</p>
<p>
<p>
Now we can find the corresponding <em>output</em> values to the input values we chose,
using the graph: The coordinates of every point on the graph of <code><var>Y</var> = f(<var>X</var>)</code>
can be written as <code>(<var>X</var>, <var>Y</var>)</code>, where the <code><var>X</var></code>-coordinate represents
the input value and the <code><var>Y</var></code>-coordinate represents the corresponding output value.
</p>
</div>
<div>
<p>
As we can see on the graph, the point with an
<code>\blue{x}</code>-coordinate of <code>\blue{<var>X_EXAMPLES[0]</var>}</code> has a
<code>\pink{y}</code>-coordinate of <code>\pink{<var>Y_EXAMPLES_R[0]</var>}</code>.
In other words, <code><var>F</var>(\blue{<var>X_EXAMPLES[0]</var>}) = \pink{<var>Y_EXAMPLES_R[0]</var>}</code>.
</p>
<div class="graphie" data-update="main">
line([X_EXAMPLES[0], -11], [X_EXAMPLES[0], 11], { strokeWidth: 1, stroke: PINK });
circle([X_EXAMPLES[0], Y_EXAMPLES[0]], 0.2, { stroke: PINK, fill: PINK });
</div>
<p>
To record this in our table, we put
<code>\blue{<var>X_EXAMPLES[0]</var>}</code> in the <code>x</code>-row and
<code>\pink{<var>Y_EXAMPLES_R[0]</var>}</code> in the <code><var>F</var>(<var>X</var>)</code>-row
below it.
</p>
</div>
<div>
<p>We can do the same for the other two input values we chose.</p>
<p><code>\quad
\begin{align}
<code><var>F</var>(\blue{<var>X_EXAMPLES[1]</var>}) &= \pink{<var>Y_EXAMPLES_R[1]</var>}</code> \\
<code><var>F</var>(\blue{<var>X_EXAMPLES[2]</var>}) &= \pink{<var>Y_EXAMPLES_R[2]</var>}</code>
\end{align}
</code></p>
<div class="graphie" data-update="main">
line([X_EXAMPLES[1], -11], [X_EXAMPLES[1], 11], { strokeWidth: 1, stroke: PINK });
circle([X_EXAMPLES[1], Y_EXAMPLES[1]], 0.2, { stroke: PINK, fill: PINK });
line([X_EXAMPLES[2], -11], [X_EXAMPLES[2], 11], { strokeWidth: 1, stroke: PINK });
circle([X_EXAMPLES[2], Y_EXAMPLES[2]], 0.2, { stroke: PINK, fill: PINK });
</div>
</div>
<div>
<p>This is a possible complete table:</p>
<table class="ttable">
<tbody>
<tr class="first-row">
<td class="label"><code><var>X</var></code></td>
<td><code><var>X_EXAMPLES[0]</var></code></td>
<td><code><var>X_EXAMPLES[1]</var></code></td>
<td><code><var>X_EXAMPLES[2]</var></code></td>
</tr>
<tr>
<td class="label"><code><var>F</var>(<var>X</var>)</code></td>
<td><code><var>Y_EXAMPLES_R[0]</var></code></td>
<td><code><var>Y_EXAMPLES_R[1]</var></code></td>
<td><code><var>Y_EXAMPLES_R[2]</var></code></td>
</tr>
</tbody>
</table>
<p>Note: There can be other correct tables that contain different points.</p>
</div>
</div>
</div>
<div id="equation-to-table" data-type="graph-to-table">
<div class="vars">
<var id="N">rand(3)</var>
<var id="FUNC">[
function(x) { return B * x + C; },
function(x) { return A * x + A * B + C; },
function(x) { return abs(B) * x * x + C; }
][N]</var>
<var id="FUNCSHOW">[
coefficient(B) + X + " + " + C,
coefficient(A) + "(" + X + " + " + B + ") + " + C,
coefficient(abs(B)) + X + "^2 + " + C
][N]</var>
<var id="X_EXAMPLES">(function() {
var xs = chooseXValues(FUNC, 1);
if (xs.length >= 3) {
return sortNumbers(shuffle(xs, 3));
} else {
xs = chooseXValues(FUNC, 2);
return sortNumbers(shuffle(xs, 3));
}
})()</var>
<var id="HINT">[
coefficient(B) + "\\blue{" + X_EXAMPLES[0] + "} + " + C,
coefficient(A) + "(\\blue{" + X_EXAMPLES[0] + "}" + B + ") + " + C,
coefficient(abs(B)) + "\\blue{" + X_EXAMPLES[0] + "}^2 + " + C
][N]</var>
<var id="Y_EXAMPLES">_.map(X_EXAMPLES, FUNC)</var>
<var id="Y_EXAMPLES_R">_.map(Y_EXAMPLES, function(y) { return "" + round(10 * y) / 10; })</var>
<var id="CORRECTOFFSET">0.1</var>
</div>
<p class="question">
Pick any three pairs of corresponding input and output values
of the following function, and fill the table accordingly.
</p>
<p class="problem"><code><var>F</var>(<var>X</var>) = <var>FUNCSHOW</var></code></p>
<p class="render-answer-area-here"></p>
<div class="hints">
<div>
<p>
We can start by choosing three <em>input</em> values.
For example, let's use the input values <code><var>X_EXAMPLES[0]</var></code>,
<code><var>X_EXAMPLES[1]</var></code>, and <code><var>X_EXAMPLES[2]</var></code>.
</p>
<p>
Now we can find the corresponding <em>output</em> values to the input values we chose, using the function's formula:
For any input value <code>a</code>, we can find its corresponding output value by substituting <code><var>X</var> = a</code>
into the formula for <code><var>F</var>(<var>X</var>)</code>.
</p>
</div>
<div>
<p>Now find the output value that corresponds to <code>\blue{x}=\blue{<var>X_EXAMPLES[0]</var>}</code>.</p>
<p><code>\quad <var>F</var>(\blue{<var>X_EXAMPLES[0]</var>}) = <var>HINT</var></code>
<p><code>\quad <var>F</var>(\blue{<var>X_EXAMPLES[0]</var>}) = \pink{<var>Y_EXAMPLES_R[0]</var>}</code>
<p>
To record this in the table, we put <code>\blue{<var>X_EXAMPLES[0]</var>}</code> in the <code><var>X</var></code>-row
and <code>\pink{<var>Y_EXAMPLES_R[0]</var>}</code> in the <code><var>F</var>(<var>X</var>)</code>-row located directly below it.
</p>
<p>We can do the same for the other two input values we chose.</p>
</div>
<div>
<p>We can do the same for the other two input values we chose.</p>
<p><code>
\quad \begin{align}
<code><var>F</var>(\blue{<var>X_EXAMPLES[1]</var>}) &= \pink{<var>Y_EXAMPLES_R[1]</var>}</code> \\
<code><var>F</var>(\blue{<var>X_EXAMPLES[2]</var>}) &= \pink{<var>Y_EXAMPLES_R[2]</var>}</code>
\end{align}
</code></p>
</div>
<div>
<p>This is a possible complete table:</p>
<table class="ttable">
<tbody>
<tr class="first-row">
<td class="label"><code><var>X</var></code></td>
<td><code><var>X_EXAMPLES[0]</var></code></td>
<td><code><var>X_EXAMPLES[1]</var></code></td>
<td><code><var>X_EXAMPLES[2]</var></code></td>
</tr>
<tr>
<td class="label"><code><var>F</var>(<var>X</var>)</code></td>
<td><code><var>Y_EXAMPLES_R[0]</var></code></td>
<td><code><var>Y_EXAMPLES_R[1]</var></code></td>
<td><code><var>Y_EXAMPLES_R[2]</var></code></td>
</tr>
</tbody>
</table>
<p>Note: There can be other correct tables that contain different points.</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>