Linear Function Calculator

Linear Function Calculator

Find the equation of a line using two points

Point 1 (x₁, y₁)

Point 2 (x₂, y₂)

Understanding Linear Functions

A linear function is a mathematical relationship that can be represented by a straight line on a Cartesian coordinate system. It is defined by a constant rate of change, known as the slope (m), and a starting point on the y-axis, known as the y-intercept (b).

The Slope-Intercept Form Formula

y = mx + b

  • y: The dependent variable (output).
  • x: The independent variable (input).
  • m: The slope, calculated as (y₂ – y₁) / (x₂ – x₁).
  • b: The y-intercept, where the line crosses the y-axis (when x = 0).

Real-World Example

Imagine you are taking a taxi. The taxi company charges a flat "entry fee" of 5.00 (the y-intercept) and then 2.00 per mile traveled (the slope). The function representing your total cost would be:

f(x) = 2x + 5

If you travel 10 miles (x=10), your cost would be 2(10) + 5 = 25.00.

Calculating the Slope Manually

To find the slope between two points (2, 4) and (5, 10):

  1. Subtract the y-coordinates: 10 – 4 = 6
  2. Subtract the x-coordinates: 5 – 2 = 3
  3. Divide the difference in y by the difference in x: 6 / 3 = 2
  4. The slope (m) is 2.
function calculateLinearFunction() { var x1 = parseFloat(document.getElementById('x1_val').value); var y1 = parseFloat(document.getElementById('y1_val').value); var x2 = parseFloat(document.getElementById('x2_val').value); var y2 = parseFloat(document.getElementById('y2_val').value); var resultDiv = document.getElementById('linear_result'); if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) { resultDiv.style.display = 'block'; resultDiv.style.backgroundColor = '#f8d7da'; resultDiv.style.color = '#721c24'; resultDiv.innerHTML = 'Error: Please enter valid numerical values for all coordinates.'; return; } if (x1 === x2) { resultDiv.style.display = 'block'; resultDiv.style.backgroundColor = '#fff3cd'; resultDiv.style.color = '#856404'; if (y1 === y2) { resultDiv.innerHTML = 'Undefined: Both points are identical. An infinite number of lines can pass through a single point.'; } else { resultDiv.innerHTML = 'Vertical Line: The slope is undefined (division by zero). The equation of this line is: x = ' + x1 + ''; } return; } // Calculate Slope (m) var m = (y2 – y1) / (x2 – x1); // Calculate Y-intercept (b) -> y = mx + b => b = y – mx var b = y1 – (m * x1); // X-intercept -> 0 = mx + b => x = -b / m var xIntercept = (m !== 0) ? (-b / m) : null; // Build the equation string var mDisp = Math.round(m * 1000) / 1000; var bDisp = Math.round(b * 1000) / 1000; var bSign = bDisp >= 0 ? "+ " : "- "; var bAbs = Math.abs(bDisp); var equation = "y = " + mDisp + "x " + (bDisp !== 0 ? bSign + bAbs : ""); if (mDisp === 0) equation = "y = " + bDisp; resultDiv.style.display = 'block'; resultDiv.style.backgroundColor = '#e8f4fd'; resultDiv.style.color = '#0c5460'; resultDiv.style.border = '1px solid #bee5eb'; var html = '

Results:

'; html += 'Equation of the Line:' + equation + ''; html += '
'; html += '
Slope (m): ' + mDisp + '
'; html += '
Y-Intercept (b): ' + bDisp + '
'; html += '
X-Intercept: ' + (xIntercept !== null ? (Math.round(xIntercept * 1000) / 1000) : "None (Parallel to x-axis)") + '
'; html += '
Line Direction: ' + (m > 0 ? "Increasing" : (m < 0 ? "Decreasing" : "Horizontal")) + '
'; html += '
'; resultDiv.innerHTML = html; }

Leave a Comment