Permalink
Fetching contributors…
Cannot retrieve contributors at this time
324 lines (269 sloc) 13.4 KB
<!DOCTYPE html>
<!-- TDOO: remove <var>s in graphie data-eachs -->
<html data-require="math rational-expressions graphie">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Even and odd functions</title>
<script data-main="../local-only/main.js" src="../local-only/require.js"></script>
<script type="text/javascript">
function makeEquation(type) {
var funcs = {
"Even": [
new KhanUtil.Term(1),
new KhanUtil.Term(1, {'x': 2}),
new KhanUtil.Term(1, {'x': 4})
],
"Odd": [
new KhanUtil.Term(1, 'x'),
new KhanUtil.Term(1, {'x': 3}),
new KhanUtil.Term(1, {'x': 5})
]
};
var equation = new KhanUtil.RationalExpression([]);
if (type === "Even" || type === "Odd") {
var f = KhanUtil.shuffle(funcs[type]);
for (var i=0; i<KhanUtil.randRange(1, 3); i++) {
var term = f[i].multiply(KhanUtil.randRangeNonZero(-5, 5));
equation.terms.push(term);
}
} else {
var f1 = KhanUtil.shuffle(funcs["Even"]);
var f2 = KhanUtil.shuffle(funcs["Odd"]);
var term1 = f1[0].multiply(KhanUtil.randRangeNonZero(-5, 5));
var term2 = f2[0].multiply(KhanUtil.randRangeNonZero(-5, 5));
equation.terms.push(term1);
equation.terms.push(term2);
}
return equation;
};
function getNegativeFunction(f) {
var terms1 = [];
var terms2 = [];
// Copy own terms
for (var i = 0; i < f.terms.length; i++) {
var term = f.terms[i];
var variables2 = { "(\\blue{x})": term.variables.x };
var coefficient = (term.variables.x % 2 === 1) ? -term.coefficient : term.coefficient;
var variables1 = {};
var variables2 = {};
if (term.variables.x) {
variables1["(\\blue{-x})"] = term.variables.x;
variables2["\\blue{x}"] = term.variables.x;
}
terms1.push([term.coefficient, variables1]);
terms2.push([coefficient, variables2]);
}
var negativeF1 = new KhanUtil.RationalExpression(terms1);
var negativeF2 = new KhanUtil.RationalExpression(terms2);
return [negativeF1, negativeF2];
};
function makeFunc(type) {
var slice;
var funcs = {
"Even": [
function(x) { return 1; },
function(x) { return 3 * Math.cos(x); },
Math.abs,
function(x) { return 0.25 * x * x; },
function(x) { return 0.05 * x * x * x * x; }
],
"Odd": [
function(x) { return 3 * Math.sin(x); },
function(x) { return x; },
function(x) { return 0.1 * x * x * x; },
function(x) { return 0.01 * x * x * x * x * x; }
]
};
var k1 = KhanUtil.randRangeNonZero(-3, 3);
var k2 = KhanUtil.randRange(-3, 3);
var k3 = KhanUtil.randRange(-3, 3);
var k4;
// Choose four even functions or four odd functions if we want a function
// that is even or odd respectively.
if (type === "Even" || type === "Odd") {
k4 = KhanUtil.randRange(-3, 3);
slice = KhanUtil.shuffle(funcs[type]).slice(0, 4);
// Choose two even and two odd functions if we want a function that
// is neither even nor odd.
} else {
k4 = KhanUtil.randRangeNonZero(-3, 3);
slice = KhanUtil.shuffle(funcs['Even']).slice(0, 2).concat(
KhanUtil.shuffle(funcs['Odd']).slice(0, 2));
}
var f = function(x) {
return slice[0](x) * k1 + slice[1](x) * k2 + slice[2](x) * k3 + slice[3](x) * k4;
};
// Scale function so that f(x) is between -10 and +10 for x = -10 to +10
var s;
for (s=0; s<10; s++) {
if (Math.abs(f(s)) > 10) {
break;
}
}
return function(x) { return f(x * s / 10); };
};
// Get the x-coordinates that we will use to show the function's value in hints.
// Filter out any points where the function is offscreen or both the pos and neg values are too close to zero.
function getUseablePoints(func, type) {
var arr = $.grep([1,2,3,4,5,6,7,8,9,10], function(i) {
return (Math.abs(func(i)) > 1 || Math.abs(func(-i)) > 1) && Math.abs(func(i)) < 10 && Math.abs(func(-i)) < 10;
})
// Filter further to remove any coordinate that would look kind of like it was even or odd.
// Also filter out 10 because that makes displaying things easier.
if (type === 'Neither') {
arr = $.grep(arr, function(i) {
return Math.abs(Math.abs(func(i)) - Math.abs(func(-i))) > 0.5 && Math.abs(i) < 10;
})
}
return arr;
};
</script>
</head>
<body>
<div class="exercise">
<div class="vars">
<var id="SOL_KEY, SOL_TEXT">randFromArray([["Even", i18n._("Even")], ["Odd", i18n._("Odd")], ["Neither", i18n._("Neither")]])</var>
</div>
<div class="problems">
<div id="graph">
<div class="vars" data-ensure="X_VALUES.length">
<var id="FUNC">makeFunc(SOL_KEY)</var>
<var id="X_VALUES">getUseablePoints(FUNC, SOL_KEY)</var>
<var id="PT">randFromArray(X_VALUES)</var>
</div>
<p class="question">
According to the visible range of the graph, is <code>f(x)</code> even, odd, or neither?
</p>
<div class="problem">
<p><code>f(x)</code> is graphed below.</p>
<p class="render-answer-area-here"></p>
<div class="graphie" id="graph">
graphInit({
range: 10,
scale: 20,
tickStep: 1,
axisArrows: "&lt;-&gt;"
});
style({
stroke: BLUE
});
plot(function(x) {
return FUNC(x);
}, [-10, 10]);
style({
stroke: RED
});
</div>
</div>
<div class="hints">
<div><p>
<div>A function is even if <code>f(x) = f(-x)</code> for all values of <code>x</code>.</div>
<div>A function is odd if <code>f(x) = -f(-x)</code> for all values of <code>x</code>.</div>
</p></div>
<div data-if="SOL_KEY === 'Neither'" data-unwrap="">
<div>
<div class="graphie" data-update="graph">
style(
{ strokeWidth: 2 },
function() {
path([[ PT, 0], [ PT, FUNC(PT)]]);
path([[-PT, 0], [-PT, FUNC(-PT)]]);
}
);
style(
{ strokeDasharray: "." },
function() {
path([[0, FUNC(PT)], [PT, FUNC(PT)]]);
path([[0, FUNC(-PT)], [-PT, FUNC(-PT)]]);
}
);
</div>
<p>
<div><code>\qquad f(\blue{<var>PT</var>}) \approx \red{<var>roundTo(1, FUNC(PT))</var>}</code></div>
<div><code>\qquad f(\blue{<var>-PT</var>}) \approx \red{<var>roundTo(1, FUNC(-PT))</var>}</code></div>
</p>
</div>
<p><code>f(\blue{<var>PT</var>}) \neq f(\blue{<var>-PT</var>})</code>, so <code>f(x)</code> is not even.</p>
<p><code>f(\blue{<var>PT</var>}) \neq -f(\blue{<var>-PT</var>})</code>, so <code>f(x)</code> is not odd.</p>
<p>Therefore <code>f(x)</code> is neither.</p>
</div>
<div data-else="" data-unwrap="">
<div data-each="X_VALUES.slice(0,2) as x" data-unwrap="">
<div>
<div class="graphie" data-update="graph">
style(
{ strokeWidth: 2 },
function() {
path([[ x, 0], [ x, FUNC(x)]]);
path([[-x, 0], [-x, FUNC(-x)]]);
}
);
style(
{ strokeDasharray: "." },
function() {
path([[0, FUNC(x)], [x, FUNC(x)]]);
path([[0, FUNC(-x)], [-x, FUNC(-x)]]);
}
);
</div>
<p>
<div><code>\qquad f(\blue{<var>x</var>}) \approx \red{<var>roundTo(1, FUNC(x))</var>}</code></div>
<div><code>\qquad f(\blue{<var>-x</var>}) \approx \red{<var>roundTo(1, FUNC(-x))</var>}</code></div>
</p>
</div>
</div>
<div data-if="SOL_KEY === 'Odd'">
<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>
</div>
<div data-else="">
<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>
</div>
</div>
</div>
</div>
<div id="equation">
<div class="vars">
<var id="FUNC">makeEquation(SOL_KEY)</var>
<var id="NEG_FUNC1, NEG_FUNC2">getNegativeFunction(FUNC)</var>
<var id="CALLABLE_FUNC">function(x){ return FUNC.evaluate(x); }</var>
<var id="X_VALUES">getUseablePoints(CALLABLE_FUNC, SOL_KEY)</var>
<var id="SHOW_EVALUATION">FUNC.terms.length !== 1 || FUNC.terms[0].variableString !== ''</var>
</div>
<p class="question">Is <code>f(x)</code> even, odd, or neither?</p>
<p class="problem"><code>f(x) = <var>FUNC</var></code></p>
<div class="hints">
<div><p>
<div>A function is even if <code>f(-x) = f(x)</code> for all values of <code>x</code>.</div>
<div>A function is odd if <code>f(-x) = -f(x)</code> for all values of <code>x</code>.</div>
</p></div>
<p>What is <code>f(\blue{-x})</code>?</p>
<p><code>f(\blue{-x}) = <var>NEG_FUNC1</var></code>
<p data-if="SHOW_EVALUATION"><code>f(\blue{-x}) = <var>NEG_FUNC2</var></code>
<div data-if="SOL_KEY === 'Odd'" data-unwrap="">
<p><code>f(\blue{-x}) = -(<var>FUNC</var>)</code></p>
<p><code>f(\blue{-x}) = -f(x)</code></p>
<p>Therefore, <code>f(x)</code> is odd.</p>
</div>
<div data-else-if="SOL_KEY === 'Even'" data-unwrap="">
<p><code>f(\blue{-x}) = f(x)</code></p>
<p>Therefore, <code>f(x)</code> is even.</p>
</div>
<div data-else="" data-unwrap="">
<p>
<code>f(-x) \neq f(x)</code>, since the signs of the terms with odd powers are different.
<code>f(-x) \neq -f(x)</code> since the signs of the terms with even powers are the same.
</p>
<p>Therefore, <code>f(x)</code> is neither odd nor even.</p>
</div>
</div>
</div>
</div>
<p class="solution"><var>SOL_TEXT</var></p>
<ul class="choices" data-category="true">
<li>Even</li>
<li>Odd</li>
<li>Neither</li>
</ul>
</div>
</body>
</html>