Lab_2_The_Logistic_Equation_Live_Script
pdf
keyboard_arrow_up
School
Drexel University *
*We aren’t endorsed by this school
Course
232
Subject
Mathematics
Date
Apr 3, 2024
Type
Pages
5
Uploaded by HighnessKookabura864
Lab 2 by Jake Black
The Logistic Equation
Question 1-2
%% Let's first plot two standard logistic curves with L = ±10.
clear, clc
figure(1)
time_pts = -10 : 0.1 : 10;
for L = 10:-1:-10
r = (L + 10) / 20;
b = 1 - r;
my_color = [r, 0, b];
% plot curve w/ k = +1
y_pts = logistic(time_pts, 0, L, 1);
plot(time_pts, y_pts, 'Color'
, my_color, 'LineWidth'
, 3) % The standard curve.
grid on
, hold on
% plot curve w/ k = -1
y_pts = logistic(time_pts, 0, L, -1);
plot(time_pts, y_pts, 'Color'
, my_color, 'LineWidth'
, 3) % The standard curve.
pause(0.2)
end
set(gca, 'FontSize'
, 20)
title(
'Logistic Curves'
)
xlabel(
'time'
); ylabel(
'y(t)'
)
axis([-10 10 -10 10])
grid off
, hold off
Question 3
clear, clc
syms P(t)
r=1/2; K = 100;
% define diff. eq
DE = diff(P,t) == r*P * (1 - P/K);
% solve diff. eq w/ initital condition
sol = simplify( dsolve(DE, P(10)==50) );
1
p = matlabFunction(sol); % Use little p for the solution.
% display exact solution
sol
Question 4
% plot solution t_pts = 0:1:20;
exact = plot(t_pts, p(t_pts), 'blue'
, 'LineWidth'
, 3);
grid on
, hold on
% plot midpoint as yellow dot
midpoint = plot(10, 50, 'bo'
, 'MarkerFaceColor'
, 'yellow'
, 'MarkerSize'
, 8);
% adding title & labeling axis
title(
'Logistic Equation with r=1/2, K=100 million'
);
xlabel(
'Time in days'
);
ylabel(
'P(t) in millions'
);
% create legend
legend([exact, midpoint], 'Exact solution'
, 'Midpoint'
)
Question 5
r = 1/2; K = 100;
f = @(t, P) r * P * (1 - P/K);
% verify slope @ given point
slope = f(10,50)
Question 6
% Initialize the variables. Here we are using y for the population.
dt = 2; % Step size. Also called h in the notes.
tStart = 10; yStart = 50; % Population starts at P(10) = 50.
tEnd = 20; % Stopping time.
% Define time points and solution vector
t_points = tStart: dt: tEnd;
y_points = zeros(size(t_points)); % Use zeros as place holders for now.
% Initialize the solution at the initial condition.
y_points(1) = yStart;
% Implement Euler's method using a for loop.
N = length(t_points);
% forwards in time
2
for n = 2:N
slope = f(t_points(n-1), y_points(n-1));
y_points(n) = y_points(n-1) + slope * dt;
% add transparent yellow triangles
tri = fill([t_points(n-1), t_points(n), t_points(n)], [y_points(n-1), y_points(n-1), y_points(n)], 'y'
);
tri.FaceAlpha = 0.25; end
% plot euler points forward
euler1 = plot(t_points, y_points, 'red*:'
, 'LineWidth'
, 2); % handle = euler1
% backwards in time
t_points = tStart: -dt: 0;
for n = 2:N
slope = f(t_points(n-1), y_points(n-1));
y_points(n) = y_points(n-1) - slope * dt;
% add transparent yellow triangles
tri = fill([t_points(n-1), t_points(n), t_points(n)], [y_points(n-1), y_points(n-1), y_points(n)], 'y'
);
tri.FaceAlpha = 0.25;
end
% plot euler points backward
euler2 = plot(t_points, y_points, 'red*:'
, 'LineWidth'
, 2); % handle = euler2
% adding title & labeling axis
title(
'Logistic Equation with r=1/2, K=100 million'
);
xlabel(
'Time in days'
);
ylabel(
'P(t) in millions'
);
yticks(0:10:100);
grid on
;
hold off
;
% add 3rd entry legend
legend([exact, midpoint, euler1], 'Exact solution'
, 'Midpoint'
, 'Euler''s Method w/ h = 2'
);
Question 7
grid off
, hold on
% Initialize the variables. Here we are using y for the population.
dt = 1/2; % Step size. Also called h in the notes.
tStart = 10; yStart = 50; % Population starts at P(10) = 50.
tEnd = 20; % Stopping time.
% Define time points and solution vector
t_points = tStart: dt: tEnd;
3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
y_points = zeros(size(t_points)); % Use zeros as place holders for now.
% Initialize the solution at the initial condition.
y_points(1) = yStart;
% Implement Euler's method using a for loop.
N = length(t_points);
% forwards in time
for n = 2:N
slope = f(t_points(n-1), y_points(n-1));
y_points(n) = y_points(n-1) + slope * dt;
% add transparent green triangles
tri = fill([t_points(n-1), t_points(n), t_points(n)], [y_points(n-1), y_points(n-1), y_points(n)], 'g'
);
tri.FaceAlpha = 0.25; end
% plot euler points forward
euler1f = plot(t_points, y_points, 'k*:'
, 'LineWidth'
, 2); % handle = euler1
% backwards in time
t_points = tStart: -dt: 0;
for n = 2:N
slope = f(t_points(n-1), y_points(n-1));
y_points(n) = y_points(n-1) - slope * dt;
% add transparent green triangles
tri = fill([t_points(n-1), t_points(n), t_points(n)], [y_points(n-1), y_points(n-1), y_points(n)], 'g'
);
tri.FaceAlpha = 0.25;
end
% plot euler points backward
euler2f = plot(t_points, y_points, 'k*:'
, 'LineWidth'
, 2); % handle = euler2
% adding title & labeling axis
title(
'Logistic Equation with r=1/2, K=100 million'
);
xlabel(
'Time in days'
);
ylabel(
'P(t) in millions'
);
yticks(0:10:100);
grid on
;
hold off
;
% add 4th entry to legend
legend([exact, midpoint, euler1, euler1f], 'Exact solution'
, 'Midpoint'
, 'Euler''s Method w/ h = 2'
, 'Euler''s Method w/ h = 1/2'
);
4
Question 8-9
grid off
, hold on
% forward in time
[t_out, y_out] = ode45(f, tStart: 1: tEnd, yStart);
ode45_pts = plot(t_out, y_out, 'd'
, 'MarkerFaceColor'
, 'g'
, 'MarkerEdgeColor'
, 'r'
, 'MarkerSize'
, 10);
% backward in time
[t_backward, y_backward] = ode45(f, 10:-1:0, yStart);
ode45_pts_back = plot(t_backward, y_backward, 'd'
, 'MarkerFaceColor'
, 'g'
,
'MarkerEdgeColor'
, 'r'
, 'MarkerSize'
, 10);
grid on
;
hold off
;
% add 5th entry legend
legend([exact, midpoint, euler1, euler1f, ode45_pts], 'Exact solution'
, 'Midpoint'
, 'Euler''s Method w/ h = 2'
, 'Euler''s Method w/ h = 1/2'
, 'ode45'
);
Question 10
% add 2 legend arguments to move legen position
legend([exact, midpoint, euler1, euler1f, ode45_pts], 'Exact solution'
, 'Midpoint'
, 'Euler''s Method w/ h = 2'
, 'Euler''s Method w/ h = 1/2'
, 'ode45'
, 'Location'
, 'southeast'
);
5