ode45 For Three Functions
Mostrar comentarios más antiguos
Hello! I am trying to run a code that will use ode45, but I have three functions. I do not know how to write a code to combine all three. The code I have written is shown below.
I'm also hoping you can give me details as to where in a file it should be - such as if I should combine the functions into one script or what. Each of my functions is a separate function script and the main code is its' own script as well.
First function
function dHdt = hive(t,H)
% This function is used to model the population changes in the hive bees
L = 1780; % Maximum egg laying rate of queen bee
omega = 27000; % Brood mortality
alpha = 0.15; % Maximum rate at which hive bees become foragers
sigma = 0.75; % social inhibition
ro = 0.000005; % death rate due to mites
% H = population of hive bees
% F = population of forager bees
% M = population of mites
dHdt = (L*(H+F))/(omega+H+F) - H*(alpha-sigma*(F/H+F))-ro*M*H;
end
Second Function
function dFdt = forager(t,F)
% This function is used to calculate the changes in the forager bee
% population
alpha = 0.15; % Maximum rate at which hive bees become foragers
sigma = 0.75; % Social inhibition
m = 0.12; % Per capita deat rate of foragers
ro = 0.000005; % Death rate due to mites
% H = population of hive bees
% F = Population of forager bees
% M = population of mites
dFdt = H*(alpha-sigma*(F/H+F))-m*F-ro*M*F;
end
Third Function
function dMdt = mite(t,M)
% This function calculates the change in the mite populations over time
r = 0.0165; % Growth rate of mites
alphat = 0.3; % Carrying capacity of mites in the hive
rotwo = 0.016; % Per capita death rate of mites
% M = Population of mites
% H = Population of hive bees
% F = Population of forager bees
dMdt = r*M*(1-M/alphat*H)-rotwo*M;
end
Main Code
% What will happen to the population of bees in 12 months?
Hinitial = 14000; % Initial Population of Hive Bees
Finitial = 7000; % Initial population of forager bees
Minitial = 400; % Initial population of mites
tspan = [0,12]; % Timespan over which the data will be calculated
[T, H, F, M] = ode45(@hive, @forager, @mite, tspan, Hinitial, Finitial, Minitial);
figure
plot(T,H)
xlabel('Time (Years)')
ylabel('Number of Hive Bees')
plot(T,F)
xlabel('Time (Years)')
ylabel('Number of Forager Bees')
plot(T,M)
xlabel('Time (Years)')
ylabel('Number of Mites')
I need the ability to make it into a plot that shows all three populations as well.
Thank you!
Respuesta aceptada
Más respuestas (1)
James Tursa
el 9 de Nov. de 2020
You made a good start, but you need to solve these differential equations simultaneously. That will mean you need to work with a state vector. Using the nomenclature of the ode45 doc, call this state vector y. Since you have three variables of interest (H, F, M), that will mean a 3-element state vector. So define
y(1) = H
y(2) = F
y(3) = M
Then define your derivative function in terms of y. You could combine all three of your current functions into one function, or call all three functions individually and combine the results. To keep changes to your current code at a minimum since you have already written the three individual derivative functions, I will show you the latter approach. So,
Step 1:
Change your function signatures to pass in H, F, and M from y to use in your derivative code since all three functions depend on all three of these variables. E.g.,
function dHdt = hive(t,H,F,M)
etc.
function dFdt = forager(t,H,F,M)
etc.
function dMdt = mite(t,H,F,M)
etc.
Step 2:
In your main code, create a function handle with a (t,y) signature that passes in the H, F, M from the 3-element y vector and returns a 3-element derivative vector. E.g., since we have defined y(1)=H, y(2)=F, and y(3)=M, this would result in:
f = @(t,y) [hive(t,y(1),y(2),y(3));forager(t,y(1),y(2),y(3));mite(t,y(1),y(2),y(3))]
Step 3:
In your main code, call ode45 with the f function handle and with a vector initial condition. E.g.,
[T, Y] = ode45(f, tspan, [Hinitial;Finitial;Minitial]);
Then pick off the appropriate columns of Y for the H, F, and M solutions.
Categorías
Más información sobre MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


