0

I have a ODE function which I am trying to solve multiple times with different parameters. here is the function

function xdot=SB(t,x)
global ps f R0
S=.0725 ; 
rhoL=998; 
vL =1.0e-3;
c = 1481; 
pvtinf=0 ; 
pinf0=1.01e5;
k =7/5; 
xdot(1,1)=x(2);
xdot(2,1)=((-0.5*x(2)^2)*(3-x(2)/(c))+(1+(1-3*k)*x(2)/c)*((pinf0-pvtinf)/rhoL+2*S/(rhoL*R0))*(R0/x(1))^(3*k)-2*S/(rhoL*x(1))-4*vL*x(2)/(rhoL*x(1))-(1+x(2)/c)*(pinf0-pvtinf+ps*sin(2*pi*f*t))/rhoL-ps*x(1)*cos(2*pi*f*t)*2*pi*f/(rhoL*c))/((1-x(2)/c)*x(1)+4*vL/(rhoL*c));

here is the solver that I'm using to solve it multiple times by changing variables in the function.

clc;clear;
global ps f R0
R=[1 2 3 4 5 6 7].*1e-6;
options= odeset('RelTol',1e-12,'AbsTol',1e-13);
f=2e6;
ps=100e3;

for i=1:length(R)
    R0=R(i);
    [t{i},x{i}]=ode45(@SB,(0:0.01/f:5/f),[R0 0],options);
end

figure()
for i=1:length(R)
    plot(t{i},x{i}(:,1))
hold on
end
hold off 
grid on

In this case I'm updating R0. in other occasions I'm updating f or ps (global variables). I am trying to use PARFOR to speed things up but I can't find a way to pass these global variables into functions when using PARFOR.

Tips ?

Hos
  • 1
  • Don't use global variables. [Parametrize the handle](https://www.mathworks.com/help/matlab/math/parameterizing-functions.html) at each iteration. [See here](https://stackoverflow.com/questions/7680224/matlab-ode45-how-to-change-a-parameter-inside-it-while-calling-it). – TroyHaskin May 24 '17 at 23:51
  • Thanks ! It worked ! – Hos May 25 '17 at 22:59

0 Answers0