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)');

回答 (1 件)

Torsten
Torsten 2016 年 11 月 17 日
編集済み: Torsten 2016 年 11 月 17 日

0 投票

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 件のコメント

Sam Michaels
Sam Michaels 2016 年 11 月 17 日
They are already in two separate m files, I just didn't know how to denote that on here.
Torsten
Torsten 2016 年 11 月 17 日
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.
Sam Michaels
Sam Michaels 2016 年 11 月 17 日
Ok I tried that just now and it's still somehow repeatedly calling the function.
Torsten
Torsten 2016 年 11 月 17 日
Of course the function is called repeatedly, but (hopefully) at different time instants t ...
Best wishes
Torsten.
Sam Michaels
Sam Michaels 2016 年 11 月 17 日
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 2016 年 11 月 17 日
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.
Muhammad
Muhammad 2019 年 1 月 4 日
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????
Steven Lord
Steven Lord 2019 年 1 月 4 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2016 年 11 月 17 日

コメント済み:

2019 年 1 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by