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 Exchange에서 Loops 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