How to use multiple function's solutions as variables in other functions

7 次查看(过去 30 天)
I am using 8 different functions, all needing eachother in order to work. If I put this in the functions,
tRange = (0:144);
Xf0 = [0.2;0];
[~,XfSol] = ode45(@lipidfreecellgrowthw,tRange, Xf0 );
C0 = [0;0];
[~,CSol] = ode45(@byproductaccumulationsw, tRange, C0);
LE0 = [0;0];
[~,LESol] = ode45(@nonEPAlipidaccumulationsw, tRange, LE0);
E0 = [0;0];
[~, ESol] = ode45 (@EPAaccumulationw, tRange, E0);
S0 = [100;0];
[~,SSol] = ode45(@glucoseconsumptionw,tRange, S0 );
N0 = [5;0];
[~,NSol] = ode45(@nitrogenconsumptionsw,tRange, N0 );
Ncell0 = [1;0];
[~,NcellSol] = ode45(@intcellsstorednitrogenw,tRange, Ncell0 ) ;
V0 =[1;0];
[~,VSol] = ode45(@volumechangew, tRange, V0);
for Xf = 1:numel(XfSol); C = 1:numel(CSol) ; LEf = 1:numel(LESol); E = 1:numel(ESol); S = 1:numel(SSol); N = 1:numel(NSol); Ncell = 1:numel(NcellSol); V = 1:numel(VSol);
they do not work because it has to go through so many iterations. I need XfSol, CSol, LESol, ESol, SSol, NcellSol, and VSol to be callable in the functions themselves so they can be used for variables.
If I put this separately from the function,
tRange = (0:144);
Xf0 = [0.2;0];
[~,XfSol] = ode45(@lipidfreecellgrowthw,tRange, Xf0 );
C0 = [0;0];
[~,CSol] = ode45(@byproductaccumulationsw, tRange, C0);
LE0 = [0;0];
[~,LESol] = ode45(@nonEPAlipidaccumulationsw, tRange, LE0);
E0 = [0;0];
[~, ESol] = ode45 (@EPAaccumulationw, tRange, E0);
S0 = [100;0];
[~,SSol] = ode45(@glucoseconsumptionw,tRange, S0 );
N0 = [5;0];
[~,NSol] = ode45(@nitrogenconsumptionsw,tRange, N0 );
Ncell0 = [1;0];
[~,NcellSol] = ode45(@intcellsstorednitrogenw,tRange, Ncell0 ) ;
V0 =[1;0];
[~,VSol] = ode45(@volumechangew, tRange, V0);
it does not work because it is not in te function. What can I do to be able to call these in the functions themselves?

回答(1 个)

Steven Lord
Steven Lord 2021-7-28
If you need to evaluate the solutions of those ODEs at different times than ode45 returned (which is how I'm interpreting "so they can be used for variables") call ode45 with one output and use deval to evaluate that solution at a different time.
format longg
[t,y]=ode45(@vdp1,[0 20],[2 0]);
plot(t, y(:, 1), 'k-+'); % Only plot the first component
hold on
sol = ode45(@vdp1, [0 20], [2 0])
sol = struct with fields:
solver: 'ode45' extdata: [1×1 struct] x: [1×60 double] y: [2×60 double] stats: [1×1 struct] idata: [1×1 struct]
tt = 0:4:20;
yy = deval(sol, tt, 1); % Only interested in the first component for plotting purposes
plot(tt, yy, 'ro')
In some cases the red circles are more or less on a plus symbol, but in other cases they represent the value of the solution of the ODE at a time not in the original t vector (the circles at t = 12 and t = 16 look to be in such a gap.)
  2 个评论
Brianna Biondo
Brianna Biondo 2021-7-28
Each function has over 100 solutions that have to be factored in. This is one of the functions I wrote.
function dXfdt = lipidfreecellgrowthw(~, Xf)
FS = 0.03;
muMax = 0.26;
KN = 0.033;
KS = 0.077;
KiS = 500;
KiX = 152;
KiN = 0.073;
YXN = 27;
Vevap = 0.0005;
for N = 1:numel(NSol); S = 1:numel(SSol) ; Xf = 1:numel(XfSol); V = 1:numel(VSol); Ncell = 1:numel(NcellSol);
FB = ((V/1000)*(7.14/YXN)*(muS +muN)*Xf) + (1.59*betaCmax*Xf);
D = (FB + FS)/V;
muS = muMax .* (N/(KN +N)) .* (S/(KS+S)) .* (1/(1+(S/KiS))) .* (1/(1+(Xf/KiX)));
muN = muMax .*(1/(1+(N/KiN))) .* (Ncell/((1/(10.*YXN))+Ncell)) .* (S./(KS+S)) .* (1./(1+(S/KS))).* (1./(1+(Xf./KiS)));
mu = muS + muN;
dXfdt = mu *Xf - (D - (Vevap/V))*Xf;
end
end
Every function I have has a for loop to use other functions. I just dont understand how I can make these work, becuase there isnt one main function.
Steven Lord
Steven Lord 2021-7-29
So it sounds like you don't have a hundred independent ODEs but you have a system of a hundred interdependent ODEs. In that case you can't solve them each on their own. See the "Nonstiff Euler Equations" example on this documentation page for an example involving a system of three interdependent ODEs. The ODE function will be (much) longer with a hundred ODEs but the principle is the same.

请先登录,再进行评论。

类别

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

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by