Ermias Atnafu Math Assignment (2)
Ermias Atnafu Math Assignment (2)
Ermias Atnafu Math Assignment (2)
BY - ERMIAS ATNAFU
Student number – 3191656
1
Wilson-Cowan: A model of activity of neural populations
1. Write a matlab function WC.m so that the call f=WC(t,X) is the evaluation of (1) and f = (E0 , I0 ) is a column
vector.
Matlab Code:
function f = WC(t, X)
% Parameters
c1 = 20;
c2 = 11.5;
c3 = 15;
c4 = 1;
P = 2;
Q = 1;
bE = 1.3;
bI = 2;
thetaE = 4;
thetaI = 3.7;
2. Write a matlab function JACWC.m so that the call A=JACWC(t,X) returns the Jacobian matrix fX. We expect you
to use the result from Tutorial 1.
%% Exercise 2
function A = JACWC(t, X, h)
% JACWC: Compute the numerical Jacobian matrix for the Wilson-Cowan system
% Inputs:
2
% X - State vector [E; I] where E and I are the excitatory and inhibitory populations
% Output:
for i = 1:n
end
end
%% Example Usage
A = JACWC(t, X, h);
3
% Display the numerical Jacobian matrix
disp('Numerical Jacobian:');
disp(A);
% J = [dE/dE, dE/dI;
% dI/dE, dI/dI]
theta_e = 4; be = 1.3;
theta_i = 3.7; bi = 2;
P = 2; Q = 1;
c1 = 20; c2 = 11.5;
c3 = 15; c4 = 1;
dIdE, dIdI];
4
3. The equations dE/dt = 0 and dI/dt = 0 define the nullclines. On the nullclines, solution curves turn direction, i.e.
up/right ↔ down/left. Since E0 = 0 involves only one term with I, we can write I as a function of E. Plot the
resulting curves. (1.5pt) Alternative to finding an analytical expression for the nullclines, you may consider a
root finding method. In this case, for every fixed value of E, find I such that E0 = 0, and do this for a suitable
range of values for E.
MATLAB CODE :
% Parameters
b_e = 1.3; b_i = 2; % Sigmoid slopes
theta_e = 4; theta_i = 3.7; % Sigmoid thresholds
c1 = 20; c2 = 11.5; % Coupling constants for E
c3 = 15; c4 = 1; % Coupling constants for I
P = 2; Q = 1; % Inputs
% Define nullclines
I_E = @(E) (log(1 ./ (E ./ (1 - E) + exp_e) - 1) ./ b_e + c1 * E + P - delta_e) / c2;
E_I = @(I) (-log(1 ./ (I ./ (1 - I) + exp_i) - 1) ./ b_i + c4 * I - Q + delta_i) / c3;
% Compute nullclines
I_e = I_E(E); % Excitatory nullcline
E_i = E_I(I); % Inhibitory nullcline
% Legend
legend('Excitatory Nullcline', 'Inhibitory Nullcline', 'Location', 'southeast');
hold off;
5
4. Find all equilibria of the Wilson-Cowan model for these parameter values numerically.(1pt)
MATLAB CODE :
% Parameters
b_e = 1.3; b_i = 2; % Sigmoid slopes
theta_e = 4; theta_i = 3.7; % Sigmoid thresholds
c1 = 20; c2 = 11.5; % Coupling constants for E
c3 = 15; c4 = 1; % Coupling constants for I
P = 2; Q = 1; % Inputs
for i = 1:size(initial_guesses, 1)
[eq_point, fval, exitflag] = fsolve(nullcline_system, initial_guesses(i, :), options);
if exitflag > 0
equilibrium_points = [equilibrium_points; eq_point];
end
end
for i = 1:size(possible_eq_points, 1)
X = possible_eq_points(i, :);
count = 0;
while true
% Compute function and Jacobian
F = [E_prima(X(1), X(2)); I_prima(X(1), X(2))];
Jac = [dEdE(X(1), X(2)), dEdI(X(1), X(2));
dIdE(X(1), X(2)), dIdI(X(1), X(2))];
% Check convergence
7
if norm(F) < tol
is_new_point = true;
if ~isempty(eq_points)
for k = 1:size(eq_points, 1)
if norm([X(1) X(2)] - eq_points(k, :)) < tol
is_new_point = false;
break;
end
end
end
if is_new_point
eq_points = [eq_points; [X(1) X(2)]];
end
break;
end
if count > 15000
break;
end
count = count + 1;
end
end
disp(eq_points)
Result: [0.1541 0.1921; 0.4727 0.4997 ; 0.4346 0.4995] these are the equilibrium points.
5. • Determine the linearization for each fixed point. Classify the fixed points based on the eigenvalues. Make local
phase portraits of the linearization x 0 = Df(E0, I0)x in separate figures.
MATLAB CODE:
eig_vals = [];
for i = 1:length(eq_points)
a = Jac(1, 1);
b = Jac(1, 2);
c = Jac(2, 1);
d = Jac(2, 2);
% Compute eigenvalues
else
end
elseif imag(eigenvalue_1) ~= 0
fprintf('The fixed point [%f, %f] is a Stable Spiral Focus.\n', X(1), X(2));
fprintf('The fixed point [%f, %f] is an Unstable Spiral Focus.\n', X(1), X(2));
else
end
end
end
Result:
6. Make a global phase portrait for a relevant part of the unit square 0 ≤ E, I ≤ 1. Your figure should contain the
nullclines, the equilibria and a few solutions (orbits). Create these solutions by simulating the model with the
Euler-Forward method and use at most 10 initial conditions. For the equilibria with real eigenvalues, also plot
the lines given by X(s) = x0+sv with v an eigenvector. Provide a short description of this figure and comment on
the attractors, i.e., where do the orbits go to?
Matlab code:
9
% Parameters
dt = 0.01; % Time step for Euler - Forward method
t_end = 10; % Simulation duration
t_steps = t_end / dt;
% Simulate Orbits
initial_conditions = [0.2, 0.2; 0.8, 0.2; 0.2, 0.8; 0.8, 0.8;
0.5, 0.5; 0.1, 0.9; 0.9, 0.1; 0.4, 0.6;
0.6, 0.4; 0.5, 0.3; 0.3, 0.5; 0.2, 0.1;
0.1, 0.2; 0.9, 0.5; 0.5, 0.9; 0.7, 0.8;
0.8, 0.7];
orbits = cell(size(initial_conditions, 1), 1);
for i = 1:size(initial_conditions, 1)
E = zeros(1, t_steps);
I = zeros(1, t_steps);
E(1) = initial_conditions(i, 1);
I(1) = initial_conditions(i, 2);
for t = 1:t_steps-1
dE = E_prima(E(t), I(t));
dI = I_prima(E(t), I(t));
E(t+1) = E(t) + dE * dt;
I(t+1) = I(t) + dI * dt;
end
orbits{i} = [E; I];
end
% Compute nullclines
I_E = @(E) (log(1 ./ (E ./ (1 - E) + exp_e) - 1) ./ b_e + c1 * E + P - delta_e) / c2;
E_I = @(I) (-log(1 ./ (I ./ (1 - I) + exp_i) - 1) ./ b_i + c4 * I - Q + delta_i) / c3;
E_zero = [E_range', I_E(E_range)']; % Excitatory nullcline
I_zero = [E_I(I_range)', I_range']; % Inhibitory nullcline
% Plotting
figure;
hold on;
plot(E_zero(:, 1), E_zero(:, 2), 'b-', 'LineWidth', 2, 'DisplayName', 'Excitatory Nullcline');
plot(I_zero(:, 1), I_zero(:, 2), 'r--', 'LineWidth', 2, 'DisplayName', 'Inhibitory Nullcline');
% Plot Equilibria
10
scatter(eq_points(:, 1), eq_points(:, 2), 50, 'k', 'DisplayName', 'Equilibria');
% Plot Orbits
for i = 1:length(orbits)
orbit = orbits{i};
plot(orbit(1, :), orbit(2, :), 'k', 'HandleVisibility', 'off');
end
% Plot Quiver
quiver(E_grid, I_grid, E_prime, I_prime, 'Color', [210/255, 180/255, 140/255], 'LineWidth', 1,
'MaxHeadSize', 0.5, 'DisplayName', 'Vector Field');
% Final touches
xlabel('E');
ylabel('I');
xlim([0 1]);
ylim([0 1]);
title('Global Phase Portrait');
legend show;
grid on;
hold off;
11
Figure Description
The output plot, titled final plot.jpg, presents a global phase portrait for the Wilson-Cowan model within the
bounds of (0 \leq E, I \leq 1). The plot includes the following elements:
Nullclines: The nullclines for the excitatory and inhibitory populations are depicted, indicating where the
derivatives (E') and (I') are zero. These curves are shown between 0 and 0.5, as only the non-imaginary values
are plotted.
Equilibria: The fixed points identified in Exercise 4 are marked as black circles, representing the system's
equilibrium points.
Eigenvector Lines: For fixed points with real eigenvalues, lines representing the eigenvectors are drawn,
illustrating the directions of the local phase space near each equilibrium.
Orbits: The trajectories for various initial conditions are plotted, showing the system's time evolution from
different starting points.
3. Write a report about the methods you have used and the results obtained. Motivate your choice for the algorithms,
step sizes, tolerances and other relevant parameters. Comment in detail on the results obtained. Do they make sense,
and are they accurate? Also, hand in the working code (in electronic form) that you have used to obtain your results.
We should not need to debug your code. There should be a single main-script that reproduces all your results. Call it
main with your initials as a suffix, e.g., I would write main HGEM. The code should be readable, i.e. contain
documentation. It should be clear what you do at every step by choosing self-explanatory names for your variables and
referencing algorithms in the book or lectures. If parts of your solution use the symbolic toolbox or commands such as
fsolve, eig or equivalent performing the numerical work in a ”black box”, this part will not be given credit. While we
check the code, ideally, the report should be self-contained.
12
The nullclines are plotted for a range of values of E and I, and they help visualize the locations of equilibrium points
where both derivatives are zero. The equilibria are found by solving these nullcline equations for E and I.
2.4. Equilibrium Points
Equilibrium points are the points where the system's trajectory reaches a steady state, i.e., where both dE/dt and dI/dt
are zero. These points are identified by solving the nullcline equations and locating their intersections. At these points,
the vector field is zero, and the system's behavior depends on the nature of the equilibrium (e.g., stable or unstable).
We assume that the equilibrium points are stable if the surrounding vector field tends to push the system back toward
the equilibrium, and unstable if the vector field pushes the system away.
2.5. Local Phase Portraits
Local phase portraits show the behavior of the system in the vicinity of equilibrium points. They are created by
simulating the system's trajectories for different initial conditions close to an equilibrium point and observing how the
system evolves over time. These portraits provide information on the stability of the equilibrium and the system's
dynamic behavior.
In our approach, the vector field is plotted near equilibrium points, with the vectors normalized to show the direction of
the system's evolution. The local phase portraits can be used to classify the equilibrium points as nodes, saddles, or foci,
based on the system's dynamics.
2.6. Global Phase Portraits
The global phase portrait provides a complete overview of the system's behavior over the entire state space. It is
created by simulating the system from various initial conditions and plotting the trajectories in the phase plane. This
helps in understanding the global behavior of the system, including the flow patterns, the influence of equilibrium
points, and the overall stability of the system.
The quiver plot is used to visualize the vector field, where the length and direction of the vectors indicate the rate of
change of E and I. In our implementation, the vectors are normalized and scaled to avoid overlapping or excessively long
arrows, especially near equilibrium points.
3. Results
The following results were obtained through simulation and visualization:
Nullclines: The nullclines for E and I were computed and plotted. The curves intersect at equilibrium points,
where the system's derivatives are zero.
Equilibrium Points: The equilibrium points were identified at the intersections of the nullclines. These points
correspond to steady states in the system where no change occurs.
Local Phase Portraits: Local phase portraits were generated near the equilibrium points to analyze the system's
stability. The behavior of the system near equilibrium points was consistent with the expected dynamics based
on the chosen functions.
Global Phase Portrait: A global phase portrait was constructed by simulating the system from various initial
conditions. The resulting plot showed the trajectories in the phase plane and provided a clear visual
representation of the system's behavior, including the flow patterns and the influence of equilibrium points.
The vector field was normalized and scaled to prevent overly large vectors, and its behavior near the equilibrium points
indicated that the system slows down and the vectors decrease in magnitude as expected.
4. Discussion
The results obtained make sense based on the model's structure and the chosen numerical methods. The equilibrium
points are correctly identified, and the nullclines intersect as expected. The local phase portraits show the expected
behavior near the equilibrium points, and the global phase portrait provides a clear overview of the system's dynamics.
The Euler method is adequate for this simulation, given the relatively simple nature of the system and the small time
step used. While more sophisticated methods (such as Runge-Kutta) could provide higher accuracy, the Euler method
strikes a balance between simplicity and computational efficiency for this task.
The choice of parameters, such as the time step (dt = 0.01), was made to ensure smooth trajectories without causing
excessive computational cost. The step size was small enough to accurately capture the system's dynamics while
maintaining reasonable computational time.
5. Conclusion
This study successfully simulates and visualizes the phase portrait of a two-variable dynamical system. The methods
used, including the Euler method for numerical integration and quiver plots for the vector field, were effective in
representing the system's behavior. The results obtained were consistent with expectations, and the model provides
valuable insights into the dynamics of the system.
13
NB. I have used a combination of Copilot and Chatgpt to improve my code and also work through some codes that
are relatively black box.
14