# What is “tolerance” in ODE45 in Matlab?

I have used ode45 in Matlab. And, it is my understanding that the 4 and the 5 are for the order of the global and local error, respectively. So, the global error is on the order of $h^4$ and the local error is on the order of $h^5$, where $h$ is the step size.

But, then there's an option called RelTol, which you can set by: odeset('RelTol',1e-6).

How is it possible to set the relative tolerance lower than the global or local error in this case? Or is the name ode45 a bit of a misnomer?

• It's not a misnomer, but a misunderstanding on your side -- as the documentation states: "ode45 is based on an explicit Runge-Kutta (4,5) formula, the Dormand-Prince pair". Furthermore, "order" in errors usually refers to the power of the stepsize (i.e., $\|y_h-y\| \leq C h^4$ would be "fourth order"), not a fixed error magnitude. The tolerance you specify is used to determine the step size $h$ to achieve the desired order. Dec 6 '16 at 21:43
• Keep in mind that the error control is local rather than global, so the error after n steps can be much larger than the error tolerance per step. Dec 6 '16 at 22:46
• @ChristianClason, so you're basically saying that the $||y_h - y||$ is the "RelTol" setting. And, to find the appropriate step size, you would solve this equation $||y_h - y|| \le Ch^4$ for $h$? Dec 6 '16 at 23:55
• No; that would be an absolute tolerance, a relative tolerance would be $\|y_h-y\|/\|y\|$. Also, you don't know $y$ (otherwise you wouldn't need to solve the ODE numerically), so you can't just solve for $h$. Rather, ode45 makes use of a sophisticated error estimator (that's where the 5 comes in) and reduces $h$ until the local error estimate satisfies (the sum of) both relative and global tolerance. Dec 7 '16 at 7:38
• Thanks. This guy seems to define the relative tolerance as dividing the absolute difference by the minimum of the estimate and the "true" value. But, you seem to think it's always the "true" value? mathworks.com/matlabcentral/answers/… Dec 7 '16 at 17:48

And, it is my understanding that the 4 and the 5 are for the order of the global and local error, respectively.

The local error of a Runge–Kutta method of order $n$ is proportional to $h^n$.

What ode45 does is to estimate the solution (of one step) with two Runge–Kutta methods with local orders of 4 and 5, respectively (hence those numbers). It uses the solution of the 5th-order method to estimate the solution of the ODE and the difference between the solutions from the two methods to estimate the error of the integration. Mind you that this is only an estimate – if you knew the precise error, you would also have already solved the problem precisely. To save computation time, the two Runge–Kutta methods are designed such that they use common intermediate results. Other embedded Runge–Kutta methods (i.e., Runge–Kutta methods with two numbers to indicate order) work similarly.

The error estimate is then used to optimise the step size: If the error is above a given tolerance (for each component), the step size is decreased until the error is below that tolerance. If the error is far below that tolerance, the step size in increased to save time. This tolerance is:

$$\text{AbsTol}_i + \text{RelTol}·|\hat{y}_i|,$$

where $|\hat{y}_i|$ is the estimated solution.

• You are correct; this is described in mathworks.com/help/pdf_doc/otherdocs/ode_suite.pdf. Dec 7 '16 at 8:35
• Thank you, @Wrzlprmft. I understand that the RK5 is used to find an estimate of the "true" value of $y$. But, if we use the RK5 to estimate the true value, then wouldn't it make sense to just use it to solve for the entire solution to the DE using RK5? Since, we have to compute it anyway for adaptive stepping. Dec 7 '16 at 17:35
• No, you use RK5 to estimate the error. That's a very different thing. (An estimate of the error is more useful here than a higher-order approximation since it allows you to take large time steps where small steps are not necessary, thus saving computational effort.) Dec 7 '16 at 18:35
• @Hunle: The estimate of the solution is obtained using the 5th-order method (see my edit). But with the solution from only one method, it’s impossible to obtain a robust estimate of the error. Therefore you need the second method. Dec 7 '16 at 22:52
• @ChristianClason no, the 5th order method is used for the stepping. This is known as local extrpolation. The 4th order embedded method is only used for error estimation. But the 4th order method is essentially free since it just is a different linear combination of the stages, so there's not much of an overhead for doing this. But ode45 is still "mostly" 4th order since it fills in extra values into the solution with a 4th order interpolation, even though its actual steps are 5th order. Oct 2 '17 at 6:40