# Thread: MATLAB ODE45 in a for cycle

1. ## MATLAB ODE45 in a for cycle

Hi guys,

As you can guess from the title, I want to have a for cycle that calls ode45 solver in each iteration, as ode45(odefun, tspan, yinitial).

The differential equation that I define for odefun (in order to dy/dt, for instance) has some parameters that have to assume different values in each iteration. The problem is that I only find examples with odefun receiving t and y as input parameters...

How can I add more input parameters to odefun, in order to import different values for each iteration of the for cycle?

Thanks for all the help!

2. Originally Posted by rber
Hi guys,

As you can guess from the title, I want to have a for cycle that calls ode45 solver in each iteration, as ode45(odefun, tspan, yinitial).

The differential equation that I define for odefun (in order to dy/dt, for instance) has some parameters that have to assume different values in each iteration. The problem is that I only find examples with odefun receiving t and y as input parameters...

How can I add more input parameters to odefun, in order to import different values for each iteration of the for cycle?

Thanks for all the help!

Example:

Code:
function yprime = myode(t, y, c)
yprime = c*y;
Code:
for k = -2:2
[t,y] = ode45(@myode, [0 3], 1, [], k);
figure
plot(t, y)
title(['Solution for k = ' num2str(k)])
end
CB

3. I tried that but it only worked for one parameter... If I try to pass more (I need to pass at least four) it doesn't work...

I also tried to put all of them in an array, but it didn't work neither...

4. Originally Posted by rber
I tried that but it only worked for one parameter... If I try to pass more (I need to pass at least four) it doesn't work...

I also tried to put all of them in an array, but it didn't work neither...
Then try making them globals.

CB

5. Passing them to global variables isn't working neither...

BTW, this is part of the code where I call ode45:

Tc_i=65;

for n=1:length(Ta)
t_i(n)=(n-1)*15*60;
t_f(n)=n*15*60;
qs_s=qs(n);
Ta_s=Ta(n);
v_s=v(n);
I_s=I(n);
[t,Tc]=ode45(@myode, [t_i(n) t_f(n)], Tc_i);
strcat(t_total,t);
strcat(Tc_total,Tc);
Tc_i=Tc(length(Tc));
end

This is the function myode that I defined:

function xprime=myode(t,Tc,qs_s,Ta_s,v_s,I_s)

ALFA=0.5;
R20=1.093e-4;
ALFA_R=4.03E-3;

EPS=0.6;
SIGMA=5.6704e-8;

d=0.023;

Sal=264.42e-6;
Cal=2432700*pi()*d;
Sac=61.7e-6;
Cac=3578800*pi()*d;

C=Sal*Cal+Sac*Cac;

xprime=(qs_s+(R20*(1+ALFA_R*(Tc-20)))*I^2-EPS*SIGMA*pi()*d*((Tc+273.15)^4-(Ta_s+273.15)^4)-8.55*(v_s*d)^0.448*(Tc-Ta_s))/C;

end