Differential Equation Calculator

.de-calc-container { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; max-width: 800px; margin: 20px auto; padding: 25px; border: 1px solid #ddd; border-radius: 8px; background-color: #fff; color: #333; box-shadow: 0 4px 12px rgba(0,0,0,0.1); } .de-calc-container h2 { color: #2c3e50; text-align: center; margin-bottom: 25px; font-size: 28px; } .de-calc-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 25px; } .de-input-group { display: flex; flex-direction: column; } .de-input-group label { font-weight: 600; margin-bottom: 8px; font-size: 14px; color: #444; } .de-input-group input, .de-input-group select { padding: 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 16px; } .de-full-width { grid-column: span 2; } .de-calc-btn { grid-column: span 2; background-color: #3498db; color: white; border: none; padding: 15px; font-size: 18px; font-weight: bold; border-radius: 4px; cursor: pointer; transition: background 0.3s; } .de-calc-btn:hover { background-color: #2980b9; } #de-result-area { margin-top: 30px; padding: 20px; background-color: #f8f9fa; border-left: 5px solid #3498db; display: none; } .de-result-title { font-size: 20px; font-weight: bold; margin-bottom: 15px; color: #2c3e50; } .de-table-wrapper { overflow-x: auto; margin-top: 15px; } .de-table { width: 100%; border-collapse: collapse; font-size: 14px; } .de-table th, .de-table td { border: 1px solid #ddd; padding: 8px; text-align: center; } .de-table th { background-color: #eee; } .de-article { margin-top: 40px; line-height: 1.6; } .de-article h3 { color: #2c3e50; border-bottom: 2px solid #eee; padding-bottom: 10px; margin-top: 30px; } .de-formula { background: #f1f1f1; padding: 10px; display: block; text-align: center; font-style: italic; margin: 15px 0; border-radius: 4px; }

First-Order Differential Equation Solver

Exponential Growth/Decay (k * y) Newton's Law of Cooling (k * (T – Ta)) Logistic Growth (r * y * (1 – y/K)) Linear Combination (x + y)
Solution Results

Step (n) xn yn f(xn, yn)

What is a Differential Equation?

A differential equation is a mathematical equation that relates a function with its derivatives. In real-world applications, the functions usually represent physical quantities, while the derivatives represent their rates of change. The equation defines the relationship between the two.

This calculator specifically solves First-Order Ordinary Differential Equations (ODEs) using the Euler Method. The general form of these equations is:

dy/dx = f(x, y)

Understanding the Numerical Models

Because many differential equations cannot be solved analytically (with a simple formula), we use numerical methods to approximate the values. This tool provides three common scientific models:

  • Exponential Growth/Decay: Used in biology for population growth or physics for radioactive decay. dy/dx = ky.
  • Newton's Law of Cooling: Describes how the temperature of an object changes relative to the surrounding temperature. dT/dt = k(T – Ta).
  • Logistic Growth: A more realistic population model that includes a "Carrying Capacity" (K), limiting growth as resources deplete. dy/dx = ry(1 – y/K).

The Euler Method Logic

The Euler Method is the most basic numerical procedure for solving ODEs. It uses the slope at a specific point to predict the value of the function at a small distance (h) away. The formula used by this calculator is:

yn+1 = yn + h · f(xn, yn)

Where h is the step size. A smaller step size generally leads to a more accurate approximation but requires more computational steps.

Practical Example: Newton's Law of Cooling

Imagine a cup of coffee at 90°C is placed in a room that is 20°C. If the cooling constant k is -0.1, what will the temperature be after 5 minutes?

  • Initial x (t₀): 0
  • Initial y (T₀): 90
  • Ambient Temp (Ta): 20
  • Target x: 5
  • k: -0.1

Inputting these values into the calculator will simulate the temperature drop step-by-step, showing you the cooling curve numerically.

function solveODE() { var eqType = document.getElementById('eqType').value; var p1 = parseFloat(document.getElementById('param1').value); var p2 = parseFloat(document.getElementById('param2').value); var x0 = parseFloat(document.getElementById('x0').value); var y0 = parseFloat(document.getElementById('y0').value); var targetX = parseFloat(document.getElementById('targetX').value); var h = parseFloat(document.getElementById('h').value); if (isNaN(p1) || isNaN(p2) || isNaN(x0) || isNaN(y0) || isNaN(targetX) || isNaN(h) || h <= 0) { alert("Please enter valid numerical values. Step size (h) must be greater than 0."); return; } var tableBody = document.getElementById('tableBody'); tableBody.innerHTML = ''; var resultArea = document.getElementById('de-result-area'); resultArea.style.display = 'block'; var currentX = x0; var currentY = y0; var steps = 0; var maxSteps = 1000; // Safety limit while ((currentX < targetX) && (steps targetX && (currentX – h) < targetX) { // This is a simple Euler implementation; for target exactness we adjust h for the final step h = targetX – (currentX – h); // The loop will finish on next check } } // Final result row var finalSlope = calculateSlope(eqType, currentX, currentY, p1, p2); var rowEnd = tableBody.insertRow(); rowEnd.insertCell(0).innerHTML = steps; rowEnd.insertCell(1).innerHTML = currentX.toFixed(4); rowEnd.insertCell(2).innerHTML = currentY.toFixed(4); rowEnd.insertCell(3).innerHTML = finalSlope.toFixed(4); document.getElementById('finalResultText').innerHTML = "Estimated value of y at x = " + targetX + " is " + currentY.toFixed(6) + " (Calculated in " + steps + " steps)."; } function calculateSlope(type, x, y, p1, p2) { if (type === 'growth') { return p1 * y; // dy/dx = k * y } else if (type === 'cooling') { return p1 * (y – p2); // dy/dx = k * (T – Ta) } else if (type === 'logistic') { return p1 * y * (1 – (y / p2)); // dy/dx = r * y * (1 – y/K) } else if (type === 'linear') { return x + y; // dy/dx = x + y } return 0; } // Dynamic Label Updates document.getElementById('eqType').onchange = function() { var val = this.value; var l1 = document.getElementById('param1Label'); var l2 = document.getElementById('param2Label'); var p2Input = document.getElementById('param2'); if (val === 'growth') { l1.innerText = "Rate Constant (k)"; l2.innerText = "N/A (Not Used)"; p2Input.disabled = true; } else if (val === 'cooling') { l1.innerText = "Cooling Constant (k)"; l2.innerText = "Ambient Temperature (Ta)"; p2Input.disabled = false; } else if (val === 'logistic') { l1.innerText = "Growth Rate (r)"; l2.innerText = "Carrying Capacity (K)"; p2Input.disabled = false; } else { l1.innerText = "N/A (Not Used)"; l2.innerText = "N/A (Not Used)"; p2Input.disabled = true; } };

Leave a Comment