I have to use {} in my for loop, but now I can't multiply...
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2017-4-20
编辑:Stephen23
2017-4-20
@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
2017-4-20
@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.
回答(1 个)
James Tursa
2017-4-19
编辑:James Tursa
2017-4-19
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 个评论
Jan
2017-4-20
编辑:Jan
2017-4-20
@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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!