Using the euler method

조회 수: 6 (최근 30일)
Kelsey Pettrone
Kelsey Pettrone 2020년 9월 21일
댓글: James Tursa 2020년 9월 22일
Hi something is wrong with my for loop i have no idea what to do
% write a program that will explore population dynamics
% The population dynamics of a community can be described by:
% dp/dt = G(Pmax - P(t))*P(t)
% Use Euler's Method to determine the population (") as a function of time (#)
% Plot the population (") as a function of time (#) from t = 0 to t = 20 years
% Use the following values for constants in the differential equation:
% pmax = 10,000
% G = 0.00005
% Use an initial condition of p = 800 when t=0
%% set perameters
pmax = 10000
G = 0.00005
tmin = 0
tmax = 20
Nx = 50
t = linspace(tmin, tmax, Nx)
dt = t(1)-t(0)
p = zeros(1,Nx)
p(0) = 800
%% calculate p values using euler method
p(1) = G*(pmax - p(0))*p(0)*dt+p(0)
for ip = 1:Nx
p(ip+1) = G*(pmax - p(t(ip))*p(t(ip))*dt+p(t(ip))
end

답변 (2개)

Geoff Hayes
Geoff Hayes 2020년 9월 21일
Kelsey - there are a couple of coding mistakes with the above. Before the code can execute, there is an error
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
with the line
p(ip+1) = G*(pmax - p(t(ip))*p(t(ip))*dt+p(t(ip))
becaue there is a missing closing bracket. The above line should be
p(ip+1) = G*(pmax - p(t(ip))*p(t(ip))*dt+p(t(ip)));
Next, there is the error
Array indices must be positive integers or logical values.
with the line
dt = t(1)-t(0)
As the error message indicates, positive integers or logical values must be used as indices. 0 is not a positive integer, so you if you want to find the delta, then just do
dt = t(2)-t(1)
Similarly, you must change
p(0) = 800
%% calculate p values using euler method
p(1) = G*(pmax - p(0))*p(0)*dt+p(0)
for ip = 1:Nx
to
p(1) = 800
%% calculate p values using euler method
p(2) = G*(pmax - p(1))*p(1)*dt+p(1)
for ip = 2:Nx
The final problem will be with
p(ip+1) = G*(pmax - p(t(ip))*p(t(ip))*dt+p(t(ip)));
and the error
Array indices must be positive integers or logical values.
because of p(t(ip)) where you are using the vaues of t - which are not integers - as indices into p. What is this code trying to do here?
  댓글 수: 1
Kelsey Pettrone
Kelsey Pettrone 2020년 9월 21일
just to take the formula that was given and use eulers method and a for loop to find the population from 1 to 20 years

댓글을 달려면 로그인하십시오.


James Tursa
James Tursa 2020년 9월 21일
편집: James Tursa 2020년 9월 21일
p(ip) is the value of p at time t(ip). p isn't a function that you are passing time into like you are doing with p(t(ip)). So get rid of that t(ip) subscripting and simply use ip subscripting instead. E.g.,
p(ip+1) = G*(pmax - p(ip))*p(ip)*dt + p(ip);
Also, the initial condition is p = 800, and the initial value is p(1), so replace that p(1) line with simply
p(1) = 800;
and get rid of the p(0) = 800 line since 0 is not a valid subscript.
Which also means that the dt calculation should be this:
dt = t(2) - t(1);
  댓글 수: 4
Kelsey Pettrone
Kelsey Pettrone 2020년 9월 21일
and i think it might be this
p(ip) = G*(pmax - p(ip-1))*p(ip-1)*dt + p(ip-1);
with -1 but im not sure
would this make sense?
James Tursa
James Tursa 2020년 9월 22일
This depends on how you do the for-loop indexing. If the for-loop index starts at 1, then you use ip+1 on the lhs and ip on the rhs. If the for-loop index starts at 2, then you would use ip on the lhs and ip-1 on the rhs. Either way works ... personal preference.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by