The tangent line to a curve is a line that passes through a single local point on the curve, that also matches the same slope. That means, the derivative of the curve must match the slope of the tangent line.
Let's assume our curve is defined by the formula below and we want to find the tangent line at
We could plug in
Finally, we can rewrite this formula in y-intercept form
Follow these steps to create a new project workspace and install the smath dependency to run this example.
# Create and open project folder
mkdir Tangent_Line_demo
cd Tangent_Line_demo
# Initialize project and install dependencies
npm init -y
npm i smath@1.12.0
# Create and open source file
touch "Tangent Line.mjs"
open "Tangent Line.mjs"
Copy and paste this source code into Tangent Line.mjs
.
import { SMath } from 'smath';
// Define our curve function
function f(x) {
return 1 / 8 * x * x - x - 4;
}
// Obtain our point (x0, y0)
const x0 = 1;
const y0 = f(x0);
// Use SMath to obtain the slope at our point
const m = SMath.differentiate(f, x0);
// Use SMath to decompose the slope and
// y-intercept into fractions.
const m_frac = SMath.rat(m);
const b_frac = SMath.rat(y0 - m * x0);
// Print results
console.log('y = ' + m_frac.num + '/' + m_frac.den + ' x + ' + b_frac.num + '/' + b_frac.den);
In Tangent_Line_demo/
, execute Tangent Line.mjs
with NodeJS to generate an output.
node "Tangent Line.mjs"
You should expect to see an output similar to the one below.
y = -3/4 x + -33/8