While running this matlab code the value of x(513) is infinite value.But while calculating manually it is a finite value only.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Veera Kalyani S
am 6 Jan. 2017
Bearbeitet: Stephen23
am 6 Jan. 2017
x(1)=3.58;
for i=2:(513)
x(i)=4*(x(i-1));
end
Why this kind of error is happening in matlab?
0 Kommentare
Akzeptierte Antwort
Stephen23
am 6 Jan. 2017
Bearbeitet: Stephen23
am 6 Jan. 2017
Because your calculation exceeds the largest value that can be represented using the floating point class double:
>> realmax()
ans = 1.7977e+308
and values larger than that are represented as Inf. The second-to-last value is 1.6089e+308 which you then multiply by four to get Inf. There is no surprise there, this is the expected behavior.
Note also that you can easily generate this vector more efficiently without a loop:
cumprod([3.58,4*ones(1,512)])
All beginners need to learn what floating point numbers are and how to work with them, otherwise their numeric calculations can easily turn into meaningless rubbish. For a readable introduction to floating point arithmetic, look at Cleve's Corner article from 1996: Floating Points.
For more rigorous and detailed information on floating point arithmetic, read the following paper: What Every Computer Scientist Should Know About Floating Point Arithmetic.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays 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!