Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

comsol script

Please login with a confirmed email address before reporting spam

Hi all!

I'm quite new to comsol scripting. I'm trying to simulate the growth of bacteria within a porous media, To do so, I need to use an expression like this:

X(t+1)=X(t)+growth(X(t))

were H(t) is the amount of bacteria at the current time,
growth(H(t)) is the mass of bacteria that has grown in tim t.
H(t+1) shold be the amount of bacteria at the next timestep.

I don't think this can be done with the COMSOLGUI, so I've been trying to do it in matlab, with little success. Could someone givme a hand with this?

Thanks a lot

2 Replies Last Post 18.10.2011, 06:01 GMT-4

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 16.10.2011, 13:10 GMT-4
Hi Roger,

if you want to use a script, it should look somehow like this:

% define growth function and save as growth.m, let's say bacteria double in every time step
function G=growth(x)
G=2*x;
end

% setup simple script for the calculation of the population in every time step and save as dynamic.m
H(1) = 1; % set initial value
dt = 0.001; % set time increment
N = 1000; % set number of time steps
for i = 1:N
H(i+1) = H(i) + dt*growth(H(i));
end

Once you have these two files, you may run your calculation by the command 'dynamic' and plot the solution with the command 'plot(H)'. Even though the upper formulation is much more adjustable, COMSOL can handle these equations because you can rewrite your step-by-step formulation into an ordinary differential equation given as

dX/dt = growth(X).

Define a growth-function in COMSOL and enter this equation as global equation, the result should be the same.
I hope this helps,
Alex



Hi Roger, if you want to use a script, it should look somehow like this: % define growth function and save as growth.m, let's say bacteria double in every time step function G=growth(x) G=2*x; end % setup simple script for the calculation of the population in every time step and save as dynamic.m H(1) = 1; % set initial value dt = 0.001; % set time increment N = 1000; % set number of time steps for i = 1:N H(i+1) = H(i) + dt*growth(H(i)); end Once you have these two files, you may run your calculation by the command 'dynamic' and plot the solution with the command 'plot(H)'. Even though the upper formulation is much more adjustable, COMSOL can handle these equations because you can rewrite your step-by-step formulation into an ordinary differential equation given as dX/dt = growth(X). Define a growth-function in COMSOL and enter this equation as global equation, the result should be the same. I hope this helps, Alex

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 18.10.2011, 06:01 GMT-4
Thank you very much Alex, your answer has been of great help!

Actually, in my model, I defined the growth function using an ODE in comsol, just as you said at the end of your post:

growth_H is defined as dH/dt=a*H (were a is related to the concentration of a nutrient, which also varies with time)

Then, should the external matlab script look something like this?

function H=bacteria(growth_H)
H(0)=1;
dt=3600;
T=86400*10;
for i=0:dt:T
H(i+1)=H(i)+growth_XH(i);
end

Is it correct to call the comsol ODE value like growth_XH from Matlab?
Also, in comsol, I should call the script from functions/MATLAB and add the function bacteria(growth_H), right?


Thank you again
Thank you very much Alex, your answer has been of great help! Actually, in my model, I defined the growth function using an ODE in comsol, just as you said at the end of your post: growth_H is defined as dH/dt=a*H (were a is related to the concentration of a nutrient, which also varies with time) Then, should the external matlab script look something like this? function H=bacteria(growth_H) H(0)=1; dt=3600; T=86400*10; for i=0:dt:T H(i+1)=H(i)+growth_XH(i); end Is it correct to call the comsol ODE value like growth_XH from Matlab? Also, in comsol, I should call the script from functions/MATLAB and add the function bacteria(growth_H), right? Thank you again

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.