Why "Error using odearguments“ and ”Error in ode15s“ ?

I am trying to solve ODE using ode15s first, when I set some parameters in the subfunction of ODE as constant, I can get the right solution. However, when I set the parameters as variable, and try to use Particle Swarm Optimization(PSO)to fit the parameters, that is to say, to optimize the parameters by fit the results of ODE to experiment datas using PSO, errors always exist like this:
if true
@(T,IN2)ZEROS(0,0) returns a vector of length 0, but the length of initial conditions vector is 30. The vector returned by @(T,IN2)ZEROS(0,0) and the initial conditions vector must have the
same number of elements.
Error in ode15s (line 150)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in PSO (line 43)
[t,N]=ode15s(odefun,[0:95],NN0);
end
The changes in subfunction is like this, first constant and then change to variable.
function A=getA(ii,j,t,R,D)
%UNTITLED9 Collision efficiency
% x1 x2 x3 are fitting parameters
global x1 x2 x3
A=x1*exp(-x2*(1-ii/j).^2)/(ii*j).^(x3);
% A=0.046*exp(-0.1*(1-ii/j).^2)/(ii*j).^(0.1);
end

3 Comments

We need to see your odefun and we need to know more about NN0
Thanks a lot for your prompt reply.
  • Item one
odefun is set as you told me in my previous question.
odefun = matlabFunction(FF, 'vars', {t, N.'});
NN0=[2.46372E+14;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0];
  • Item two
I have tried to not use global, but it doesn't work.
My whole code is attached, the PSO.m is the main program.
Thanks again and best wishes.

Sign in to comment.

 Accepted Answer

In function getA you use
global x1 x2 x3
However, you never assign values to x1, x2, or x3 anywhere in any of your code, so those default to being the empty array, leading getA to return the empty array, so the results of all of your F() calls are the empty array, so you end up trying to process the empty array as an ODE.
You would not have this problem if you were to rewrite your code so that you did not use global.

1 Comment

Thanks for your valuable reply, I have rewrite my code and getA is OK now. But there still another problem. It is about ODE, I will try to fix it. Thanks againg and sorry for reply so late.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!