2 views (last 30 days)

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.

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.

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

Start Hunting!