Why is ode45 not working when I insert the options input?

I am trying to model some functions, but it seems that ode45 is not working. However, when I get rid of 'options' when I'm calling ode45, it runs but it will not finish running. I'm still new to MATLAB, so how can I fix this issue?
%CONSTANTS
i = 6.3;
a = 1;
b = 0.83;
vNa = 115;
vK = -12;
vL = 10.5989;
simtime = [0 100];
xinit = [0, 0.4, 0.1];
%CALLING FUNCTIONS
options=odeset('RelTol',1e-9,'Refine',10);
[t,x]=ode45(@(t,x) HHModel(t,i,a,b,vNa,vK,vL,x),simtime,xinit, options);
%GRAPHS
%Membrane Potential
subplot(221); plot(t,x(:,1)); grid on;
xlabel('Time [s]'); ylabel('v');
%FUNCTIONS
function amv = am(v)
amv = ((25-v)/10)/((exp((25-v)/10))-1);
end
function bmv = bm(v)
bmv = 4*exp(-v/18);
end
function tmv = tm(v)
tmv = 1/(am(v) + bm(v));
end
function anv = an(v)
anv = ((10-v)/100)/((exp((10-v)/10))-1);
end
function bnv = bn(v)
bnv = 0.125*exp(-v/80);
end
function dx = HHModel(t,i,a,b,vNa,vK,vL,x)
amv = am(x(1));
tmv = tm(x(1));
anv = an(x(1));
bnv = bn(x(1));
dx1 = i - 120*(x(3))^3*(b - a*(x(2)))*(x(1)-vNa) - 36*(x(2))^4*(x(1)-vK)-0.3*(x(1)-vL);
dx2 = anv - x(2)*(anv - bnv);
dx3 = amv - (x(3)/tmv);
dx = [dx1;dx2;dx3];
end

 Accepted Answer

Try ode15s instead, it seems to converge, you have to check if the answer makes sense
%CALLING FUNCTIONS
options=odeset('RelTol',1e-9,'Refine',10);
[t,x]=ode15s(@(t,x) HHModel(t,i,a,b,vNa,vK,vL,x),simtime,xinit,options);

3 Comments

It may be that you have a wide range of time scales in your model, that is some relatively fast dynamics and some that are relatively slow. In this case ode45 can have trouble because it needs to use such small time steps to converge that it takes a long time. For this type of system the "stiff solvers" such as ode15s are more effective.
By the way, the above call using ode15s seems to work, and give similar results with or without the call to odeset to change the default paramaters.
One other thing, regarding running with ode45 and the options set, the reason that you get no data plotted is becuase all but the first point is returned as NaN (not a number) and does not get plotted. Clearly something wrong with using that particular set of options. In anycase, seems OK with ode15s.
Your welcome, glad this helped.

Sign in to comment.

More Answers (0)

Products

Release

R2022a

Asked:

on 29 Jun 2022

Commented:

Jon
on 30 Jun 2022

Community Treasure Hunt

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

Start Hunting!