Absolutely! Here is a complete HTML example that demonstrates Euler's method. This implementation includes a <canvas> for visual output, various UI elements for user interaction, and it avoids any potential infinite loops. I've ensured to follow your specifications closely.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
background-color: #f0f0f0;
}
.eulerSimulation {
margin: 20px;
border: 1px solid #ccc;
padding: 20px;
background-color: white;
border-radius: 8px;
}
canvas {
border: 1px solid #000;
margin-bottom: 10px;
}
.UI {
margin: 10px 0;
display: flex;
flex-direction: column;
}
.UI label {
margin-bottom: 5px;
}
.UI button {
margin-top: 10px;
}
</style>
</head>
<body>
<div class="eulerSimulation">
<canvas id="maybeSVG" width="400" height="400"></canvas>
<div class="UI">
<label for="stepSize">Step Size:</label>
<input type="number" id="stepSize" value="0.1" step="0.01" min="0.01">
<label for="iterations">Iterations:</label>
<input type="number" id="iterations" value="100" min="1">
<label for="function">Function (f(x,y):</label>
<input type="text" id="function" value="x + y">
<label for="initialY">Initial Y:</label>
<input type="number" id="initialY" value="1" step="0.1">
<button id="runSimulation">Run Simulation</button>
</div>
<div class="info" id="formulation">
<strong>Euler's Method:</strong> Approximates solutions of differential equations using the formula:
<code>y[n+1] = y[n] + h * f(x[n], y[n])</code>, where <code>h</code> is the step size.
</div>
<div class="info" id="detailedExplanation">
<strong>Detailed Explanation:</strong>
Euler's method computes an approximate solution of ordinary differential equations given an initial condition. By iterating values based on the defined function, the solution can be visualized.
</div>
</div>
<script>
const canvas = document.getElementById('maybeSVG');
const ctx = canvas.getContext('2d');
const stepSizeInput = document.getElementById('stepSize');
const iterationsInput = document.getElementById('iterations');
const functionInput = document.getElementById('function');
const initialYInput = document.getElementById('initialY');
const runButton = document.getElementById('runSimulation');
runButton.addEventListener('click', () => {
const stepSize = parseFloat(stepSizeInput.value);
const iterations = parseInt(iterationsInput.value);
const initialY = parseFloat(initialYInput.value);
if (isNaN(stepSize) || isNaN(iterations) || isNaN(initialY) || stepSize <= 0 || iterations < 1) {
alert('Please enter valid numerical values.');
return; // Prevent execution if inputs are invalid
}
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear previous drawings
drawEuler(stepSize, iterations, initialY);
});
function drawEuler(stepSize, iterations, initialY) {
const f = new Function('x', 'y', 'return ' + functionInput.value); // Create function from input
let x = 0;
let y = initialY;
const scale = 20; // Scale factor for drawing
ctx.beginPath();
ctx.moveTo(x * scale, canvas.height - y * scale); // Starting point
for (let i = 0; i < iterations; i++) {
y += stepSize * f(x, y); // Calculate the next value of y
x += stepSize; // Increment x
ctx.lineTo(x * scale, canvas.height - y * scale); // Draw to the next point
}
ctx.stroke();
}
</script>
</body>
</html>
Breakdown of the Code:
-
HTML Structure:
- The main container is a
<div> holding a <canvas> for visual representation and various UI elements allowing user interaction.
- Inputs for step size, number of iterations, differential function, and initial
y value are included for manipulation.
-
CSS:
- Simple styling is applied to maintain visual clarity and structure without any font specifications or headings.
-
JavaScript Logic:
- Event Listener: Triggered when the "Run Simulation" button is clicked. It validates the inputs to ensure they are valid numerical values.
- Function Creation: The user-defined function is evaluated dynamically using the
Function constructor to create a function from the input string, which allows flexible user input.
- Drawing Logic: The Euler's method is employed in a
for loop that iterates a specified number of times, calculating the new y values and drawing on the canvas without any infinite loops. The program only proceeds if the loop condition is met.
User Interaction:
- Users can manipulate the function definition (e.g., input
x + y), set the step size, the number of iterations, and the initial y value. This makes the simulation interactive and adaptable.
This implementation respects your requirements and ensures clarity and functionality while demonstrating Euler's method visually.