Simple loop with equation problem
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Gabriela Ziola
am 13 Mär. 2023
Kommentiert: John D'Errico
am 13 Mär. 2023
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 Kommentar
Dyuman Joshi
am 13 Mär. 2023
Bearbeitet: Dyuman Joshi
am 13 Mär. 2023
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.
Akzeptierte Antwort
Les Beckham
am 13 Mär. 2023
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')
Weitere Antworten (1)
John D'Errico
am 13 Mär. 2023
Bearbeitet: John D'Errico
am 13 Mär. 2023
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 Kommentare
John D'Errico
am 13 Mär. 2023
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.
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!