For loop producing correct values but returning zeros

Working on what should be a very simple task. A for loop that follows the equation for n number of times, and stores each result in the array Z. As each iteration runs, the correct number is generated then stored, however, beyond roughly iteration 8 or so, it resets the previous numbers to zero and sets the rest to infinity (or zero) depending on how I write it. I'm not sure what I'm doing wrong?
% n = 100;
% c=0.25
% z = zeros(1,n);
% z(1,1) = 1;
% z(1,2) = 1
% for i = 1:(n-1)
% z(1, i+1) = ((z(1,i))^2 + c);
% end
% plot(z)
n = 100;
c=0.25
c = 0.2500
z = zeros(1,n);
z(1,1) = 1;
z(1,2) = 1
z = 1×100
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
for i = 1:(n-1)
z(1, i+1) = ((z(1,i))^2 + c);
end
plot(z)

 采纳的回答

It might look like the values are zero, but they are not.
However, compared to the values obtained at the later iterations, the initial values are so miniscule that they appear to be virtually 0.
The rate of change of values is so high that the changes are abrupt, and so abrupt that the values after the 13th iteration are effectively Infinite. This can be observed from the output figure, where the curve is plotted upto x = 13 only.
The values are larger than the capacity of double precision numbers, thus they are stored as Infinity.
You could use symbolic numbers if you want to obtain the values with full accuracy, but even then, the values after the 34th iteration become Infinite.
n = 100;
c=0.25;
z = zeros(1,n);
z(1,1) = 1;
z(1,2) = 1;
for i = 1:(n-1)
z(1, i+1) = ((z(1,i))^2 + c);
end
%Values after 13th interation
z(14:20)
ans = 1×7
Inf Inf Inf Inf Inf Inf Inf
plot(z)
%Changing the scale of the y axis to log might help you visualize the progression of values
yscale('log')

更多回答(1 个)

"it resets the previous numbers to zero"
That's not correct. The first few iterations' results appear to be zero on the plot because they are so small compared to later iterations' results (but you can change the y scale to logarithmic to see them better). They also appear to be zero on the command line for the same reason (but you can change the format of command-line format to see them better).
"sets the rest to infinity"
That is correct: at some point the numbers become larger than the largest representable double-precision floating-point number, so they are stored as Inf.
n = 100;
c=0.25;
z = zeros(1,n);
z(1,1) = 1;
z(1,2) = 1;
for i = 1:(n-1)
z(1, i+1) = ((z(1,i))^2 + c);
end
semilogy(z) % plot on log scale
% the first few elements of z appear to be zero:
z
z = 1×100
1.0e+283 * 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1.1832 Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
% but they're not really:
z(1:6)
ans = 1×6
1.0000 1.2500 1.8125 3.5352 12.7473 162.7444
z(7)
ans = 2.6486e+04
z(8)
ans = 7.0151e+08
z(9)
ans = 4.9211e+17
z(10)
ans = 2.4218e+35
z(11)
ans = 5.8649e+70
z(12)
ans = 3.4397e+141
z(13)
ans = 1.1832e+283
z(14)
ans = Inf
% adjusting the command-line format:
format longG
z(:)
ans = 100×1
1.0e+00 * 1 1.25 1.8125 3.53515625 12.7473297119141 162.744414784247 26485.9945434671 701507907.206566 4.92113343873337e+17 2.42175543218197e+35

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2023b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by