Implement for loop in ode45 solver

I'm trying to vary parameter.D with a for loop so i can use the ode45 solver for different values of parameter.D
parameter.mumax = 0.42; %1/h
parameter.Km = 0.25; %g/l
parameter.Sin = 60; %g/l
parameter.Yxs = 0.25; %g/g
parameter.D = 0.1:0.1:0.5;%1/h
y0 = [ 5, 60]; %biomass substrate
tspan = [0 100]; %h
options = odeset('RelTol', 1e-6);
[t,y] = ode45(@biomassproductionmodel, tspan, y0, options, parameter);
function [dydt] = biomassproductionmodel(t,y, parameter)
mu = parameter.mumax*y(2)/(parameter.Km+y(2));
dydt(1,1) = mu*y(1) - parameter.D*y(1);
dydt(2,1) = -mu*y(1)/parameter.Yxs + parameter.D*(parameter.Sin - y(2));
end

 Accepted Answer

parameter.mumax = 0.42; %1/h
parameter.Km = 0.25; %g/l
parameter.Sin = 60; %g/l
parameter.Yxs = 0.25; %g/g
D = 0.1:0.1:0.5;
% parameter.D = 0.1:0.1:0.5;%1/h
y0 = [ 5, 60]; %biomass substrate
tspan = [0 100]; %h
options = odeset('RelTol', 1e-6);
for i=1:numel(D)
parameter.D = D(i);
[t,y] = ode45(@(t,y)biomassproductionmodel(t,y,parameter), tspan, y0, options);
T{i} = t;
Y{i} = y;
end
figure(1)
plot(T{1},Y{1}(:,1),T{2},Y{2}(:,1),T{3},Y{3}(:,1),T{4},Y{4}(:,1),T{5},Y{5}(:,1))
figure(2)
plot(T{1},Y{1}(:,2),T{2},Y{2}(:,2),T{3},Y{3}(:,2),T{4},Y{4}(:,2),T{5},Y{5}(:,2))
function [dydt] = biomassproductionmodel(t,y, parameter)
mu = parameter.mumax*y(2)/(parameter.Km+y(2));
dydt(1,1) = mu*y(1) - parameter.D*y(1);
dydt(2,1) = -mu*y(1)/parameter.Yxs + parameter.D*(parameter.Sin - y(2));
end

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Asked:

on 15 Nov 2022

Edited:

on 15 Nov 2022

Community Treasure Hunt

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

Start Hunting!