Use standard JS math: Math.exp(x), Math.pow(x,2), Math.sin(x), etc.
What is a Differential Equation?
A differential equation is a mathematical equation that relates a function with its derivatives. In applied mathematics and physics, these equations represent the relationship between a quantity and its rate of change. For example, the velocity of an object is the derivative of its position with respect to time.
Solving ODEs Numerically via Euler's Method
While some simple differential equations can be solved analytically (finding an exact formula), many real-world equations are too complex for that. This is where numerical methods come in. Euler's Method is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial value.
The logic follows a simple iterative process:
Start at the point (x₀, y₀).
Calculate the slope (m) at that point using the function f(x, y).
Take a small step (h) in the direction of that slope to find the next point: yn+1 = yn + h * f(xn, yn)
The accuracy of Euler's method depends heavily on the Step Size (h). A smaller step size generally leads to a more accurate approximation but requires more computational steps. For extremely precise needs, higher-order methods like Runge-Kutta (RK4) are typically used, but Euler's method provides an excellent foundation for understanding numerical integration.
function solveODE() {
var funcStr = document.getElementById('odeFunction').value;
var x0 = parseFloat(document.getElementById('initialX').value);
var y0 = parseFloat(document.getElementById('initialY').value);
var targetX = parseFloat(document.getElementById('targetX').value);
var h = parseFloat(document.getElementById('stepSize').value);
var resultBox = document.getElementById('ode-result-box');
var summaryDiv = document.getElementById('ode-summary');
var tableContainer = document.getElementById('ode-table-container');
if (isNaN(x0) || isNaN(y0) || isNaN(targetX) || isNaN(h) || h <= 0) {
alert("Please enter valid numerical values. Step size must be greater than 0.");
return;
}
if (targetX < x0) {
alert("Target X must be greater than Initial X for this solver.");
return;
}
try {
// Clean the function string for simple security/syntax
var f = new Function('x', 'y', 'return ' + funcStr);
var currentX = x0;
var currentY = y0;
var results = [];
results.push({x: currentX.toFixed(4), y: currentY.toFixed(6)});
// Limiting iterations to prevent browser freeze
var iterations = 0;
var maxIterations = 2000;
while (currentX < targetX && iterations targetX) currentX = targetX;
results.push({x: currentX.toFixed(4), y: currentY.toFixed(6)});
iterations++;
}
// Build UI
resultBox.style.display = 'block';
summaryDiv.innerHTML = "Final Result: y(" + targetX + ") ≈ " + currentY.toFixed(6) + "";
var tableHtml = "
Step (n)
x
y (Approx)
";
for (var i = 0; i < results.length; i++) {
tableHtml += "
" + i + "
" + results[i].x + "
" + results[i].y + "
";
// Cap table display for performance
if (i > 100) {
tableHtml += "
… (only first 100 steps shown) …
";
break;
}
}
tableHtml += "
";
tableContainer.innerHTML = tableHtml;
} catch (e) {
alert("Error in calculation or formula: " + e);
}
}