Price Equilibrium Calculator

Price Equilibrium Calculator & Guide | Understand Market Balance :root { –primary-color: #004a99; –success-color: #28a745; –background-color: #f8f9fa; –text-color: #333; –border-color: #ddd; –card-background: #fff; –shadow: 0 2px 5px rgba(0,0,0,0.1); } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: var(–background-color); color: var(–text-color); line-height: 1.6; margin: 0; padding: 0; } .container { max-width: 960px; margin: 20px auto; padding: 20px; background-color: var(–card-background); border-radius: 8px; box-shadow: var(–shadow); } header { background-color: var(–primary-color); color: white; padding: 20px 0; text-align: center; margin-bottom: 20px; border-radius: 8px 8px 0 0; } header h1 { margin: 0; font-size: 2.5em; } h1, h2, h3 { color: var(–primary-color); } h2 { border-bottom: 2px solid var(–primary-color); padding-bottom: 5px; margin-top: 30px; } .loan-calc-container { background-color: var(–card-background); padding: 25px; border-radius: 8px; box-shadow: var(–shadow); margin-bottom: 30px; } .input-group { margin-bottom: 20px; text-align: left; } .input-group label { display: block; margin-bottom: 8px; font-weight: bold; color: var(–primary-color); } .input-group input[type="number"], .input-group select { width: calc(100% – 22px); padding: 10px; border: 1px solid var(–border-color); border-radius: 4px; font-size: 1em; box-sizing: border-box; } .input-group .helper-text { font-size: 0.85em; color: #666; margin-top: 5px; display: block; } .error-message { color: red; font-size: 0.8em; margin-top: 5px; display: none; /* Hidden by default */ } .button-group { display: flex; justify-content: space-between; margin-top: 25px; gap: 10px; } button { padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; font-size: 1em; transition: background-color 0.3s ease; flex: 1; } button.primary { background-color: var(–primary-color); color: white; } button.primary:hover { background-color: #003366; } button.success { background-color: var(–success-color); color: white; } button.success:hover { background-color: #218838; } button.secondary { background-color: #6c757d; color: white; } button.secondary:hover { background-color: #5a6268; } #results { margin-top: 30px; padding: 20px; background-color: var(–primary-color); color: white; border-radius: 8px; text-align: center; box-shadow: var(–shadow); } #results h3 { color: white; margin-top: 0; } .result-item { margin-bottom: 15px; } .result-item strong { display: block; font-size: 1.2em; margin-bottom: 5px; } .result-item span { font-size: 1.8em; font-weight: bold; } .intermediate-results { display: flex; justify-content: space-around; flex-wrap: wrap; margin-top: 20px; gap: 15px; } .intermediate-result-card { background-color: rgba(255, 255, 255, 0.15); padding: 15px; border-radius: 6px; text-align: center; flex: 1; min-width: 150px; } .intermediate-result-card strong { font-size: 1.1em; display: block; margin-bottom: 5px; } .intermediate-result-card span { font-size: 1.4em; font-weight: bold; } .formula-explanation { margin-top: 20px; font-size: 0.9em; color: #555; text-align: center; } table { width: 100%; border-collapse: collapse; margin-top: 20px; box-shadow: var(–shadow); } th, td { padding: 12px; text-align: left; border-bottom: 1px solid var(–border-color); } thead { background-color: var(–primary-color); color: white; } tbody tr:nth-child(even) { background-color: #f2f2f2; } caption { font-size: 1.1em; font-weight: bold; margin-bottom: 10px; color: var(–primary-color); text-align: left; } canvas { display: block; margin: 20px auto; max-width: 100%; border: 1px solid var(–border-color); border-radius: 4px; } .article-content { margin-top: 40px; background-color: var(–card-background); padding: 30px; border-radius: 8px; box-shadow: var(–shadow); } .article-content h2 { margin-top: 40px; color: var(–primary-color); border-bottom: 2px solid var(–primary-color); padding-bottom: 5px; } .article-content h3 { margin-top: 25px; color: var(–primary-color); } .article-content p, .article-content ul, .article-content ol { margin-bottom: 15px; } .article-content ul, .article-content ol { padding-left: 25px; } .article-content li { margin-bottom: 8px; } .faq-item { margin-bottom: 15px; } .faq-item strong { display: block; color: var(–primary-color); margin-bottom: 5px; } .internal-links { margin-top: 30px; padding: 20px; background-color: #e9ecef; border-radius: 8px; } .internal-links h3 { margin-top: 0; color: var(–primary-color); } .internal-links ul { list-style: none; padding: 0; } .internal-links li { margin-bottom: 10px; } .internal-links a { color: var(–primary-color); text-decoration: none; font-weight: bold; } .internal-links a:hover { text-decoration: underline; } .internal-links p { font-size: 0.9em; color: #555; margin-top: 5px; } .highlighted-result { font-size: 2.5em; font-weight: bold; color: var(–success-color); display: block; margin-top: 10px; } .copy-button { background-color: #6c757d; color: white; padding: 10px 15px; border-radius: 4px; cursor: pointer; font-size: 0.9em; margin-top: 15px; transition: background-color 0.3s ease; } .copy-button:hover { background-color: #5a6268; } .tooltip { position: relative; display: inline-block; border-bottom: 1px dotted black; cursor: help; } .tooltip .tooltiptext { visibility: hidden; width: 220px; background-color: #555; color: #fff; text-align: center; border-radius: 6px; padding: 5px 0; position: absolute; z-index: 1; bottom: 125%; left: 50%; margin-left: -110px; opacity: 0; transition: opacity 0.3s; font-size: 0.8em; line-height: 1.4; } .tooltip .tooltiptext::after { content: ""; position: absolute; top: 100%; left: 50%; margin-left: -5px; border-width: 5px; border-style: solid; border-color: #555 transparent transparent transparent; } .tooltip:hover .tooltiptext { visibility: visible; opacity: 1; }

Price Equilibrium Calculator

Find the market-clearing price and quantity

Market Equilibrium Inputs

Enter the parameters for your supply and demand curves to find the equilibrium point.

The maximum price consumers are willing to pay when quantity is 0.
How much price changes for each unit change in quantity (typically positive for inverse relationship).
The minimum price producers are willing to accept when quantity is 0.
How much price changes for each unit change in quantity (typically positive).

Market Equilibrium Results

Equilibrium Price (P*)
Equilibrium Quantity (Q*)
Demand at P*
Supply at P*
Demand Intercept (a)
Supply Intercept (c)
Equilibrium occurs where Quantity Demanded (Qd) equals Quantity Supplied (Qs). Demand: Qd = a – bP Supply: Qs = c + dP At equilibrium: a – bP* = c + dP* => P* = (a – c) / (b + d) Then, Q* = c + dP* (or a – bP*)

Equilibrium Analysis Table

This chart visualizes the demand and supply curves and their intersection point at equilibrium.

Key Market Parameters
Parameter Value Unit
Demand Intercept (a) Price Units
Demand Slope (b) Price Units / Quantity Units
Supply Intercept (c) Price Units
Supply Slope (d) Price Units / Quantity Units
Equilibrium Price (P*) Price Units
Equilibrium Quantity (Q*) Quantity Units

Understanding Price Equilibrium

What is Price Equilibrium?

Price equilibrium, often referred to as the market equilibrium, is a fundamental economic concept that describes the state where the quantity of a good or service that consumers demand is equal to the quantity that producers supply at a specific price. At this point, the market is considered to be in balance, with no inherent tendency for the price or quantity to change. This equilibrium price is also known as the market-clearing price because, at this level, all goods produced are sold, and all consumers willing to buy at that price can do so.

Who should use it: This concept is crucial for economists, business strategists, policymakers, and students of economics. Businesses use it to understand optimal pricing strategies, forecast sales, and manage inventory. Policymakers might analyze equilibrium shifts to understand the impact of taxes, subsidies, or regulations. Anyone interested in how markets function will find price equilibrium a cornerstone concept.

Common misconceptions: A frequent misunderstanding is that equilibrium is a static, permanent state. In reality, market conditions are dynamic, and equilibrium is constantly shifting due to changes in consumer preferences, production costs, technology, and external factors. Another misconception is that equilibrium always represents a "fair" or "optimal" outcome for all parties; it simply represents a point of balance based on current supply and demand forces.

Price Equilibrium Formula and Mathematical Explanation

The price equilibrium is determined by the intersection of the demand curve and the supply curve. We typically represent these relationships using linear equations:

Demand Equation: Qd = a – bP

Supply Equation: Qs = c + dP

Where:

  • Qd is the quantity demanded
  • Qs is the quantity supplied
  • P is the price
  • 'a' is the demand intercept (the quantity demanded when price is zero, or the maximum price consumers would pay for the first unit)
  • 'b' is the slope of the demand curve (representing how much quantity demanded changes for a one-unit change in price; typically positive in this formulation where P is on the y-axis and Q on the x-axis, but often represented as negative if Q is a function of P, i.e., Q = a – bP, where b is positive)
  • 'c' is the supply intercept (the quantity supplied when price is zero, or the minimum price producers would accept for the first unit)
  • 'd' is the slope of the supply curve (representing how much quantity supplied changes for a one-unit change in price; typically positive)

At the point of equilibrium (P*, Q*), the quantity demanded equals the quantity supplied:

Qd = Qs

Substituting the equations:

a – bP* = c + dP*

To solve for the equilibrium price (P*), we rearrange the equation:

a – c = bP* + dP*

a – c = P*(b + d)

Equilibrium Price (P*): P* = (a – c) / (b + d)

Once the equilibrium price (P*) is found, we can substitute it back into either the demand or supply equation to find the equilibrium quantity (Q*). Using the supply equation:

Equilibrium Quantity (Q*): Q* = c + dP*

Alternatively, using the demand equation:

Q* = a – bP*

Both methods should yield the same Q* if the calculations are correct.

Variables Table

Price Equilibrium Variables
Variable Meaning Unit Typical Range
a (Demand Intercept) Maximum price consumers are willing to pay or quantity demanded at zero price. Price Units (e.g., USD, EUR) Non-negative
b (Demand Slope) Rate of change in quantity demanded per unit change in price. Quantity Units / Price Units Positive (in Qd = a – bP form)
c (Supply Intercept) Minimum price producers will accept or quantity supplied at zero price. Price Units (e.g., USD, EUR) Non-negative
d (Supply Slope) Rate of change in quantity supplied per unit change in price. Quantity Units / Price Units Positive
P* (Equilibrium Price) The price at which quantity demanded equals quantity supplied. Price Units (e.g., USD, EUR) Non-negative
Q* (Equilibrium Quantity) The quantity bought and sold at the equilibrium price. Quantity Units (e.g., units, kg, liters) Non-negative

Practical Examples (Real-World Use Cases)

Example 1: Smartphone Market

Consider the market for a new smartphone model. Based on market research:

  • The demand equation is estimated as: Qd = 500,000 – 10,000P (where P is price in USD)
  • The supply equation is estimated as: Qs = 50,000 + 5,000P

Here, a = 500,000, b = 10,000, c = 50,000, and d = 5,000.

Calculation:

P* = (a – c) / (b + d) = (500,000 – 50,000) / (10,000 + 5,000) = 450,000 / 15,000 = $30

Q* = c + dP* = 50,000 + 5,000 * 30 = 50,000 + 150,000 = 200,000 units

Interpretation: The equilibrium price for this smartphone is $30, and at this price, 200,000 units will be demanded and supplied. If the price were set higher, say $40, quantity supplied would exceed quantity demanded, leading to a surplus. If set lower, say $20, quantity demanded would exceed quantity supplied, leading to a shortage.

Example 2: Organic Coffee Beans

Let's analyze the market for premium organic coffee beans:

  • Demand: Qd = 1,000 – 20P (where P is price in USD per kg)
  • Supply: Qs = 100 + 30P

Here, a = 1,000, b = 20, c = 100, and d = 30.

Calculation:

P* = (a – c) / (b + d) = (1,000 – 100) / (20 + 30) = 900 / 50 = $18

Q* = c + dP* = 100 + 30 * 18 = 100 + 540 = 640 kg

Interpretation: The equilibrium price for these organic coffee beans is $18 per kg. At this price, 640 kg will be traded. This equilibrium reflects the balance between consumer willingness to pay and producer costs and willingness to sell.

How to Use This Price Equilibrium Calculator

Our Price Equilibrium Calculator simplifies the process of finding the market-clearing price and quantity. Follow these steps:

  1. Input Demand Parameters: Enter the 'Demand Curve Intercept (a)' and 'Demand Curve Slope (b)' based on your market analysis or data. The intercept 'a' represents the theoretical demand at zero price, and 'b' reflects how sensitive demand is to price changes.
  2. Input Supply Parameters: Enter the 'Supply Curve Intercept (c)' and 'Supply Curve Slope (d)'. The intercept 'c' represents the theoretical supply at zero price (or minimum viable price), and 'd' reflects how sensitive supply is to price changes.
  3. Calculate: Click the "Calculate Equilibrium" button.

How to read results:

  • Equilibrium Price (P*): This is the market-clearing price.
  • Equilibrium Quantity (Q*): This is the quantity of the good or service that will be bought and sold at P*.
  • Demand at P* / Supply at P*: These values confirm that at the calculated equilibrium price, the quantity demanded equals the quantity supplied, validating the equilibrium point.
  • Table & Chart: The table summarizes all input and output values, while the chart visually represents the intersection of the demand and supply curves.

Decision-making guidance: Understanding the equilibrium helps businesses set prices, forecast production, and anticipate market responses. If the current market price is above equilibrium, expect a surplus and downward pressure on prices. If below, expect a shortage and upward pressure.

Key Factors That Affect Price Equilibrium Results

The equilibrium price and quantity are not static; they are influenced by numerous factors that can shift the demand and supply curves:

  1. Consumer Income: An increase in average income often leads to higher demand for normal goods, shifting the demand curve rightward and increasing both P* and Q*. For inferior goods, demand decreases.
  2. Consumer Tastes and Preferences: Changes in fashion, trends, or perceived value can significantly alter demand. A surge in popularity for a product shifts demand right, raising equilibrium price and quantity.
  3. Prices of Related Goods:
    • Substitutes: If the price of a substitute good (e.g., another brand of smartphone) increases, demand for the original good rises, shifting demand right and increasing P* and Q*.
    • Complements: If the price of a complementary good (e.g., phone cases for smartphones) decreases, demand for the original good increases, shifting demand right and increasing P* and Q*.
  4. Input Costs and Technology: Changes in the cost of raw materials, labor, or energy affect supply. Increased costs shift the supply curve left, raising P* and lowering Q*. Improved technology or efficiency shifts supply right, lowering P* and raising Q*.
  5. Number of Buyers and Sellers: An increase in the number of consumers in the market shifts demand right. An increase in the number of producers shifts supply right. Both scenarios generally lead to a higher equilibrium quantity, but the effect on price depends on the relative shifts.
  6. Expectations: If consumers expect prices to rise in the future, they might increase current demand. If producers expect prices to fall, they might increase current supply to sell before the drop. These expectations can cause immediate shifts in equilibrium.
  7. Government Policies (Taxes, Subsidies, Regulations): Taxes on producers increase costs, shifting supply left (higher P*, lower Q*). Subsidies decrease costs, shifting supply right (lower P*, higher Q*). Price controls (ceilings or floors) can prevent the market from reaching its natural equilibrium, leading to shortages or surpluses.
  8. External Shocks: Events like natural disasters, pandemics, or geopolitical instability can disrupt supply chains, affect production costs, and alter consumer behavior, leading to significant shifts in market equilibrium.

Frequently Asked Questions (FAQ)

Q1: What happens if the price is above the equilibrium price?

If the price is set above the equilibrium price (P*), the quantity supplied (Qs) will exceed the quantity demanded (Qd). This creates a surplus of goods in the market. Sellers will likely lower their prices to clear inventory, pushing the market back towards equilibrium.

Q2: What happens if the price is below the equilibrium price?

If the price is set below the equilibrium price (P*), the quantity demanded (Qd) will exceed the quantity supplied (Qs). This creates a shortage of goods. Consumers may bid up prices, or sellers may realize they can charge more, pushing the market back towards equilibrium.

Q3: Can the equilibrium price and quantity be negative?

In standard economic models, price (P) and quantity (Q) are non-negative. The calculated equilibrium price (P*) and quantity (Q*) should ideally be positive. If the formula yields a negative P*, it often indicates that the demand and supply curves do not intersect in the economically relevant positive quadrant, meaning equilibrium might not be achievable under current conditions or the model is oversimplified.

Q4: What does a steep slope mean for demand or supply?

A steep slope (large absolute value of 'b' for demand, large 'd' for supply) indicates that the quantity is highly sensitive to price changes. For demand, this means consumers are price-elastic; for supply, producers are responsive to price changes. A flatter slope indicates price inelasticity.

Q5: How do taxes affect the equilibrium price and quantity?

A tax levied on producers typically increases their cost of production, shifting the supply curve upwards (or leftwards). This results in a higher equilibrium price paid by consumers and a lower equilibrium quantity traded. The burden of the tax is shared between consumers and producers, depending on the elasticities of demand and supply.

Q6: What is the difference between a shift in demand/supply and a movement along the curve?

A movement along the curve occurs when the price changes, causing a change in quantity demanded or supplied. A shift of the curve occurs when a factor *other than price* changes (like income, input costs, technology), altering the entire relationship between price and quantity at every price level.

Q7: Is the equilibrium price always the "fair" price?

Equilibrium price is the market-clearing price based on current supply and demand forces. It doesn't necessarily reflect fairness or equity. For instance, essential goods might have an equilibrium price that is unaffordable for low-income individuals, leading to discussions about price controls or subsidies.

Q8: How can I improve my understanding of market dynamics?

Continuously monitor market news, analyze economic reports, and practice using tools like this price equilibrium calculator with different scenarios. Understanding related concepts like elasticity, consumer surplus, and producer surplus will also provide a more comprehensive view of market behavior.

var demandInterceptInput = document.getElementById('demandIntercept'); var demandSlopeInput = document.getElementById('demandSlope'); var supplyInterceptInput = document.getElementById('supplyIntercept'); var supplySlopeInput = document.getElementById('supplySlope'); var equilibriumPriceSpan = document.getElementById('equilibriumPrice'); var equilibriumQuantitySpan = document.getElementById('equilibriumQuantity'); var demandAtEquilibriumSpan = document.getElementById('demandAtEquilibrium'); var supplyAtEquilibriumSpan = document.getElementById('supplyAtEquilibrium'); var demandInterceptResultSpan = document.getElementById('demandInterceptResult'); var supplyInterceptResultSpan = document.getElementById('supplyInterceptResult'); var tableDemandIntercept = document.getElementById('tableDemandIntercept'); var tableDemandSlope = document.getElementById('tableDemandSlope'); var tableSupplyIntercept = document.getElementById('tableSupplyIntercept'); var tableSupplySlope = document.getElementById('tableSupplySlope'); var tableEquilibriumPrice = document.getElementById('tableEquilibriumPrice'); var tableEquilibriumQuantity = document.getElementById('tableEquilibriumQuantity'); var chart; var chartContext; function validateInput(inputId, errorId, minValue, maxValue) { var input = document.getElementById(inputId); var errorSpan = document.getElementById(errorId); var value = parseFloat(input.value); errorSpan.style.display = 'none'; input.style.borderColor = '#ccc'; if (isNaN(value)) { errorSpan.textContent = 'Please enter a valid number.'; errorSpan.style.display = 'block'; input.style.borderColor = 'red'; return false; } if (minValue !== undefined && value maxValue) { errorSpan.textContent = 'Value is too high.'; errorSpan.style.display = 'block'; input.style.borderColor = 'red'; return false; } return true; } function calculateEquilibrium() { var validDemandIntercept = validateInput('demandIntercept', 'demandInterceptError', 0); var validDemandSlope = validateInput('demandSlope', 'demandSlopeError', 0); var validSupplyIntercept = validateInput('supplyIntercept', 'supplyInterceptError', 0); var validSupplySlope = validateInput('supplySlope', 'supplySlopeError', 0); if (!validDemandIntercept || !validDemandSlope || !validSupplyIntercept || !validSupplySlope) { resetResults(); return; } var a = parseFloat(demandInterceptInput.value); var b = parseFloat(demandSlopeInput.value); var c = parseFloat(supplyInterceptInput.value); var d = parseFloat(supplySlopeInput.value); var equilibriumPrice = (a – c) / (b + d); var equilibriumQuantity = c + d * equilibriumPrice; // Ensure results are non-negative and sensible if (equilibriumPrice < 0 || equilibriumQuantity < 0 || isNaN(equilibriumPrice) || isNaN(equilibriumQuantity)) { resetResults(); // Display a general error or specific message if needed document.getElementById('result-main').innerHTML = 'Cannot calculate equilibrium with these inputs. Ensure demand intercept is greater than supply intercept and slopes are positive.'; return; } var demandAtEq = a – b * equilibriumPrice; var supplyAtEq = c + d * equilibriumPrice; equilibriumPriceSpan.textContent = equilibriumPrice.toFixed(2); equilibriumQuantitySpan.textContent = equilibriumQuantity.toFixed(2); demandAtEquilibriumSpan.textContent = demandAtEq.toFixed(2); supplyAtEquilibriumSpan.textContent = supplyAtEq.toFixed(2); demandInterceptResultSpan.textContent = a.toFixed(2); supplyInterceptResultSpan.textContent = c.toFixed(2); // Update table tableDemandIntercept.textContent = a.toFixed(2); tableDemandSlope.textContent = b.toFixed(2); tableSupplyIntercept.textContent = c.toFixed(2); tableSupplySlope.textContent = d.toFixed(2); tableEquilibriumPrice.textContent = equilibriumPrice.toFixed(2); tableEquilibriumQuantity.textContent = equilibriumQuantity.toFixed(2); updateChart(a, b, c, d, equilibriumPrice, equilibriumQuantity); } function resetResults() { equilibriumPriceSpan.textContent = '–'; equilibriumQuantitySpan.textContent = '–'; demandAtEquilibriumSpan.textContent = '–'; supplyAtEquilibriumSpan.textContent = '–'; demandInterceptResultSpan.textContent = '–'; supplyInterceptResultSpan.textContent = '–'; tableDemandIntercept.textContent = '–'; tableDemandSlope.textContent = '–'; tableSupplyIntercept.textContent = '–'; tableSupplySlope.textContent = '–'; tableEquilibriumPrice.textContent = '–'; tableEquilibriumQuantity.textContent = '–'; if (chart) { chart.destroy(); chart = null; } document.getElementById('result-main').innerHTML = `
Equilibrium Price (P*)
Equilibrium Quantity (Q*)
`; } function resetCalculator() { demandInterceptInput.value = 100; demandSlopeInput.value = 2; supplyInterceptInput.value = 10; supplySlopeInput.value = 1; // Clear errors document.getElementById('demandInterceptError').textContent = "; document.getElementById('demandSlopeError').textContent = "; document.getElementById('supplyInterceptError').textContent = "; document.getElementById('supplySlopeError').textContent = "; document.getElementById('demandIntercept').style.borderColor = '#ccc'; document.getElementById('demandSlope').style.borderColor = '#ccc'; document.getElementById('supplyIntercept').style.borderColor = '#ccc'; document.getElementById('supplySlope').style.borderColor = '#ccc'; resetResults(); calculateEquilibrium(); // Recalculate with defaults } function copyResults() { var resultsText = "Price Equilibrium Results:\n\n"; resultsText += "Equilibrium Price (P*): " + equilibriumPriceSpan.textContent + "\n"; resultsText += "Equilibrium Quantity (Q*): " + equilibriumQuantitySpan.textContent + "\n"; resultsText += "Demand at P*: " + demandAtEquilibriumSpan.textContent + "\n"; resultsText += "Supply at P*: " + supplyAtEquilibriumSpan.textContent + "\n\n"; resultsText += "Key Assumptions:\n"; resultsText += "Demand Intercept (a): " + demandInterceptResultSpan.textContent + "\n"; resultsText += "Demand Slope (b): " + demandSlopeInput.value + "\n"; resultsText += "Supply Intercept (c): " + supplyInterceptResultSpan.textContent + "\n"; resultsText += "Supply Slope (d): " + supplySlopeInput.value + "\n"; var textArea = document.createElement("textarea"); textArea.value = resultsText; document.body.appendChild(textArea); textArea.select(); try { document.execCommand("copy"); alert("Results copied to clipboard!"); } catch (err) { console.error("Failed to copy results: ", err); alert("Failed to copy results. Please copy manually."); } textArea.remove(); } function updateChart(a, b, c, d, eqP, eqQ) { var canvas = document.getElementById('equilibriumChart'); if (!canvas) return; if (chart) { chart.destroy(); } chartContext = canvas.getContext('2d'); chart = new Chart(chartContext, { type: 'line', data: { labels: [], // Labels will be generated dynamically datasets: [{ label: 'Demand Curve', data: [], borderColor: 'rgba(255, 99, 132, 1)', backgroundColor: 'rgba(255, 99, 132, 0.2)', fill: false, tension: 0, pointRadius: 0 }, { label: 'Supply Curve', data: [], borderColor: 'rgba(54, 162, 235, 1)', backgroundColor: 'rgba(54, 162, 235, 0.2)', fill: false, tension: 0, pointRadius: 0 }, { label: 'Equilibrium Point', data: [], borderColor: 'rgba(75, 192, 192, 1)', backgroundColor: 'rgba(75, 192, 192, 0.2)', fill: false, tension: 0, pointRadius: 6, pointHoverRadius: 8 }] }, options: { responsive: true, maintainAspectRatio: false, scales: { x: { type: 'linear', position: 'bottom', title: { display: true, text: 'Quantity (Q)' }, min: 0, max: Math.max(eqQ * 2, a / b * 1.1, c / d * 1.1) // Adjust max based on equilibrium and intercepts }, y: { title: { display: true, text: 'Price (P)' }, min: 0, max: Math.max(a, c + d * (a/b)) * 1.1 // Adjust max price based on intercepts and slopes } }, plugins: { tooltip: { callbacks: { label: function(context) { var label = context.dataset.label || "; if (label) { label += ': '; } if (context.parsed.x !== null) { label += 'Q=' + context.parsed.x.toFixed(2) + ', P=' + context.parsed.y.toFixed(2); } return label; } } }, legend: { position: 'top', } } } }); // Generate data points for curves var maxQuantity = Math.max(eqQ * 2, (a > 0 ? a / b : 10) * 1.5); // Ensure enough range var priceStep = maxQuantity / 100; for (var i = 0; i = 0) { chart.data.datasets[0].data.push({ x: q, y: demandPrice }); } if (supplyPrice >= 0) { chart.data.datasets[1].data.push({ x: q, y: supplyPrice }); } } // Add equilibrium point chart.data.datasets[2].data.push({ x: eqQ, y: eqP }); // Adjust scale max if needed var maxY = 0; chart.data.datasets.forEach(dataset => { dataset.data.forEach(point => { if (point.y > maxY) maxY = point.y; }); }); if (maxY > 0) { chart.options.scales.y.max = maxY * 1.1; } else { chart.options.scales.y.max = 100; // Default if no points } var maxX = 0; chart.data.datasets.forEach(dataset => { dataset.data.forEach(point => { if (point.x > maxX) maxX = point.x; }); }); if (maxX > 0) { chart.options.scales.x.max = maxX * 1.1; } else { chart.options.scales.x.max = 100; // Default if no points } chart.update(); } // Initial calculation on load document.addEventListener('DOMContentLoaded', function() { calculateEquilibrium(); }); // Chart.js library inclusion (required for the chart) // In a real WordPress setup, you'd enqueue this script properly. // For a single HTML file, we embed it here. var script = document.createElement('script'); script.src = 'https://cdn.jsdelivr.net/npm/chart.js@3.7.0/dist/chart.min.js'; script.onload = function() { console.log('Chart.js loaded'); // Ensure chart is updated after Chart.js is loaded and initial calculation is done calculateEquilibrium(); }; document.head.appendChild(script);

Leave a Comment