ode23tb and non-linear curve fitting
Show older comments
Dear All,
I think that I found an interesting bug-like thing while I was using ode23tb (MATLAB 2016b).
According to the Documentation :
[t,y] = ode23tb(odefun,tspan,y0,options) %where t is the same as tspan,if tspan has more than 2 elements
My tspan has 1185 elements.
I use the ode23tb in a function,which I want to minimize in a non-linear curve fitting procedure. I use fminsearchcon to find the minimum. There is no problem to find a nice-looking minimum.
The problem first occured when I called the function jacobianest in order to calculate the the confidence intervals. This function calls multiple times the fit function and therefore ode23tb, but with different (fit) parameters because the jacobianest calculates the partial derivatives. Near the end of the jacobianest routine, ode23tb returns with t that has only 759 elements while the tspan has still 1185 elements. It seems that a special configuration of parameters for ode23tb reduces the dimension of the returning values. Of course, later this results in an error because of dimension mismatch. This is very weird and I could not find the problematic part in my code.
When I got this dimension loss of t, I saved the paramteres from the jacobianest , and I just plugged them into ode23tb without any fitting routine and it gave the same error.
The order of the parameters were correct but one obtained a negative sign, which is not realistic, though mathematically it should not be an issue.
Do you have any idea what is happening here?
Just for clarity: I have 5 fit parameters, which define the solution of the ode23tb, none of them is the initial parameter. I have three equations, the ode23tb returns three columns. Then, another function makes a single column out of these three. This is the vector that I want to fit to my data. I guess the issue is not with the fitting routine but with ode23tb.
9 Comments
Ameer Hamza
on 29 Apr 2020
I guess the shortening of vector 't' is accompanied by a warning in the command window stating that the ode23tb is unable to meet integration tolerance or something similar. My guess is that your system of ODE is unstable at that point. MATLAB ode functions return partial solutions if the response of ODE becomes unstable.
Walter Roberson
on 30 Apr 2020
That would be my suspicion too.
Tim
on 30 Apr 2020
Walter Roberson
on 30 Apr 2020
The step size in your tspan is not the problem; ode23tb calculates where-ever it feels like calculating to find locations that it feels comfortable are within error tolerance of the previous location, and it uses those where-ever-it-feels-like points to predict the values at the tspan values you provide. You are not using a fixed step solver, so the solver does not bother solving at the tspan locations, just close enough to them that it is happy with the error estimates.
What would make a difference is reducing your maximum t below 4.164915e-10 to avoid the discontinuity. And if that is not possible, then you have a problem.
Ameer Hamza
on 30 Apr 2020
Step-size in tspan has no effect on the internal working of the ode functions. These functions use their internal algorithm to calculate a solution, and then use interpolation to return the solution vector at points in tspan.
You may need to analyze your system of ODE to find out where they can become unstable and then set some bounds on the parameter passed to jacobianest such that they remain inside the stability region of ode function.
Are Mjaavatten
on 30 Apr 2020
I think the comments by Walter and Ameer are very relevant. When I encounter the "unable to meet integration limits" error in solving an ODE system it generally means that I hit a singularity. This may be a real singularity in my problem, but usually it means that I have made some error. Since the error comes very early in your case, I would first run odefun(0,y0) and see if all derivatives seem reasonable. If they seem OK, I would follow Walter's suggestion and use tspan = [0, 4.16e-10], or shorter if this crashes. I would then examine the time series. One or more components may start to diverge, which may give you a hint as to what is happening.
Walter Roberson
on 30 Apr 2020
A difficulty that I have encountered several times recently with ode45, is a situation where ode45 is projecting points (there is a mathematically optimal way of projecting for each different ode* routine) to calculate error, and it happens to project to a location where the return value from odefun includes nan (typically because the previous point produced inf). When that happens, the ode*() routine fails and cannot continue. The ode*() functions can survive +/- inf in themselves, but not nan -- but if you generate an inf then it is highly likely that a nan will soon follow.
The expection of the ode* routines is that if they project too far, that the return values will be sufficiently different from the current values that the calculated error is large, which the ode* routines would detect and reject the location as a candidate location, shrinking back to a less ambitious step, Over-projecting is normal for the ode*() routines, and as long as the odefun does not return nan recovery is fine. But the logic does not permit recovery when a nan is encountered.
The work-around for this is to put into your code tests for the calculated derivatives being nan or inf, and if that is found, emitting something large but finite that will "surely" lead to the ode*() routine deciding that the error is too much and that the step should be rejected.
Tim
on 30 Apr 2020
Ameer Hamza
on 30 Apr 2020
Tim, The idea suggested by Walter is to filter out Inf and NaN values inside your odefun, so that the ode23tb never get to see those. Something like this
function dydt = odefun(t,y)
% your current code which calculates dydt
dydt(isinf(dydt)) = 1e50; % 1e50 or a very large number as compared to the normal
% value of your function
dydt(isnan(dydt)) = 1e50; % same for nan.
end
Add these lines at the end of your odefun. It will replace all the infinities in dydt with a very large number. This technique will signal the ode23tb that it is approaching infinity if it projects too far from the current point while prevent the messing up of its internal states because of some unexpected NaN value caused by infinities.
Accepted Answer
More Answers (0)
Categories
Find more on Manual Performance Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!