From: Jeff on
Can someone please explain why the last statement is returning 0 when the two statements seem equal to me.

EDU>> k=3;
EDU>> (9*2^k - 8*3^k)/6^k
ans =
-0.666666666666667
EDU>> u(k+1)
ans =
-0.666666666666667
EDU>> (9*2^k - 8*3^k)/6^k == u(k+1)
ans =
0

The formula on the second line is the closed formula for u_k I obtained using a generating function on the following sequence:
u(0)=1
u(1)=-1
6u(k)=5u(k-1)-u(k-2)

I realize that way out in the sequence, there's bound to be some rounding error, but we're talking about the fourth term here. Why does Matlab say they're not equal?

Thanks,
-J
From: Rune Allnor on
On 5 Mar, 10:50, "Jeff " <spREMOVEHITSjef...(a)SIGNoptonline.net> wrote:
> Can someone please explain why the last statement is returning 0 when the two statements seem equal to me.
>
> EDU>> k=3;
> EDU>> (9*2^k - 8*3^k)/6^k
> ans =
>   -0.666666666666667
> EDU>> u(k+1)
> ans =
>   -0.666666666666667
> EDU>> (9*2^k - 8*3^k)/6^k == u(k+1)
> ans =
>      0
>
> The formula on the second line is the closed formula for u_k I obtained using a generating function on the following sequence:
> u(0)=1
> u(1)=-1
> 6u(k)=5u(k-1)-u(k-2)
>
> I realize that way out in the sequence, there's bound to be some rounding error, but we're talking about the fourth term here. Why does Matlab say they're not equal?

It's because of the rounding error.

Rune
From: Bruno Luong on
"Jeff " <spREMOVEHITSjeffAT(a)SIGNoptonline.net> wrote in message <hmqk55$f1c$1(a)fred.mathworks.com>...

> Why does Matlab say they're not equal?

Subtract them to see why.

Bruno
From: John D'Errico on
"Jeff " <spREMOVEHITSjeffAT(a)SIGNoptonline.net> wrote in message <hmqk55$f1c$1(a)fred.mathworks.com>...
> Can someone please explain why the last statement is returning 0 when the two statements seem equal to me.
>
> EDU>> k=3;
> EDU>> (9*2^k - 8*3^k)/6^k
> ans =
> -0.666666666666667
> EDU>> u(k+1)
> ans =
> -0.666666666666667
> EDU>> (9*2^k - 8*3^k)/6^k == u(k+1)
> ans =
> 0
>
> The formula on the second line is the closed formula for u_k I obtained using a generating function on the following sequence:
> u(0)=1
> u(1)=-1
> 6u(k)=5u(k-1)-u(k-2)
>
> I realize that way out in the sequence, there's bound to be some rounding error, but we're talking about the fourth term here. Why does Matlab say they're not equal?
>
> Thanks,
> -J

What you misunderstand is that rounding error happens
after EVERY floating point addition, subtraction, etc.

The least significant bits of these numbers are not
identical, so they will not be equal.

John
From: Jeff on
"John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <hmr07i$l98$1(a)fred.mathworks.com>...
> "Jeff " <spREMOVEHITSjeffAT(a)SIGNoptonline.net> wrote in message <hmqk55$f1c$1(a)fred.mathworks.com>...
> > Can someone please explain why the last statement is returning 0 when the two statements seem equal to me.
> >
> > EDU>> k=3;
> > EDU>> (9*2^k - 8*3^k)/6^k
> > ans =
> > -0.666666666666667
> > EDU>> u(k+1)
> > ans =
> > -0.666666666666667
> > EDU>> (9*2^k - 8*3^k)/6^k == u(k+1)
> > ans =
> > 0
> >
> > The formula on the second line is the closed formula for u_k I obtained using a generating function on the following sequence:
> > u(0)=1
> > u(1)=-1
> > 6u(k)=5u(k-1)-u(k-2)
> >
> > I realize that way out in the sequence, there's bound to be some rounding error, but we're talking about the fourth term here. Why does Matlab say they're not equal?
> >
> > Thanks,
> > -J
>
> What you misunderstand is that rounding error happens
> after EVERY floating point addition, subtraction, etc.
>
> The least significant bits of these numbers are not
> identical, so they will not be equal.
>
> John

OK. Thanks all. That makes sense now.

How can I write this type of code correctly? Do I just multiply both by 10^n, round, and compare?
 |  Next  |  Last
Pages: 1 2
Prev: BER for BPSK
Next: variable label in scatterplot matrix