MATLAB Assignment Removed
MATLAB Assignment Removed
### Task 01
%%
% Parameters
r = 0.5; % Growth rate
K = 80; % Carrying capacity
P0 = 30; % Initial population
tspan = [0 20]; % Time span
logistic_model = @(t, P) r * P * (1 - P/K); % Logistic Growth Model function
[t, P] = ode45(logistic_model, tspan, P0); % Solve using ode45
plot(t, P, 'r', 'LineWidth', 2); % Plot solution trajectory
xlabel('Time (t)');
ylabel('Population, P(t)');
grid on
title(sprintf('Logistic Growth Model (r = %.2f, P0 = %.2f, K = %.2f)', r, P0, K)); % Add
r, P0, and K values to the title
%%
% Parameters
r = 0.35; % Growth rate
K = 40; % Carrying capacity
tspan = [0 30]; % Time span
logistic_model = @(t, P) r * P * (1 - P/K); % Logistic Growth Model function
figure;
hold on;
i = 1;
for P0 = 5:10:60
[t, P] = ode45(logistic_model, tspan, P0); % Solve ODE for logistic growth
plot(t, P, 'LineWidth', 1.5); % Plot the solution trajectory
21
xlabel('Time (t)');
ylabel('Population, P(t)');
hold on;
22
### Task 02
% Define parameters
r = 1.0; % Growth rate
K = 220; % Carrying capacity
P0 = 10; % Initial population
N = 100; % Number of iterations
logistic = @(P, r, K) P + r * P * (1 - P / K); % Define the logistic map function
P = zeros(1, N+1);
P(1) = P0;
for n = 1:N
P(n+1) = logistic(P(n), r, K);
end
figure;
%plot(0:N, P, '-o');
plot(0:N,P)
xlabel('Time step (n)');
23
ylabel('Population (P)');
title(['Logistic map trajectory for r = ' num2str(r) ', K = ' num2str(K) ', P0 = '
num2str(P0)]);
grid on;
% Find fixed points
syms P_fixed;
eqn = P_fixed == logistic(P_fixed, r, K);
fixed_points = solve(eqn, P_fixed);
% Analyze stability: Calculate derivative of the logistic map function
d_logistic = diff(logistic(P_fixed, r, K), P_fixed);
% Evaluate derivative at the fixed points
stability = double(subs(d_logistic, P_fixed, fixed_points));
% Display fixed points and their stability
for i = 1:length(fixed_points)
fprintf('Fixed point: %f\n', double(fixed_points(i)));
if abs(stability(i)) < 1
fprintf('Stability: Stable (|f''(P)| < 1)\n');
else
fprintf('Stability: Unstable (|f''(P)| >= 1)\n');
end
end
% Analyze dynamics by varying r
figure;
hold on;
colors = lines(5);
for r = 0.5:0.5:2.5
P(1) = P0;
for n = 1:N
P(n+1) = logistic(P(n), r, K);
end
plot(0:N, P, '-o', 'Color', colors(round(r/0.5), :));
end
xlabel('Time step (n)');
ylabel('Population (P)');
title('Logistic map trajectory for varying r');
legend('r = 0.5', 'r = 1.0', 'r = 1.5', 'r = 2.0', 'r = 2.5');
grid on;
hold off;
24
### Task 02 Output
### Task 03
25
%% 3(a)
% Parameters
alpha=2/3; % Growth rate of elk
beta=4/3; % Predation Rate Coefficient
delta=1; % Predation Efficiency Coefficient
gamma=0.1; % Death rate of wolves
tspan = [0 400];
y0 = [0.9 0.9];
odefun=@(t,y) [alpha.*y(1)-beta.*y(1).*y(2); delta.*y(1).*y(2)-gamma.*y(2)];
%trajectory
[t, sol]=ode45(odefun,tspan,y0);
p=plot(t,sol(:,1),'r',t,sol(:,2),'b');
set(p,'Linewidth',2)
legend('Elk population','Wolf population');
%% Stability analysis
syms x y sigma
f=@(x,y) alpha.*x-beta.*x.*y;
g=@(x,y) delta.*x.*y-gamma.*y;
[u,v]=solve(f(x,y)==0,g(x,y)==0,[x,y]); % finding fixed points
cp(x,y) = charpoly(jacobian([f(x,y),g(x,y)],[x,y]),sigma); % characteristic polynomial
for i=1:2
sln=solve(cp(u(i),v(i))==0,sigma) % finding eigenvalues
if (real(sln)==0 & imag(sln)~=0)
z=[u(i) v(i)]
fprintf('Stable Centre')
elseif (real(sln)<0)
z=[u(i) v(i)]
fprintf('Asym stable.')
elseif (real(sln)>0)
z=[u(i) v(i)]
fprintf('Unstable.')
elseif (sln(1)<0 & sln(2)>0 | sln(1)>0 & sln(2)<0)
z=[u(i) v(i)]
fprintf('saddle')
end
end
26
%% Phase plane
for a=0.9:0.1:1.8
for b=0.9:0.1:1.8
y0=[a b];
hold on
[t, sol]=ode45(odefun,tspan,y0);
p1=plot(sol(:,1),sol(:,2));
end
end
### Task 03(a) Output
27
### Task 03(b)
% Parameters
alpha=2/3; % Growth rate of elk
beta=4/3; % Predation Rate Coefficient
delta=1; % Predation Efficiency Coefficient
gamma=0.1; % Death rate of wolves
lambda=0.2; % Competition coefficient for elk
kappa=0.05; % Competition coefficient for wolves
tspan = [0 400];
y0 = [0.9 0.9];
odefun=@(t,y) [alpha.*y(1)-lambda.*y(1)^2-beta.*y(1).*y(2); delta.*y(1).*y(2)-
gamma.*y(2)-kappa.*y(2)^2];
%trajectory
[t, sol]=ode45(odefun,tspan,y0);
p=plot(t,sol(:,1),'r',t,sol(:,2),'b');
set(p,'Linewidth',2)
legend('Elk population','Wolf population');
%% Stability analysis
syms x y sigma
f=@(x,y) alpha.*x-lambda.*x^2-beta.*x.*y;
g=@(x,y) delta.*x.*y-lambda.*y-kappa.*y^2;
[u,v]=solve(f(x,y)==0,g(x,y)==0,[x,y]); % finding fixed points
cp(x,y) = charpoly(jacobian([f(x,y),g(x,y)],[x,y]),sigma); % characteristic polynomial
for i=1:2
sln=solve(cp(u(i),v(i))==0,sigma) % finding eigenvalues
if (real(sln)==0 & imag(sln)~=0)
z=[u(i) v(i)]
fprintf('Stable Centre')
elseif (real(sln)<0)
z=[u(i) v(i)]
fprintf('Asym stable.')
elseif (real(sln)>0)
z=[u(i) v(i)]
fprintf('Unstable.')
elseif (sln(1)<0 & sln(2)>0 | sln(1)>0 & sln(2)<0)
z=[u(i) v(i)]
fprintf('saddle')
end
28
end
%% Phase plane
for a=0.9:0.1:1.8
for b=0.9:0.1:1.8
y0=[ a b];
hold on
[t, sol]=ode45(odefun,tspan,y0);
p1=plot(sol(:,1),sol(:,2));
end
end
### Task 03(b) Output
29
### Task 04
% Parameters
a = 2; b = 0.5; c = 0.5; d = 0.5; e = 1; f = 1; g = 1;
y0 = [1 1 1]; % initial conditions for x1, x2, y
tspan = [0 50];
odefun=@(t,y) [a.*y(2)-b.*y(1)-c.*y(1);b.*y(1)-d.*y(2)-e.*y(2)*y(3);-f.*y(3)+g.*y(2)*y(3)];
% deriving the model
[t, y] = ode45(odefun, tspan, y0); % solving the model
plot(t, y(:,1), 'r');
hold on;
plot(t, y(:,2), 'b');
hold on;
plot(t, y(:,3), 'g');
xlabel('Time');
ylabel('Population');
legend('x1(t) - Young','x2(t) - Adults','y(t) - Predators');
title('Predator-Prey Model with Child Care');
hold off;
30