Simple loop with equation problem
1 次查看(过去 30 天)
显示 更早的评论
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1323220/image.png)
While book plot is looking like this (exact same values):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1323225/image.png)
It's like loop doesn't work and I don't know why
Thank you for help
1 个评论
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
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 个)
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 个评论
John D'Errico
2023-3-13
Play around in MATLAB. Try things. For now, I'll leave the semi-colons off.
T = 1: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
whos
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!