Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: To display big number in all digits form.
Date: Mon, 23 Mar 2009 10:25:01 -0400
Organization: The MathWorks, Inc.
Lines: 44
Message-ID: <gq863b$fg9$1@fred.mathworks.com>
References: <gps84r$1e2$1@news.onet.pl> <gptja9$hml$1@news.onet.pl> <gptvs5$6qg$1@fred.mathworks.com> <gpuhks$27v$1@news.onet.pl>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1237818283 15881 144.212.105.187 (23 Mar 2009 14:24:43 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 23 Mar 2009 14:24:43 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-RFC2646: Format=Flowed; Response
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
Xref: news.mathworks.com comp.soft-sys.matlab:526949



"ZikO" <zebik@op.pl> wrote in message news:gpuhks$27v$1@news.onet.pl...
>>> A = sym(fib(1476));
>>
>> This is not correct, unless internally your fib function performs its 
>> calculations symbolically.  If it doesn't, this will compute fib(1476) in 
>> double precision and then take that double precision result and convert 
>> it into a symbolic variable.  That's _not_ the same as computing 
>> fib(1476) using symbolic calculations.
>>
>
> Thanks Steve.
>
> Would you like to show me how to do that correctly?


function y = fib(n)
% Error checking required, not included
% Note that this is just one way to implement Fibonacci symbolically
% I can think of at least three additional methods off the top of my head.
% These other implementations left as an exercise for the reader.
xm1 = sym(1);
xm2 = sym(1);
if n < 3
    y = xm1;
else
    for k = 3:n
        y = xm1+xm2;
        xm2 = xm1;
        xm1 = y;
    end
end


Note that because all the addition operations are performed on sym objects, 
the result is symbolic.  In this case, n must be numeric (since < isn't 
defined for sym objects, for reasons that have been discussed on CSSM in the 
past) but the result is symbolic.

-- 
Steve Lord
slord@mathworks.com