I need write a function called "compute_basin" that takes in a steady state and its k value and computes which set of initial conditions in [15,30] approach that steady state. Specifically it will return two values, "a" and "b", which describe the set [a,b] of initial conditions that approach the steady state. The function will be assessed by running
[a,b]=compute_basin(steady_state,k);
for many different values of k and the associated steady states.
Inputs:
- a scalar value of a steady state
- a scalar value of k (note the steady state should be correct for the k value)
Outputs:
- a scalar for the lower bounds of the basin of attraction
- a scalar for the upper bounds for the basin of attraction
This is what i have so far:
function [a, b] = compute_basin(Ceq, k)
mid = (left + right) / 2;
tstar = compute_time_to_steady(mid, k, Ceq);
mid = (left + right) / 2;
tstar = compute_time_to_steady(mid, k, Ceq);
But unfortunalty this is not correct. I have a tip saying that "There are many ways of doing this but a binary search algorithm might be helpful (Wikipedia entry for binary searchLinks to an external site.). Make sure that the [a,b] bounds are within the range of [15,30]. Note that some steady states may be unstable and so their basin of attraction will only be their exact value. In these cases a=b. It helps to solve for the upper and lower bounds separately, i.e. running the code for a and then running it again for b."
I have acces to three functions, the first is "compute_ode" that computes the derivative dC/dt for the differential equation model dcdt = 0.1 * (c-20) * (23-c) * (c-26) + k.
function dcdt = compute_ode(t,c,k)
dcdt = 0.1 * (c-20) * (23-c) * (c-26) + k;
Then i have the second "compute_states" that takes as input a value for k and then returns as output all real-valued steady states for the differential equation.
function vectCeq = compute_states(k)
coeffs = [-0.1, 6.9, -157.8, 1196 + k];
all_roots = roots(coeffs);
real_roots = all_roots(imag(all_roots) == 0);
And latsly, I have "compute_time_to_steady" that solves the differential equation and returns the earliest time it takes to get within 1% error of the steady state value:
function tstar = compute_time_to_steady(init_cond, k, steady_state)
options = odeset('RelTol', 1e-6, 'AbsTol', 1e-9);
function dcdt = ode_function(t, c)
dcdt = compute_ode(t, c, k);
[T, C] = ode45(@ode_function, tspan, init_cond, options);
if abs(C(i) - steady_state) / steady_state < threshold
function dcdt = compute_ode(t, c, k)
dcdt = 0.1 * (c - 20) * (23 - c) * (c - 26) + k;
function steady_states = compute_states(k)
steady_states = [20; 23; 26];
I would really appriciate any help on computing the basin of attraction for a steady state!
Thank you!