Trying to use central difference scheme to solve logistic equation

5 vues (au cours des 30 derniers jours)
Prajit Baruah
Prajit Baruah le 13 Mai 2022
Commenté : Jan le 13 Mai 2022
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

Réponse acceptée

Jan
Jan le 13 Mai 2022
Your code is no valid Matlab code. I suggest to learn the basics from: https://www.mathworks.com/learn/tutorials/matlab-onramp.html
This line cannot work:
y(k) = y(k-2)+ 2*h*y(k-1)*(1-y(k-1));
neither y nor k nor h have been defined before.
== is the operator for the elementwise comparison:
h == 0.1;
You mean:
h = 0.1;
This is too much clutter:
for k = 1;
y(k) = 0.2; % Again: = instead of ==
end
for k = 2;
y (k) = 0.28;
end
Use this:
y(1) = 0.2;
y(2) = 0.28;
Then this part is working:
for k = 3:20
y(k) = y(k-2) + 2 * h * y(k-1) * (1 - y(k-1));
end
I assume you want:
result = y(20); % not: y(k=20)
and this is valid only after the loop. Inside the loop y(20) is not defined until the last iteration.
  2 commentaires
Prajit Baruah
Prajit Baruah le 13 Mai 2022
Thank you! Yes, I've just started using Matlab and this was my first exercise. I was just quite confused with how the indexing and assigning of variables was working, but now it is clear. Thank you so much.
Jan
Jan le 13 Mai 2022
You are welcome. Read Bjorn's useful explanations also.

Connectez-vous pour commenter.

Plus de réponses (1)

Bjorn Gustavsson
Bjorn Gustavsson le 13 Mai 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 commentaires
Bjorn Gustavsson
Bjorn Gustavsson le 13 Mai 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
Jan le 13 Mai 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. :-)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by