Major confusion for loops - precision not acquired
4 views (last 30 days)
I have a loop that sums the values of function called pdfomegat
My second loop is slightly different loop and is as follows
I know for fact that c1 is 1 .. However notice that in the second loop I change nothing but multiply by symbolic character t. Howver I get that
Why isn't c2=t?
Is there anyway I can fix it, I can't use the round function because it symbolic.. Any thoughts?
John D'Errico on 6 Feb 2015
Edited: John D'Errico on 6 Feb 2015
No. You cannot make a double precision number more precise than it is. There are simple many numbers that are not exactly representable using a double. For example, 0.1 is not so, since doubles are stored in binary form.
Anyway, my very good bet is that c1 is NOT 1, but that it was displayed in the command window as 1, using format short. So I think what you know for a fact is actually completely false. As well, it is entirely possible that you were using a single precision format to store pdfomegat, since c1 was so inaccurately NOT equal to 1.
However, you can do many things to improve your work. For example, in this line, rather than multiplying by 0.01, divide by 100, an integer. You do this because 0.01 is not stored exactly as a double, but 100 is.
As for the code itself, write it more efficiently. Learn to use vectors, and simple tools like sum.
Your first loop does nothing more than multiply the vector pdfomegat by 0.01, and then sum it.
func1 = pdfomegat/100;
c1 = sum(func1);
You can also totally replace that second loop simply as
func2 = t*func1;
c2 = t*c1;
As for making the sums themselves more accurate, you could convert the vector pdfomegat into a symbolic form (or my own HPF) but since they were originally stored as doubles, you simply won't gain much at all there, unless you create those numbers in a form that allows you to store them more accurately in the first place.
Star Strider on 6 Feb 2015
It’s not straightforward, but you can round the coefficient:
c2=0.99999464659732599597363744692302*t; % Original Expression
cf = double(vpa(coeffs(c2))) % Get Coefficient
cf = fix(cf * 1E+8)/1E+8 % Round To 8 Digits