How to solve an ODE calculating the value of a variable inside a loop?

1 visualización (últimos 30 días)
Hello, I am having some issues including my dummy variable Xv inside the loop. I want to predict Xv at every point of Tspan.
My script:
%% Initialization
clear; close all; clc
%% User-defined decisions
% Initial conditions
y(1) = 3 ; %Bioreactor volume, L
y(2) = 1000 ; %Dummy variable to include in loop
x0 = [y] ;
%Feeding Profile/Time spam
tspan = [0 10 10.01 20 20.01 30 30.1 40 ] ; %Sampling/Feeding Events
F_glc = [0 2 0 5 0 1 0 0 ] ; %Glucose feed (L/h)
F_out = [0 0.04 0 0.04 0 0.04 0 0 ] ; %Sampling (L/h)
system.F_glc =F_glc' ;
system.F_out =F_out' ;
% Define initial empty arrays for storing solutions and time points
Xs = [];
Ts = [];
%Loop at each sampling/feeding point
for i = 1:(length(tspan)-1)
% Define parameters for current timespan
system.F_glc = F_glc(i);
system.F_out = F_out(i);
% Compute solutions
[T,X] = ode45(@(t,x) bioreactor(t,x,system), [tspan(i) tspan(i+1)], x0);
% Set initial point for next iteration
x0 = X(end);
% Save solutions
Ts = [Ts; T];
Xs = [Xs; X];
end
% Create figure
figure
plot(Ts,Xs(:,1),'.')
------
%my function
function dy=bioreactor(t,y,system)
%% State Variables
% Unwrap variables
F_glc = system.F_glc;
F_out = system.F_out;
V = y(1);
Xv = y(2);
%Volume of Bio
dVdt = F_glc - F_out;
%Dummny variable Test
dXvdt = Xv*F_glc ;
dy =[dVdt; dXvdt] ;
end
I am getting the folliwing error:Index exceeds the number of array elements (1)
Error in Testfeeding>bioreactor (line 52)
Xv = y(2);
I want to calculate Xv at the same points of dVdt

Respuesta aceptada

Star Strider
Star Strider el 19 de Oct. de 2021
The problem is:
x0 = X(end);
so the error only occurs after the first iteration.
The solution is:
x0 = X(end,:)
and thin it works!
% Initial conditions
y(1) = 3 ; %Bioreactor volume, L
y(2) = 1000 ; %Dummy variable to include in loop
x0 = [y] ;
%Feeding Profile/Time spam
tspan = [0 10 10.01 20 20.01 30 30.1 40 ] ; %Sampling/Feeding Events
F_glc = [0 2 0 5 0 1 0 0 ] ; %Glucose feed (L/h)
F_out = [0 0.04 0 0.04 0 0.04 0 0 ] ; %Sampling (L/h)
system.F_glc =F_glc' ;
system.F_out =F_out' ;
% Define initial empty arrays for storing solutions and time points
Xs = [];
Ts = [];
%Loop at each sampling/feeding point
for i = 1:(length(tspan)-1)
% Define parameters for current timespan
system.F_glc = F_glc(i);
system.F_out = F_out(i);
% Compute solutions
[T,X] = ode45(@(t,x) bioreactor(t,x,system), [tspan(i) tspan(i+1)], x0);
% Set initial point for next iteration
x0 = X(end,:);
% Save solutions
Ts = [Ts; T];
Xs = [Xs; X];
end
% Create figure
figure
plot(Ts,Xs(:,1),'.')
% ------
%my function
function dy=bioreactor(t,y,system)
%% State Variables
% Unwrap variables
F_glc = system.F_glc;
F_out = system.F_out;
V = y(1);
Xv = y(2);
%Volume of Bio
dVdt = F_glc - F_out;
%Dummny variable Test
dXvdt = Xv*F_glc ;
dy =[dVdt; dXvdt] ;
end
.
  5 comentarios

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by