Linear Programming Calculator

Linear Programming Solver (2 Variables)

Objective Function

Maximize Z = (a)x + (b)y

Constraints (≤ format)

Enter coefficients for: (C1)x + (C2)y ≤ Value

1: x + y ≤
2: x + y ≤
3: x + y ≤

* Non-negativity constraints (x ≥ 0, y ≥ 0) are applied automatically.

Optimization Result


Understanding Linear Programming

Linear Programming (LP) is a mathematical modeling technique used to achieve the best outcome (such as maximum profit or lowest cost) in a mathematical model whose requirements are represented by linear relationships. This specific tool solves 2-variable maximization problems using the Graphical Method logic.

Core Components of a Linear Programming Problem

  • Decision Variables (x and y): These represent the quantities we want to determine (e.g., number of units of Product A and Product B to manufacture).
  • Objective Function: The linear equation you want to maximize (Profit) or minimize (Cost).
  • Constraints: Linear inequalities that represent the limitations of your resources, such as labor hours, raw materials, or budget.
  • Feasible Region: The set of all possible points (x, y) that satisfy all constraints simultaneously.

Real-World Example: Production Mix

Imagine a carpenter makes tables (x) and chairs (y).

  • Profit: Each table yields $30 profit, and each chair yields $50 profit. (Maximize Z = 30x + 50y)
  • Constraint 1 (Wood): Tables use 1 unit of wood, chairs use 2 units. Total wood available is 10 units. (1x + 2y ≤ 10)
  • Constraint 2 (Labor): Tables take 4 hours, chairs take 1 hour. Total labor available is 12 hours. (4x + 1y ≤ 12)

By entering these values into the calculator above, you can find the exact production mix that yields the highest possible profit without exceeding resources.

function solveLP() { var a = parseFloat(document.getElementById('objA').value) || 0; var b = parseFloat(document.getElementById('objB').value) || 0; var c1 = { x: parseFloat(document.getElementById('c1x').value) || 0, y: parseFloat(document.getElementById('c1y').value) || 0, v: parseFloat(document.getElementById('c1v').value) || 0 }; var c2 = { x: parseFloat(document.getElementById('c2x').value) || 0, y: parseFloat(document.getElementById('c2y').value) || 0, v: parseFloat(document.getElementById('c2v').value) || 0 }; var c3 = { x: parseFloat(document.getElementById('c3x').value) || 0, y: parseFloat(document.getElementById('c3y').value) || 0, v: parseFloat(document.getElementById('c3v').value) || 0 }; var lines = [ { a: c1.x, b: c1.y, c: c1.v }, { a: c2.x, b: c2.y, c: c2.v }, { a: c3.x, b: c3.y, c: c3.v }, { a: 1, b: 0, c: 0 }, // x = 0 { a: 0, b: 1, c: 0 } // y = 0 ]; var points = []; // Intersection of all lines for (var i = 0; i < lines.length; i++) { for (var j = i + 1; j 0.000001) { var px = (lines[i].c * lines[j].b – lines[j].c * lines[i].b) / det; var py = (lines[i].a * lines[j].c – lines[j].a * lines[i].c) / det; points.push({ x: px, y: py }); } } } var validPoints = []; var epsilon = 0.000001; for (var k = 0; k = -epsilon && p.y >= -epsilon) { var satisfiesAll = true; if (c1.x * p.x + c1.y * p.y > c1.v + epsilon) satisfiesAll = false; if (c2.x * p.x + c2.y * p.y > c2.v + epsilon) satisfiesAll = false; if (c3.x * p.x + c3.y * p.y > c3.v + epsilon) satisfiesAll = false; if (satisfiesAll) { validPoints.push(p); } } } if (validPoints.length === 0) { document.getElementById('lp-result').style.display = 'block'; document.getElementById('lp-output-content').innerHTML = 'No feasible solution found with given constraints.'; return; } var maxZ = -Infinity; var bestX = 0; var bestY = 0; for (var m = 0; m maxZ) { maxZ = currentZ; bestX = validPoints[m].x; bestY = validPoints[m].y; } } // Clean up negative zeros or tiny floating point errors bestX = Math.abs(bestX) < 0.000001 ? 0 : bestX; bestY = Math.abs(bestY) < 0.000001 ? 0 : bestY; var resultDiv = document.getElementById('lp-result'); var resultContent = document.getElementById('lp-output-content'); resultDiv.style.display = 'block'; resultContent.innerHTML = '
Maximum Value (Z): ' + maxZ.toLocaleString(undefined, {maximumFractionDigits: 4}) + '
' + '
' + '
Optimal Variable x: ' + bestX.toLocaleString(undefined, {maximumFractionDigits: 4}) + '
' + '
Optimal Variable y: ' + bestY.toLocaleString(undefined, {maximumFractionDigits: 4}) + '
' + '
' + 'The optimal solution occurs at vertex (' + bestX.toFixed(2) + ', ' + bestY.toFixed(2) + ') of the feasible region boundary.'; }

Leave a Comment