Simple loop with equation problem

2 ビュー (過去 30 日間)
Gabriela Ziola
Gabriela Ziola 2023 年 3 月 13 日
コメント済み: John D'Errico 2023 年 3 月 13 日
Hello,
I'm fighting with simple problem with loop with equation inside.
I have to say that I started learing Matlab yesterday and I'm not really good at it.
Can you show me where am I commiting mistake? I have function:
clear all;
close all;
clc;
Time(1) = [0];
Quantity(1) = [0];
Xt = 0.2
a = 0.5
t = 9
for i=1:t
Xt1 = a * Xt * (1 - Xt);
Quantity(i+1) = Xt1;
Time(i+1) = Time(i) + 1;
end
figure ()
hold on
plot(Time,Quantity)
title('Chart')
xlabel('Time')
ylabel('Quantity')
My plot is looking like this:
While book plot is looking like this (exact same values):
It's like loop doesn't work and I don't know why
Thank you for help
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 3 月 13 日
編集済み: Dyuman Joshi 2023 年 3 月 13 日
Are you plotting the right variables? Are the initial values of all the variables correct?
The book plot starts from 0.2 (Xt is defined to be 0.2 initially), and plots xn vs n.
Your plot starts from 0, and plots quantity vs time.

サインインしてコメントする。

採用された回答

Les Beckham
Les Beckham 2023 年 3 月 13 日
Time(1) = [0];
Quantity(1) = 0.2; % Corrected initial condition
Xt1 = 0.2;
a = 0.5;
t = 9;
for i=1:t
Xt1 = a * Xt1 * (1 - Xt1); % Update this each time; your were setting Xt1 to the same value every time
Quantity(i+1) = Xt1;
Time(i+1) = Time(i) + 1;
end
figure ()
hold on
plot(Time, Quantity)
grid on
title('Chart')
xlabel('Time')
ylabel('Quantity')
  1 件のコメント
Gabriela Ziola
Gabriela Ziola 2023 年 3 月 13 日
That works, thank you!

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
John D'Errico 2023 年 3 月 13 日
編集済み: John D'Errico 2023 年 3 月 13 日
Looking at your code...
Learn to use semi-colons at the end of your lines. This avoids crap being dumped into the command window.
There is no need to put brackets around a scalar value.
But time will be a vector of length t. So instead of growing the time and Quantity vectors in a loop, allocate them in advance, using zeros. Better yet, you know what the vector Time will be. The numbers 1:10.
Next, you NEVER saved the result where you computed Xt1. Instead, you kept on using the existing value of Xt. but it was always exactly the same. (This was the only important thing you did wrong. Everything else is minor. Look carefully at your code. You use Xt to compute Xt1. Then you never used Xt1, allowing MATLAB to dump XT1 into the bit bucket on each pass through the loop.)
And, Quantity is identical to the vector Xt. Since Xt is being created in the loop, just assign it to Quantity.
t = 9;
Time = 1:t+1;
Xt = 0.2;
a = 0.5;
for i=1:t
Xt(i+1) = a * Xt(i) * (1 - Xt(i));
end
Quantity = Xt;
Now, there is no need to set hold to be on, if you are not plotting anything else on the plot. Titles and axis labels don't count.
plot(Time,Quantity,'o-b')
title('a = 0.5')
xlabel('Time')
ylabel('Quantity')
grid on
  2 件のコメント
Gabriela Ziola
Gabriela Ziola 2023 年 3 月 13 日
編集済み: Gabriela Ziola 2023 年 3 月 13 日
Thank you for the tips, those will be really helpfull for the next couple of months :)
So per my understanding:
1) My starting value is Xt = 0.2
2) When loop ends my new Xt (which was multiplied by the equation) is being saved in Quantity array
Sounds perfect and the code is easy, but I don't get
Quantity = Xt;
It says that Quantity array is made of Xt. But for noob like me it looks like it's only made of just one Xt, not 9x Xt.
Also, I need to make bifurcation diagram out of it. Is there a way to do that?
John D'Errico
John D'Errico 2023 年 3 月 13 日
Play around in MATLAB. Try things. For now, I'll leave the semi-colons off.
T = 1:10
T = 1×10
1 2 3 4 5 6 7 8 9 10
So T here is a vector. It contains the numbers 1 through 10.
We can copy T into a new variable. I'll call it S. What is S now?
S = T
S = 1×10
1 2 3 4 5 6 7 8 9 10
whos
Name Size Bytes Class Attributes S 1x10 80 double T 1x10 80 double cmdout 1x33 66 char
We now have two variables, in this case, copies of each other.
I'd strongly suggest you want to take the MATLAB Onramp tutorial. Its free, and you can learn a lot.
As far as the bifurcation diagram goes, you need ot make a good start at homework before I will step in.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by