Equilibrium Calculator
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f8f9fa;
color: #333;
line-height: 1.6;
margin: 0;
padding: 20px;
display: flex;
justify-content: center;
align-items: flex-start;
min-height: 100vh;
}
.loan-calc-container {
background-color: #ffffff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
max-width: 700px;
width: 100%;
margin-top: 20px;
}
h1, h2 {
color: #004a99;
text-align: center;
margin-bottom: 20px;
}
.input-group {
margin-bottom: 20px;
display: flex;
flex-direction: column;
}
.input-group label {
display: block;
margin-bottom: 8px;
font-weight: 500;
color: #555;
}
.input-group input[type="number"] {
padding: 10px 12px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1rem;
width: calc(100% – 24px); /* Adjust for padding */
box-sizing: border-box;
}
.input-group input[type="number"]:focus {
border-color: #004a99;
outline: none;
box-shadow: 0 0 0 2px rgba(0, 74, 153, 0.2);
}
button {
background-color: #004a99;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
font-size: 1.1rem;
cursor: pointer;
transition: background-color 0.3s ease;
width: 100%;
margin-top: 10px;
}
button:hover {
background-color: #003b7f;
}
#result {
margin-top: 30px;
padding: 20px;
background-color: #e9ecef;
border-radius: 8px;
text-align: center;
border: 1px solid #dee2e6;
}
#result h3 {
margin-top: 0;
color: #004a99;
font-size: 1.5rem;
}
#equilibrium-point {
font-size: 2.5rem;
font-weight: bold;
color: #28a745;
display: block;
margin-top: 10px;
}
.article-section {
margin-top: 40px;
background-color: #ffffff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}
.article-section h2 {
text-align: left;
color: #004a99;
margin-bottom: 15px;
}
.article-section p {
margin-bottom: 15px;
}
.article-section ul {
margin-left: 20px;
margin-bottom: 15px;
}
.article-section li {
margin-bottom: 8px;
}
.article-section strong {
color: #004a99;
}
@media (max-width: 600px) {
.loan-calc-container {
padding: 20px;
}
h1 {
font-size: 1.8rem;
}
button {
font-size: 1rem;
}
#result h3 {
font-size: 1.3rem;
}
#equilibrium-point {
font-size: 2rem;
}
}
Understanding Economic Equilibrium
Economic equilibrium is a fundamental concept that describes a state where opposing economic forces, such as supply and demand, are balanced. In this state, there is no tendency for change. For a market, equilibrium occurs at the price and quantity where the quantity demanded by consumers equals the quantity supplied by producers. This intersection point is crucial for understanding market dynamics, price determination, and resource allocation.
The Law of Supply and Demand
The equilibrium point is determined by the interaction of two key curves:
- Demand Curve: This curve illustrates the relationship between the price of a good or service and the quantity consumers are willing and able to buy at various prices, holding other factors constant. Generally, as the price decreases, the quantity demanded increases (the law of demand).
- Supply Curve: This curve shows the relationship between the price of a good or service and the quantity producers are willing and able to sell at various prices, holding other factors constant. Typically, as the price increases, the quantity supplied increases (the law of supply).
Calculating Equilibrium
To find the equilibrium, we need to solve for the price (P) and quantity (Q) where the quantity demanded (Qd) equals the quantity supplied (Qs). This is achieved by setting the demand equation equal to the supply equation.
If your demand equation is represented as $P = f(Q_d)$ and your supply equation is represented as $P = g(Q_s)$, then at equilibrium ($P_e, Q_e$):
$Q_d = Q_s$
And
$P_e = f(Q_e) = g(Q_e)$
The calculator above simplifies this by assuming the input equations are in the form of Price as a function of Quantity (P = …). It then sets these two expressions for P equal to each other to solve for the equilibrium quantity (Qe), and subsequently substitutes this Qe back into either the demand or supply equation to find the equilibrium price (Pe).
Example Calculation:
Let's consider the following equations:
- Demand: $P = 100 – 2Q_d$
- Supply: $P = 10 + 3Q_s$
To find the equilibrium quantity ($Q_e$), we set the two equations equal:
$100 – 2Q_e = 10 + 3Q_e$
Add $2Q_e$ to both sides:
$100 = 10 + 5Q_e$
Subtract 10 from both sides:
$90 = 5Q_e$
Divide by 5:
$Q_e = 18$
Now, substitute $Q_e = 18$ into either equation to find the equilibrium price ($P_e$):
Using the demand equation: $P_e = 100 – 2(18) = 100 – 36 = 64$
Using the supply equation: $P_e = 10 + 3(18) = 10 + 54 = 64$
So, the equilibrium point is a price of 64 and a quantity of 18.
Use Cases
Understanding equilibrium is vital in economics and business for:
- Price Setting: Businesses use equilibrium principles to understand optimal pricing strategies.
- Market Analysis: Economists use it to predict how changes in supply or demand (due to factors like new technology, taxes, or consumer preferences) will affect market prices and quantities.
- Policy Decisions: Governments consider equilibrium when implementing policies like price controls or subsidies.
- Forecasting: It helps in forecasting future market conditions.
function calculateEquilibrium() {
var demandEqText = document.getElementById("demandEquation").value.trim();
var supplyEqText = document.getElementById("supplyEquation").value.trim();
var resultDisplay = document.getElementById("equilibrium-point");
var errorMessage = document.getElementById("error-message");
errorMessage.style.display = 'none'; // Hide previous error
resultDisplay.textContent = "—"; // Reset result
// — Equation Parsing and Solving —
// This is a simplified parser. It expects linear equations in the form P = mQ + c or P = c – mQ
// It will attempt to extract coefficients for P and Q.
var demandCoeffs = parseLinearEquation(demandEqText, 'demand');
var supplyCoeffs = parseLinearEquation(supplyEqText, 'supply');
if (!demandCoeffs || !supplyCoeffs) {
errorMessage.textContent = "Could not parse one or both equations. Please ensure they are in a valid format (e.g., P = 100 – 2Q or P = 10 + 3Q).";
errorMessage.style.display = 'block';
return;
}
// Now we have:
// demandCoeffs = { P_coeff: 1, Q_coeff: -2, constant: 100 } (for P = 100 – 2Q)
// supplyCoeffs = { P_coeff: 1, Q_coeff: 3, constant: 10 } (for P = 10 + 3Q)
// At equilibrium, P_demand = P_supply and Q_demand = Q_supply.
// P = demandCoeffs.P_coeff * Q + demandCoeffs.constant
// P = supplyCoeffs.P_coeff * Q + supplyCoeffs.constant
// Set them equal:
// demandCoeffs.P_coeff * Q + demandCoeffs.constant = supplyCoeffs.P_coeff * Q + supplyCoeffs.constant
// Rearrange to solve for Q:
// demandCoeffs.constant – supplyCoeffs.constant = (supplyCoeffs.P_coeff – demandCoeffs.P_coeff) * Q
// Q = (demandCoeffs.constant – supplyCoeffs.constant) / (supplyCoeffs.P_coeff – demandCoeffs.P_coeff)
var qNumerator = demandCoeffs.constant – supplyCoeffs.constant;
var qDenominator = supplyCoeffs.P_coeff – demandCoeffs.P_coeff;
// Check for parallel lines (denominator is zero) or invalid inputs
if (qDenominator === 0) {
errorMessage.textContent = "The supply and demand curves are parallel or identical. No unique equilibrium point exists.";
errorMessage.style.display = 'block';
return;
}
if (isNaN(qNumerator) || isNaN(qDenominator)) {
errorMessage.textContent = "Invalid coefficients derived from equations.";
errorMessage.style.display = 'block';
return;
}
var equilibriumQuantity = qNumerator / qDenominator;
// Substitute equilibrium quantity back into either equation to find equilibrium price
// Using demand equation: P = mQ + c
var equilibriumPrice = demandCoeffs.P_coeff * equilibriumQuantity + demandCoeffs.constant;
// Basic validation for resulting values
if (isNaN(equilibriumQuantity) || isNaN(equilibriumPrice) || equilibriumQuantity < 0 || equilibriumPrice < 0) {
errorMessage.textContent = "Calculated equilibrium values are invalid (e.g., negative price or quantity). Ensure your equations represent realistic market conditions.";
errorMessage.style.display = 'block';
return;
}
resultDisplay.textContent = "Price (P) = " + equilibriumPrice.toFixed(2) + ", Quantity (Q) = " + equilibriumQuantity.toFixed(2);
}
// Helper function to parse linear equations like "P = 100 – 2Q" or "P = 10 + 3Q"
// It tries to extract the coefficient of Q and the constant term, assuming P is isolated.
function parseLinearEquation(equationString, type) {
var cleanedEquation = equationString.toLowerCase().replace(/\s+/g, ''); // Remove whitespace
var pIndex = cleanedEquation.indexOf('p=');
if (pIndex === -1) return null; // 'P=' not found
var expression = cleanedEquation.substring(pIndex + 2); // Get the part after 'P='
var qIndex = expression.indexOf('q');
var qCoeff = 0;
var constantTerm = 0;
var pCoeff = 1; // We assume P is isolated on one side, so its coefficient is 1.
if (qIndex !== -1) {
// Extract Q coefficient
var coeffStr = expression.substring(0, qIndex);
if (coeffStr === '' || coeffStr === '+') {
qCoeff = 1;
} else if (coeffStr === '-') {
qCoeff = -1;
} else {
// Try to parse the number
var parsedQCoeff = parseFloat(coeffStr);
if (!isNaN(parsedQCoeff)) {
qCoeff = parsedQCoeff;
} else {
return null; // Invalid coefficient format
}
}
// Extract constant term
var constantStr = expression.substring(qIndex + 1);
if (constantStr === '') {
constantTerm = 0; // e.g., P = 2Q
} else {
// Try to parse the number, it should start with '+' or '-'
var parsedConstant = parseFloat(constantStr);
if (!isNaN(parsedConstant)) {
constantTerm = parsedConstant;
} else {
return null; // Invalid constant format
}
}
} else {
// No 'Q' term found, it's a horizontal line P = constant
var parsedConstant = parseFloat(expression);
if (!isNaN(parsedConstant)) {
constantTerm = parsedConstant;
qCoeff = 0; // Coefficient of Q is zero
} else {
return null; // Invalid constant format
}
}
// Handle negative signs correctly for Q coefficient, e.g., P = 100 – 2Q
// The parsing logic above needs to correctly identify the sign _before_ the number.
// Let's refine the extraction:
var match = expression.match(/([+-]?\d*\.?\d*)q?([+-]\d+\.?\d*|[-+]?\d*\.?\d*)$/);
if (match) {
var qPart = match[0]; // e.g., -2q or +3q or q or -q
var constantPart = match[1] || ''; // e.g., 100 or 10
// Extract Q coefficient
if (qPart.includes('q')) {
var qCoeffStr = qPart.replace('q', '');
if (qCoeffStr === '' || qCoeffStr === '+') qCoeff = 1;
else if (qCoeffStr === '-') qCoeff = -1;
else qCoeff = parseFloat(qCoeffStr);
} else { // Case like P = 100 (no Q term explicitly)
qCoeff = 0;
constantPart = qPart; // The whole thing is the constant
}
// Extract constant term
if (constantPart !== '') {
constantTerm = parseFloat(constantPart);
} else {
constantTerm = 0; // Should not happen if expression is valid
}
// Ensure P coefficient is 1
pCoeff = 1;
} else {
// Try a simpler regex for cases like P = c
var simpleMatch = expression.match(/^([+-]?\d*\.?\d+)$/);
if (simpleMatch) {
pCoeff = 1;
qCoeff = 0;
constantTerm = parseFloat(simpleMatch[0]);
} else {
// A more robust regex might be needed for complex expressions, but let's stick to simple linear forms.
// Re-evaluate based on common formats: P = 100 – 2Q
var parts = expression.split(/([+-])(?=\d|\.)/); // Split by +/- signs that are followed by a digit or decimal
var terms = [];
var currentTerm = '';
for (var i = 0; i < expression.length; i++) {
if (expression[i] === '+' || expression[i] === '-') {
if (currentTerm !== '') {
terms.push(currentTerm);
}
currentTerm = expression[i];
} else {
currentTerm += expression[i];
}
}
if (currentTerm !== '') {
terms.push(currentTerm);
}
qCoeff = 0;
constantTerm = 0;
for (var j = 0; j < terms.length; j++) {
var term = terms[j].trim();
if (term.includes('q')) {
var qTermCoeffStr = term.replace('q', '');
if (qTermCoeffStr === '' || qTermCoeffStr === '+') qCoeff = 1;
else if (qTermCoeffStr === '-') qCoeff = -1;
else qCoeff = parseFloat(qTermCoeffStr);
} else {
constantTerm += parseFloat(term);
}
}
}
}
// Final sanity check for parsed values
if (isNaN(qCoeff) || isNaN(constantTerm)) {
console.error("Parsing failed for:", equationString, "Parsed:", {p: pCoeff, q: qCoeff, c: constantTerm});
return null;
}
return { P_coeff: pCoeff, Q_coeff: qCoeff, constant: constantTerm };
}