Effacer les filtres
Effacer les filtres

I have to use {} in my for loop, but now I can't multiply...

1 vue (au cours des 30 derniers jours)
cestcandice
cestcandice le 19 Avr 2017
Modifié(e) : Jan le 20 Avr 2017
I have to develop random numbers where n=1-100. So, I did this:
x=1:100;
for n=1:length(x);
y{n}=rand(n)
end
After that, I have to use only the last 50 matrices (50-100),
y2=y(50:100)
to put into this equation:
xn=r*(y2)*(1-(y2))
But it doesn't work, I'm guessing because of the {}... but I can't get rid of that.
  17 commentaires
Stephen23
Stephen23 le 20 Avr 2017
Modifié(e) : Stephen23 le 20 Avr 2017
@Candice Stauffer: you do not need to guess how to use MATLAB, because MATLAB has documentation which explains how it works, and how to use it. I just tried several internet search engines with the term "MATLAB rand", and they all returned the same first result: the MATLAB documentation for the function rand. You can do this too! Then you would know what the input to rand does.
Also you need to learn to stop relying on what you wish/believe/imagine/want/assure/... that your code is doing, and start to actually look at what the code is really doing. Code does not care what is in your head: it will do exactly what it does, regardless. You job is to understand it, and check it. This means do not write code without knowing what it is doing: write one line at-a-time: read the help for the functions you need (try the examples too), test that each line really does what you need it to do, check its output by hand. Only then should you move on to the next line.
Jan
Jan le 20 Avr 2017
@Stephen: +1. Exactly.
@Candice: Please consider Stephen's advice. You find 16 comments here already, althought the problem is not hard. Something is going wrong here. Take Stephen's suggestion as motivation to learn a deterministic programming style.

Connectez-vous pour commenter.

Réponses (1)

James Tursa
James Tursa le 19 Avr 2017
Modifié(e) : James Tursa le 19 Avr 2017
Treat the x"sub n" in the screenshot as x(n) in MATLAB. I.e., the n'th element of a vector called "x".
So x"sub 1" in the screenshot would be x(1). So that first expression for x(1) would be:
x(1) = rand();
Then the recursion relationship in MATLAB is:
x(n+1) = r*x(n)*(1 - x(n));
So you need to put this together in a loop:
r = something; % <-- you need to fill in this value
x = zeros(1,100); % <-- pre-allocate the result
x(1) = rand(); % <-- the 1st element
for n=1:99 % <-- end the loop at 99 so that 99+1=100 is the final element
% put the recursion relationship here
end
Then you will be in a position to do the plotting. The elements 50 through 100 are simply x(50:100).
In your jpg, what is the lower part of the plot? I.e., what is the horizontal axis?
  6 commentaires
cestcandice
cestcandice le 19 Avr 2017
Sure!
Jan
Jan le 20 Avr 2017
Modifié(e) : Jan le 20 Avr 2017
@Candice: "It's saying that In an assignment A(:)=B, the same." Do not post rephrased or abbreviated error message in the forum. Only a complete copy of the message is meaningful. In addition it tells you exactly where and what the problem is: The right hand side is a [1 x 4] vector, but on the left side of the = you have the scalar variable x(n+1). This cannot work and it is your turn to find out, what you want to do instead.
Do not provide r as a vector:
rv = [2.99,3.05,3.54,3.6];
for ir = 1:numel(rv)
r = rv(ir);
...
end
Now you perform the calculations in a loop for the 4 different values of r separately.

Connectez-vous pour commenter.

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by