๐ง Let's Learn About Slopes with Cubes! (f(x) = x³)
Graph of f(x) = x³ and Its Tangent Line
๐ง Let's Learn About Slopes with Cubes! (f(x) = x³)
Hi friends! ๐
Have you ever wondered how we can figure out how steep a curve is at one single point?
Well, that’s what slopes and derivatives help us do! Today, we’re going to use a special math function:
This just means we multiply a number by itself 3 times. So if x = 2, then f(2) = 2 × 2 × 2 = 8.
๐ What Does This Program Do?
This fun little program lets you:
-
Type in any number for
x -
Draw the curve of
f(x) = x³(it’s a bendy line!) -
Show the slope (how steep it is) at the point you typed in
-
Draw a red line that touches the curve at just that point — this is called the tangent line
๐ What is a Derivative?
A derivative is like a magic formula that tells us the slope of a curve at any point.
To find the slope, we use this formula:
This means we take two very, very close points and see how much the function changes. That gives us the slope!
For our cube function:
the derivative (slope formula) is:
So, if you type x = 2, the slope is:
That means the curve is going up super fast at x = 2!
๐จ What’s in the Drawing?
๐ฆ Blue curve = the function f(x) = x³
๐ด Red line = the slope (or tangent) at your point
➕ You also see:
-
What
f(x)equals -
What
f′(x)(the slope) is -
The red line’s formula like:
y = 12x - 16
๐งช Try Changing the x Value!
Try typing in:
-
0— the slope is flat! -
1— slope is 3 -
-2— slope is positive again, because (-2)² = 4
It’s fun to explore how steep the curve is at different spots. ๐
๐ Full Source Code (Copy & Paste into a .html file!)
<!DOCTYPE html>
<html>
<head>
<title>f(x) = x³ and Its Derivative</title>
<style>
canvas {
border: 1px solid black;
margin-top: 10px;
}
body {
font-family: sans-serif;
}
.formula {
font-size: 18px;
margin: 10px 0;
}
</style>
</head>
<body>
<h2>Graph of f(x) = x³ and Its Tangent Line</h2>
<label for="xInput">Enter x value:</label>
<input type="number" id="xInput" value="1" step="any">
<button onclick="drawGraph()">Draw</button>
<div class="formula" id="fxValue"></div>
<div class="formula" id="derivativeFormula">f′(x) = 3x²</div>
<div class="formula" id="slopeValue"></div>
<div class="formula" id="tangentFormula"></div>
<canvas id="graphCanvas" width="600" height="400"></canvas>
<script>
const canvas = document.getElementById('graphCanvas');
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
const scaleX = 40; // pixels per unit x
const scaleY = 0.01; // pixels per unit y (y grows fast for x³)
function f(x) {
return x * x * x;
}
function fPrime(x) {
return 3 * x * x;
}
function drawAxes() {
ctx.strokeStyle = '#aaa';
ctx.lineWidth = 1;
// x-axis
ctx.beginPath();
ctx.moveTo(0, height / 2);
ctx.lineTo(width, height / 2);
ctx.stroke();
// y-axis
ctx.beginPath();
ctx.moveTo(width / 2, 0);
ctx.lineTo(width / 2, height);
ctx.stroke();
}
function drawFunction() {
ctx.beginPath();
ctx.strokeStyle = 'blue';
ctx.lineWidth = 2;
for (let px = 0; px < width; px++) {
const x = (px - width / 2) / scaleX;
const y = f(x);
const py = height / 2 - y / scaleY;
if (px === 0) {
ctx.moveTo(px, py);
} else {
ctx.lineTo(px, py);
}
}
ctx.stroke();
}
function drawTangent(x0, y0, slope) {
const lineLength = 2;
const x1 = x0 - lineLength;
const y1 = y0 - slope * lineLength;
const x2 = x0 + lineLength;
const y2 = y0 + slope * lineLength;
const px1 = width / 2 + x1 * scaleX;
const py1 = height / 2 - y1 / scaleY;
const px2 = width / 2 + x2 * scaleX;
const py2 = height / 2 - y2 / scaleY;
ctx.beginPath();
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.moveTo(px1, py1);
ctx.lineTo(px2, py2);
ctx.stroke();
}
function drawGraph() {
const x0 = parseFloat(document.getElementById('xInput').value);
const y0 = f(x0);
const slope = fPrime(x0);
const b = y0 - slope * x0;
const slopeText = `f′(${x0}) = 3 × ${x0}² = ${slope}`;
const tangentText = `Tangent line: y = ${slope.toFixed(2)}x + ${b.toFixed(2)}`;
// Update formulas
document.getElementById('fxValue').innerText = `f(${x0}) = ${y0}`;
document.getElementById('slopeValue').innerText = slopeText;
document.getElementById('tangentFormula').innerText = tangentText;
// Clear canvas and draw everything
ctx.clearRect(0, 0, width, height);
drawAxes();
drawFunction();
drawTangent(x0, y0, slope);
}
// Draw on page load
drawGraph();
</script>
</body>
</html>
Comments
Post a Comment