ode45 is causing an infinite loop

Can someone tell me why my ode45 keeps looping and calling the function without stopping?

 function dcdt = project101r1d1x1(t, c)
dcdt = zeros(size(c));
D = 1.97e10; 
r_cup = 0.075; %r_cup1
h = r_cup / 10;
dcdt(1) = 0;
c11 = (( 2 * r_cup + h^2 ) * c(10) - r_cup * c(9) ) / (r_cup + h) ;
for x = 2 : 9
    dcdt(x) = D * ( ( c(x + 1) - 2 * c(x) + c(x - 1) ) / h^2 + (1 / r_cup) * ( c(x + 1) - c(x - 1) ) / (2 * h));
end
dcdt(10) = D * ( ( c11 - 2 * c(10) + c(9) ) / h^2 + 1 / r_cup * ( c11 - c(9) ) / (2 * h));
 c0 = zeros(10, 1);
x_teabag = 0.04; 
y_teabag = 0.0075;  
z_teabag = 0.06;
c0(1) = (5.03 / 290.26 ) / (2 * x_teabag * y_teabag + 2 * y_teabag * z_teabag + 2 * x_teabag * z_teabag);
tspan = [0 0.1];
r_cup1 = 0:0.0075:.07425;
[t,c] = ode45('project101r1d1x1', tspan, c0);
plot(r_cup1, c(2,:));
legend('t = 0.1 s');
xlabel('r (m)');
ylabel('C (mol/m^3)');

Risposte (1)

Torsten
Torsten il 17 Nov 2016
Modificato: Torsten il 17 Nov 2016
Separate the two parts of your code:
function main
c0 = zeros(10, 1);
x_teabag = 0.04;
y_teabag = 0.0075;
z_teabag = 0.06;
c0(1) = (5.03 / 290.26 ) / (2 * x_teabag * y_teabag + 2 * y_teabag * z_teabag + 2 * x_teabag * z_teabag);
tspan = [0 0.1];
r_cup1 = 0:0.0075:.07425;
[t,c] = ode45(@project101r1d1x1, tspan, c0);
plot(t, c(2,:))
legend('t = 0.1 s')
xlabel('r (m)')
ylabel('C (mol/m^3)')
function dcdt = project101r1d1x1(t, c)
dcdt = zeros(10,1);
D = 1.97e10;
r_cup = 0.075; %r_cup1
h = r_cup / 10;
dcdt(1) = 0;
c11 = (( 2 * r_cup + h^2 ) * c(10) - r_cup * c(9) ) / (r_cup + h) ;
for x = 2 : 9
dcdt(x) = D * ( ( c(x + 1) - 2 * c(x) + c(x - 1) ) / h^2 + (1 / r_cup) * ( c(x + 1) - c(x - 1) ) / (2 * h));
end
dcdt(10) = D * ( ( c11 - 2 * c(10) + c(9) ) / h^2 + 1 / r_cup * ( c11 - c(9) ) / (2 * h));
Best wishes
Torsten.

8 Commenti

They are already in two separate m files, I just didn't know how to denote that on here.
Torsten
Torsten il 17 Nov 2016
If you run the above file as one .m file named main.m, there can't be an infinite loop anywhere. Maybe ODE45 cannot continue at a certain time instant ?
Best wishes
Torsten.
Ok I tried that just now and it's still somehow repeatedly calling the function.
Torsten
Torsten il 17 Nov 2016
Of course the function is called repeatedly, but (hopefully) at different time instants t ...
Best wishes
Torsten.
Well yes but it never stops. I tried making tspan = [0 .1 1] and that should call the function 3 times right? But it keeps going.
Torsten
Torsten il 17 Nov 2016
No, it keeps calling the function with (usually) increasing t-values until t=1.
The number of calls depends on the uglyness of your ODEs.
Best wishes
Torsten.
I am also suffreing the same issue whether I have different system. How to get rid of it, Sir Torsten if tspan consisting of only 10 time-steps????
It's possible your ODE is stiff and so is not actually stuck in an infinite loop but is making very, very slow progress. Try a stiffer solver to check this. See this documentation page for a discussion of the different ODE solvers.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 17 Nov 2016

Commentato:

il 4 Gen 2019

Community Treasure Hunt

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

Start Hunting!

Translated by