ode45 not working within loop

1 次查看(过去 30 天)
I have a for loop within a function that is not working properly when using ode45. What this loop is supposed to do is change two variables at the F1 index and F3 index within the cell named 'b', write each line of cell b into the odefun 'mass_bal_pG', use ode45 to evaluate the differential equations contained in mass_bal_pG, then plot the data. The problem I am having is that when I run this code it will report the same x and y data for each iteration, but when I debug and step manually line by line it does exactly what it is supposed to do. I tried placing pause functions in between each line with no luck. It seems that the only thing I can do is run the code while debugging. Any ideas?
for i=F1_range
for j=F3_range
count=count+1;
b{F1_index}=['F1=' num2str(i) ';'];
b{F3_index}=['F3=' num2str(j) ';'];
fh=fopen('mass_bal_pG.m','w');
for k=1:numel(b)
fprintf(fh,'%s\n',b{k});
end
fclose(fh);
simdata=ode45(@mass_bal_pG,timeframe,initial);
x_dat{count}=simdata.x';
y_dat{count}=simdata.y';
figure
plot(x_dat{count},y_dat{count})
end
end
  2 个评论
Star Strider
Star Strider 2014-9-6
We would probably have to see the code for mass_bal_pG. You say it’s doing everything it should when you step through it, so I assume it is also saving x_dat and y_dat correctly as well, incrementing them each time.
Yours is the definitely the most creative way of passing parameters I’ve ever seen! (There are more efficient ways.)
Nick
Nick 2014-9-7
Yes the parameters for x_dat and y_dat are saving correctly when I manually step through the function only. This is the mass_bal_pG:
function [dCdt]=mass_bal_pG(t,conc)
dCdt=zeros(4,1);
F0=100;
Ca0=0.9;
Vr=1000;
k1=0.7;
Ki=.03;
F1=50;
F3=20;
Cp30=0.05;
Vx=200;
Vy=100;
m=2;
Ka=1;
cp3=(1/Vy)*(Cp30*F3-F3*conc(1)+Vy*Ka*(m*conc(2)-conc(1)));
cp2=(1/Vx)*((F0+F1)*(conc(3)-conc(2))-Vx*Ka*(m*conc(2)-conc(1)));
cp1=((F1*conc(2)-(F0+F1)*conc(3))/Vr)+(k1*conc(4))/(1+conc(3)/Ki);
ca1=((F0*Ca0+F1*conc(4)-(F0+F1)*conc(4))/Vr)-(k1*conc(4))/(1+conc(3)/Ki);
dCdt(1)=cp3;
dCdt(2)=cp2;
dCdt(3)=cp1;
dCdt(4)=ca1;
end
Would you have any tips for me on changing the values of F1 and F3 within mass_bal_pG on each iteration in a more efficient way? Im always open to advice. Thanks!

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2014-9-8
Untested code, but I would change the first line of the function to:
function [dCdt]=mass_bal_pG(t,conc,F1,F3)
and then change the ode45 call to:
F1 = i;
F3 = j;
simdata=ode45(@(t,conc) mass_bal_pG(t,conc,F1,F3), timeframe,initial);
and delete everything between: count = count + 1; and your ode45 call. Your ‘mass_bal_pG’ will pick up F1 and F3 from your workspace.
However consider changing the ode45 call to the more conventional (and somewhat easier):
[x,y] = ode45(@(t,conc) mass_bal_pG(t,conc,F1,F3), timeframe,initial);
x_dat{count} = x;
y_dat{count} = y;
I am not certain these changes will solve the problem, but they’re worth considering.
  2 个评论
Nick
Nick 2014-9-9
The first suggestion worked perfectly. Thank you so much!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by