Clear Filters
Clear Filters

How to terminate simulation and output timesteps to termination

1 view (last 30 days)
Hello everyone!
I am attempting to count the number of time steps it takes for a simulation to reach equilibrium, and once its there to terminate the simulation. Here, the simulation reaches equilibrium when n(t+1) = n(t) AND when S(t+1) = S(t). The following code runs the simulation until t = T, but I want it to stop when it reaches the above conditional and then I want it to output eq = t where t is the amount of time it took to reach equilibrium. Does anyone know how to make this happen, as this code is spitting out errors?
function [n, S] = simulate_dynamics(n0, S0, dt, T, A, B, C, D, beta, thetaI, thetaC, lambda, alpha, K)
eq = []
S(1) = S0;
n(1) = n0;
for t = 1:T
pStart = A + B * (S(t).^2./(S(t).^2 + thetaI.^2));
pStop = C + D * (1 ./ (1 + exp(beta*(S(t)-thetaC))));
n(t+1) = n(t) + dt * (pStart * (1 - n(t)) - pStop*n(t));
S(t+1) = S(t) + dt * (lambda*S(t).^alpha - K*n(t));
% keep both variables inside [0 1]
if S(t+1) > 1
S(t+1) = 1;
end
if n(t+1) > 1
n(t+1) = 1;
end
if S(t+1) < 0
S(t+1) = 0;
end
if n(t+1) < 0
n(t+1) = 0;
end
if n(t+1) = n(t) & S(t+1) = S(t)
eq = t
return
end
end

Answers (0)

Categories

Find more on General Physics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!