About
Ordinary differential equations:
numerical solution
Initial value problem
Comparison of Euler, Heun und Runge−Kutta algorithms
In this simulation the code for the three above mentioned algorithms is demonstrated, as applied to the numerical solution of an explicit, linear ordinary differential equation of first order. Starting from an initial value, a series of consecutive points is calculated and shown in a graphic.
As an example the exponential function is chosen with
differential equation dy/dt = y ´ = y
analytic solution y (t) = (initial value) * et
For this special function the differential equation does not explicitly depend on the variable t, which allows a simple graphical interpretation of the approach.
When opening the file the abscissa range is divided into 5 intervals. The number of intervals can be chosen between 1 and 24 by means of a slider; the interval widths change correspondingly.
The default initial value for x = 0 is y0 = 1 . It can be changed with a second slider. The analytic solution curve (blue) changes with y0 . Each change starts a new calculation of the numerical approximations for the three methods.
The discrete points calculated with Euler are drawn blue, those with Heun in green, those with Runge_Kutta (4 step) in red.
Back resets the number of intervals and initial value.
A second window shows the relative residual difference between the numerical approximations and the analytic solution. Coordinate scales adjust themselves to the mistake of the Euler approach, for which it is largest.
When you open this file from the EJS console you can easily insert other differential equations instead of the very simple one of the exponential. The corresponding places are highlighted in the code pages.
Just a few lines of code are sufficient for the numeric algorithms. They are applied in a loop as often as there are intervals. The end value of one loop is the initial value for the next one.
Parameters that are constant for all methods:
Initial value (for x = 0)
Range of Variables
Number of intervals n
Width of intervals delta = Range/(n-1)
Variables x , y, first derivative (derivative)
specified for the methods by subscript, as xE, xH, xRK
Runge−Kutta uses intermediate values of the first derivative, which are specified by subscripts a und b (derivative_a, derivative_b)
Loops have an index i that runs from 0 to n, as xE[i]
F[i] are deviations of the numerical results from the analytic ones.
Euler
You will find this code in the EJS console at page Initialization.
lines marked // include remarks that do not influence the calculation.
xE[0]=0;
yE[0]=valueFrom;
derivativeE[0]=yE[0];//for exponential
for (var i=1; i<n; i++) {//loop definitionn)
xE[i]=xE[i-1]+delta;
yE[i]=yE[i-1]+delta*derivativeE[i-1];
derivativeE[i]=yE[i];//for exponential
//back to start of loop
FE[i]=(valueFrom*Math.exp(xE[i])-yE[i])/(valueFrom*Math.exp(xE[i]));
}
Heun
xH[0]=0;
yH[0]=valueFrom;
derivativeH_a[0]=yH[0];//for exponential
for (var i=1; i<n; i++) {//loop definition)
xH[i]=xH[i-1]+delta;
yH_a[i]=yH[i-1]+delta*derivativeH_a[i-1];
derivativeH_b[i]=yH_a[i];////for exponential
yH[i]=yH[i-1]+delta/2*(derivativeH_a[i-1]+derivativeH_b[i]);
derivativeH_a[i]=yH[i];////for exponential
//back to loop
FH[i]=(valueFrom*Math.exp(xH[i])-yH[i])/(valueFrom*Math.exp(xH[i]));
}
Runge-Kutta 4 steps
You will find this code in the EJS console at page Initialization
lines marked // include remarks that do not influence the calculation.
xRK[0]=0;
yRK[0]=valueFrom;
derivativeRK[0]=yRK[0];//for exponential
for (var i=1; i<n; i++) { //loop definition)
y_a[i]=yRK[i-1]+delta/2*derivativeRK[i-1];
derivative_a[i]=y_a[i];//for exponential
y_b[i]=yRK[i-1]+delta/2*derivative_a[i];
derivative_b[i]=y_b[i];//for exponential
y_c[i]=yRK[i-1]+delta*derivative_b[i];
derivative_c[i]=y_c[i];//for exponential
yRK[i]=yRK[i-1]+delta/6*(derivativeRK[i-1]+2*derivative_a[i]+2*derivative_b[i]+derivative_c[i]);
xRK[i]=xRK[i-1]+delta;
derivativeRK[i]=yRK[i];//for exponential
FRK[i]=(valueFrom*Math.exp(xRK[i])-yRK[i])/(valueFrom*Math.exp(xRK[i]));
//back to start of loop
}
E 1: Choose the number of intervals as n = 5. Visually compare the quality of the three numerical approximations. Use the right window with its zoomed scale for that purpose, too.
E 2: Change the number of intervals n and compare qualitatively how the three approximations approach the analytic solution.
E 3: Make notes of the relative mistakes of the three methods with increasing n and draw a graph of their dependences. Compare this graph with a straight line for Euler and with a second grade parabola for Heun (with Runge−Kutta the deviations will be too small to clearly recognize the graph as a fourth grade parabola).
E 4: Open the file from the EJS console and study the code at page Initialization. Use other derivatives to solve different differential equations (for the exponential the derivative was simply y). The correct places are marked in red.
created by Dieter Roess in March 2009
This simulation is part of
“Learning and Teaching Mathematics using Simulations
– Plus 2000 Examples from Physics”
ISBN 978-3-11-025005-3, Walter de Gruyter GmbH & Co. KG
Translations
Code | Language | Translator | Run | |
---|---|---|---|---|
![]() |
Credits
Dieter Roess - WEH- Foundation; Tan Wei Chiong; Loo Kang Wee
Briefing Document: Differential Equation Methods JavaScript Simulation Applet
1. Overview:
This document details a JavaScript HTML5 applet simulation designed to visually demonstrate and compare different numerical methods for solving ordinary differential equations (ODEs). The simulation is part of the Open Educational Resources / Open Source Physics @ Singapore project. It allows users to explore the Euler, Heun (Improved Euler), and Runge-Kutta (4th order, RK4) algorithms in the context of an initial value problem.
2. Main Themes and Ideas:
- Numerical Solutions of ODEs: The core focus is on illustrating how numerical methods approximate solutions to differential equations, specifically first-order, linear, explicit ODEs.
- Algorithm Comparison: The simulation provides a side-by-side comparison of the Euler, Heun, and Runge-Kutta methods, highlighting their strengths and weaknesses in terms of accuracy.
- Visual Representation: The simulation uses a graphical interface to show the discrete points calculated by each method alongside the analytic (exact) solution, allowing for a visual assessment of accuracy. A second window displays the relative residual difference, further emphasizing the error associated with each method.
- Initial Value Problem: The simulation emphasizes the concept of an initial value problem, where a starting point is given, and the numerical methods iteratively approximate the solution forward in time.
- Exponential Function Example: The simulation uses the differential equation dy/dt = y, whose analytic solution is y(t) = (initial value) * e^t, as a concrete example. This equation is chosen for its simplicity and clear graphical interpretation.
- Interactive Exploration: Users can adjust the number of intervals (steps) and the initial value to observe the effect on the accuracy of each numerical method.
- Code Accessibility: The underlying code for the algorithms is accessible through the EJS (Easy JavaScript Simulations) console, enabling users to modify and experiment with different differential equations. This is particularly noted: "When you open this file from the EJS console you can easily insert other differential equations instead of the very simple one of the exponential. The corresponding places are highlighted in the code pages."
3. Key Features and Functionality:
- Simulation Interface:
- A graph displaying the analytic solution (blue) and the numerical approximations from Euler (blue), Heun (green), and Runge-Kutta (red) methods.
- A slider to adjust the number of intervals (1 to 24).
- A slider to change the initial value y0 at x = 0.
- A "Back" button to reset the parameters.
- A second window showing the relative residual difference between the numerical approximations and the analytic solution.
- Algorithm Code (Illustrative Snippets):
- Euler:
- xE[0https://iwant2study.org/lookangejss/math/Differential_Equations_Ordinary/ejss_model_e_Diffequ_methods/e_Diffequ_methods_Simulation.xhtml " frameborder="0"></iframe>
Differential Equations: Numerical Solution Study Guide
Quiz
Answer the following questions based on the provided source material. Each answer should be 2-3 sentences.
- What type of differential equation does the simulation focus on, and what specific example is used?
- What are the three numerical methods compared in the simulation for solving ordinary differential equations?
- How does the simulation represent the analytical solution, and how can you modify it?
- Describe how the simulation visually represents the results of each numerical method and how they compare to the analytical solution.
- What parameters remain constant for all numerical methods in the simulation?
- In the Euler method, how is the next y-value (yE[i]) calculated based on the previous value and the interval width (delta)?
- How does the Heun method improve upon the Euler method in calculating the next y-value?
- How does the Runge-Kutta method further refine the approximation compared to the Euler and Heun methods?
- What happens to the accuracy of the numerical approximations as the number of intervals (n) increases, and why?
- Besides exponential, what other differential equations can be solved?
Quiz Answer Key
- The simulation focuses on explicit, linear ordinary differential equations of the first order. The specific example used is the exponential function, described by the differential equation dy/dt = y.
- The three numerical methods compared are the Euler method, the Heun method (also known as the Improved Euler method), and the Runge-Kutta method (specifically the 4-step RK4 method).
- The analytical solution is represented by a blue curve on the graph. The initial value (y0) can be modified using a slider, which causes the analytical solution curve to change accordingly.
- The Euler method results are shown as blue discrete points, the Heun method results are shown as green points, and the Runge-Kutta method results are shown as red points. The simulation allows visual comparison of these points against the blue analytical solution curve.
- The parameters that remain constant for all methods are the initial value (for x=0), the range of variables, the number of intervals (n), and the width of the intervals (delta).
- In the Euler method, the next y-value (yE[i]) is calculated as yE[i] = yE[i-1] + delta * derivativeE[i-1], where yE[i-1] is the previous y-value, delta is the interval width, and derivativeE[i-1] is the derivative at the previous point.
- The Heun method improves upon the Euler method by using an average of two derivative estimates. First it calculates an intermediate y-value (yH_a[i]) using the Euler method, then it uses the derivative at this intermediate point to find a second derivative estimate. The next y-value (yH[i]) is then calculated using the average of these two derivative estimates.
- The Runge-Kutta method further refines the approximation by using four derivative estimates at different points within the interval. These derivative estimates are then combined in a weighted average to calculate the next y-value, resulting in a more accurate approximation.
- As the number of intervals (n) increases, the accuracy of the numerical approximations generally improves. This is because smaller interval widths (delta) lead to better approximations of the slope of the function at each step.
- The provided document indicates that if the file was opened from the EJS console, the user can easily insert other derivatives to solve different differential equations, the correct places are marked in red in the code.
Essay Questions
Consider the following essay questions and formulate well-structured responses based on your understanding of the source material.
- Discuss the advantages and disadvantages of each of the three numerical methods (Euler, Heun, and Runge-Kutta) in approximating the solution to an ordinary differential equation. How does the computational complexity of each method relate to its accuracy?
- Explain the concept of an "initial value problem" in the context of ordinary differential equations and how the initial value affects the numerical solution obtained using the methods presented in the simulation.
- Analyze the relationship between the number of intervals (n) and the accuracy of the numerical approximations. How does the error in each method change as n increases, and what are the practical limitations of increasing n indefinitely?
- Describe how the simulation could be extended or modified to explore other numerical methods for solving differential equations, or to visualize the solutions to different types of differential equations.
- Discuss the role of simulations like this one in teaching and learning about numerical methods for solving differential equations. What are the benefits of using an interactive simulation compared to traditional analytical methods?
Glossary of Key Terms
- Ordinary Differential Equation (ODE): An equation involving a function of one independent variable and its derivatives.
- Numerical Solution: An approximate solution to a mathematical problem, often obtained using computational methods when an analytical solution is difficult or impossible to find.
- Initial Value Problem (IVP): A differential equation along with a specified initial condition, which is a value of the function at a given point.
- Euler Method: A first-order numerical method for approximating the solution of an initial value problem. It uses the derivative at the beginning of the interval to estimate the function's value at the end of the interval.
- Heun Method (Improved Euler Method): A second-order numerical method that improves upon the Euler method by averaging derivative estimates at the beginning and end of the interval.
- Runge-Kutta Method (RK4): A family of numerical methods for solving ODEs. The simulation uses the 4th order Runge-Kutta method (RK4), which uses a weighted average of derivative estimates at multiple points within the interval for higher accuracy.
- Interval Width (delta): The size of the step taken along the independent variable (t or x) in each iteration of the numerical method. It is calculated as the range divided by the number of intervals.
- Analytic Solution: An exact solution to a differential equation, expressed as a mathematical function.
- Relative Residual Difference: A measure of the error in the numerical approximation, calculated as the difference between the numerical result and the analytic solution, divided by the analytic solution.
Sample Learning Goals
[text]
For Teachers
This simple simulation helps to visualize the different numerical methods of solving differential equations.
The three methods that this simulation provides are the Euler method, the Heun method (otherwise known as the Improved Euler method), and the Runge-Kutta method (specifically RK4).
There are two graphs shown in the simulation. The one of the left shows visually how well each method approximates the equation y = e^x, while the graph on the right shows the relative deviation of each method from the actual curve formed by y = e^x
Each numerical method of solving the differential equation y' = y is color-coded as shown on both graphs.
The number of intervals (steps) can be adjusted with the slider at the top, going up to n = 25. The button at the top right resets the simulation.
As the number of intervals increase, note how the approximations become more accurate to the original curve.
Research
[text]
Video
[text]
Version:
- http://weelookang.blogspot.sg/2016/02/vector-addition-b-c-model-with.html improved version with joseph chua's inputs
- http://weelookang.blogspot.sg/2014/10/vector-addition-model.html original simulation by lookang
Other Resources
[text]
Ordinary Differential Equations Simulation FAQ
- What is this simulation about?
- This simulation demonstrates numerical methods for solving ordinary differential equations (ODEs), focusing on the initial value problem. It compares the Euler, Heun (Improved Euler), and Runge-Kutta (RK4) algorithms. The simulation uses the example of the exponential function (dy/dt = y) to visually show how each method approximates the analytic solution.
- Which numerical methods are compared in this simulation?
- The simulation compares three numerical methods: the Euler method, the Heun method (also known as the Improved Euler method), and the Runge-Kutta method (specifically, the 4th order Runge-Kutta method or RK4).
- What differential equation is used as an example in the simulation, and why?
- The simulation uses the differential equation dy/dt = y, representing the exponential function. This equation is used because it's simple, linear, and of the first order. It also doesn't explicitly depend on the variable 't', which makes for easier graphical interpretation. The analytic solution is y(t) = (initial value) * e^t.
- How does the simulation visualize the accuracy of the numerical methods?
- The simulation displays two graphs. The first graph shows the analytic solution and the points calculated by each numerical method, allowing a visual comparison of their accuracy. The second graph shows the relative residual difference (mistake) between the numerical approximations and the analytic solution, providing a more detailed view of the error for each method. Different colors represent each method (Euler in blue, Heun in green, Runge-Kutta in red).
- What parameters can be adjusted in the simulation, and how do they affect the results?
- Users can adjust the following parameters:
- Number of intervals (n): This controls the step size of the numerical methods. Increasing the number of intervals generally improves the accuracy of all methods, as the step size (delta) decreases (delta = Range/(n-1)).
- Initial value (y0): This sets the starting point for the solution. Changing the initial value affects the analytic solution curve and the numerical approximations.
- Where can I find the code for the numerical methods, and how can I modify the differential equation?
- The code for the Euler, Heun, and Runge-Kutta methods is available within the EJS console, on the Initialization page. Users can modify the derivative within the code to solve different differential equations. The sections where modifications can be made are highlighted in red within the code.
- What are some suggested experiments or activities I can do with this simulation?
- The simulation suggests the following experiments:
- Compare the quality of the three numerical approximations with a small number of intervals (e.g., n = 5).
- Increase the number of intervals and observe how the approximations approach the analytic solution.
- Track the relative errors of each method as the number of intervals increases and attempt to graph the error dependence. (Error vs. n). The Euler method error will appear as a straight line, Heun’s method will look like a second order parabola and Runge-Kutta’s method will have deviations that are too small to see as a fourth order parabola.
- Modify the code in the EJS console to solve different differential equations by changing the derivative calculation.
- Who created this simulation, and where can I find more information about the Easy JavaScript Simulations (EJS) tool?
- 📈The simulation was created by Dieter Roess in March 2009. The simulation is part of the "Learning and Teaching Mathematics using Simulations" project. For commercial use of EasyJavaScriptSimulations Library, please read https://www.um.es/fem/EjsWiki/Main/EJSLicense and contact fem@um.es directly.
- Details
- Written by Wei Chiong
- Parent Category: 5 Calculus
- Category: 5.5 Differential equations
- Hits: 6466