You are on page 1of 4

BME 360 - Homework #3 Submission - Tej Patel

% Define the system


dxdt = @(t, x, r) r - x - exp(-x);
% Define the range of r values
r_values = linspace(0, 3, 1000);
% Initialize arrays to store the bifurcation points
bifurcation_points = [];
stable_states = [];
% Iterate over each value of r
for r = r_values
% Define the function whose root we need to find
f = @(x) dxdt(0, x, r);

% Attempt to find the root


try
% Find the roots of the function
roots = fzero(f, [-2 2]);

% Check stability
if dxdt(0, roots, r + 1e-6) < 0 && dxdt(0, roots, r - 1e-6) > 0
stability = 1; % Stable
elseif dxdt(0, roots, r + 1e-6) > 0 && dxdt(0, roots, r - 1e-6) < 0
stability = -1; % Unstable
else
stability = 0; % Neutral
end

% Store bifurcation points and stability


bifurcation_points = [bifurcation_points; r roots];
stable_states = [stable_states; stability];
catch
% If fzero fails to find a root, skip this value of r
continue;
end
end
% Plot bifurcation diagram
scatter(bifurcation_points(:,1), bifurcation_points(:,2), 10, stable_states,
'filled');
xlabel('r');
ylabel('x');
title('Bifurcation Diagram');
BME 360 - Homework #3 Submission - Tej Patel
BME 360 - Homework #3 Submission - Tej Patel
BME 360 - Homework #3 Submission - Tej Patel

You might also like