Trying to use central difference scheme to solve logistic equation
Show older comments
I'm trying to use the central difference scheme to approximate a solution to the Logistic Differential equation:
To get this, I'm essentially trying to write a code to calculate the following

Here, h is the time step. I want to pre define
and
, and then run this equation from 2 to some n, but I keep getting this error :
Error using indexing
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function
arguments must be symbolic variables, and function body must be sym expression.
Error in test2 (line 1)
y (k) = y(k-2)+ 2*h*y(k-1)*(1-y(k-1));
Related documentation
y(k) = y(k-2)+ 2*h*y(k-1)*(1-y(k-1));
h == 0.1;
for k = 1;
y(k) == 0.2;
end
for k = 2;
y (k) == 0.28;
end
for k = 3:20
y(k) = y(k-2)+ 2*h*y(k-1)*(1-y(k-1));
result = y (k=20)
end
Accepted Answer
More Answers (1)
Bjorn Gustavsson
on 13 May 2022
Well run your code line-by-line and figure out what the problem is.
This is what you let us know about your script (My comments as added comments after each dubioud line):
y(k) = y(k-2)+ 2*h*y(k-1)*(1-y(k-1)); % Here you have an assignment of the k-th element of y
% except we cannot tell the current
% value of k nor whether y has enough
% elements at this point. This is
% your definition for the
% propagation, not a line of code you
% should try to run.
h == 0.1; % OK
for k = 1; % Why run a loop with one pass?
y(k) == 0.2;
end
% This does exactly the same job:
y(1) = 0.2
for k = 2; % Same here...
y (k) == 0.28;
end
% instead of doing these assignments one-by-one and then increment the size
% of y in every step of the loop below we do this neatly:
y = zeros(1,20);
y(1:2) = [0.2 0.28];
for k = 3:20
y(k) = y(k-2)+ 2*h*y(k-1)*(1-y(k-1));
%result = y (k=20); % this will not work, you want to extract the last,
% the 20th element of y, not at every pass through
% the loop - it will obviously be zero until k==20,
% notice that the equality-operation is ==, while =
% means assignment in matlab
end
% Here we add the result-extraction - but also use a sensible naming:
y_last = y(end); % extract the last element of y instead of the 20th -
% if you for example were to reduce the step-size to 0.1
% and double the number of steps to 40.
HTH
3 Comments
@Bjorn Gustavsson: "h == 0.1; % OK" - no, not okay. "==" is the elementwise comparison, not the assignment. == and = are repeatedly confused in the code.
The selective perception of an experienced programmer hides this detail before the brain gets aware of it. So it is really interesting that you oversaw this mistake. It is like a musician does not "see" not matching keys of the keyboard during an improvisation.
The explanations are very good, e.g. for the pre-allocation.
Bjorn Gustavsson
on 13 May 2022
Aaahhhhhhhhhhhhhhhh, I'm embarrassed to a physical level. Time for the afternoon coffee, before I miss something like this in my current work...
Jan
on 13 May 2022
Coffee is a good choice.
I think, overseeing this means, that you are not able to type it, even not by accident. Such mistakes do not match in the universe of an experienced programmer. This is "professionally blinkered" and I like this example.
Issac Newton's cat got three kitten. He sawed 3 additional small holes in the door to his workroom beside the large one for the mother. This is a straight solution for a mathematician. His house keeper found a more efficient solution. :-)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!