Need technique to compare accuracy between different methods

9 views (last 30 days)
I have used Euler's method, Runge Kutta 2nd and 4th order computational methods to compute my radioactive decay program. Now, How do I compare accuracy and efficiency of these program in the code. Thank you

Answers (1)

Jim Riggs
Jim Riggs on 25 Sep 2019
Edited: Jim Riggs on 25 Sep 2019
As @darova suggests, if you have an analytical solution, then you can compare your numerical solution to the exact answer. This is obviously the best measure of error you can get. Appart from that, here are some other suggestions:
To measure the efficiency of the method, you want to consider how long it takes to get an answer with a certain level of accuracy. Note that Euler is a "single pass" method, that only evaluates your funtion once per integration step. 2nd order Runge-Kutta (RK2) is a "2-pass" method, that evaluates your function twice every step. This means that, in general, RK2 needs twice as much computation per integration step compared to Euler. Similarly, RK4 requires 4 function evaluations for each step, so it takes four times the computation compared to Euler. But the benefit is that RK2 is more acccurate than Euler, so you can take a bigger step (for a given level of accuracy), and RK4 is more accurate than RK2, so it can take an even bigger step. In general, the accuracy improves a lot with increasing solution order, so you end up with better accuracy with over-all less computation.
Note, however, that in order to see this behavior, the function that you are evaluating needs to be of sufficient order that the higher order solver can contribute to it's full potential. For example, if the function is a straight line, then RK4 and RK2 will provide the exact same solution, so in this case, RK2 will be more efficient. If the function you are evaluating is 4th order or higher, then RK4 will prove to be the most efficient, because it can give a better approximation to the higher order function and allow you to use a bigger step size.
PERFORMANCE EVALUATION
Create a table for recording the performance of each method. The table should record;
  • Solution Method (Euler, RK2, RK4)
  • Integration step size
  • Final solution value
  • Execution time (from Matlab tick/tock)
You can get a measure of the time it takes to perform the calculation using the tick/tock commands in Matlab. Place the "tick" command at the beginning of the calculation, and "tock" at the end. This will tell you the time (clock time) that it took to perform the calculation.
tick;
... (run model)
executionTime = tock;
Run your solver using each of the solution methods (Euler, RK2, RK4) for a range of different time steps and record the solution and the execution time. If the function is of sufficiently high order, RK4 should give a better solution than RK2, and RK2 will be better than Euler. Create plots of the solution value vs. the time step (or log(time step) ) to see the value change with the time step. As the step gets smaller, the value should asymptotically approach the exact solution. You should notice that RK4 gets there for a larger step size, and Euler requires a very small step to get in the same ballpark. I like to use a log scale because you will want to vary the time step over many orders of magnitude.
With this information, (if you don't have an analytical solution with the exact answer) you can pic your best solution value from the RK4 data and let it represent the exact solution. Use this value to calculate the % error for all of the other solutions.
Now you can plot the %error vs. the time step and the %error vs. the run time for each soution method. This should show for a specific level of accuracy (%error) RK4 is the fastest; it will take longer to get the same accuracy using RK2, and much, much longer using Euler.
Again, these trends are observable only if the function you are evaluating is sufficiently complex ("high order").
(I give an example of a case study in the discussion of Euler's method here )

Categories

Find more on Visual Exploration in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!