MATLAB: How to solve this summation with exponential in Matlab
functionMATLABmatlab functionsum
Related Solutions
With only 1/k in the denominator, that sum will take forever to give you any degree of precision in the sum. So that degree of precision theta is sort of wasted, because you would need literally trillions of terms to get even double precision accuracy.
Think of it like this: how many terms to do need to get 1000 digit accuracy in a sum with terms that decrease with 1/k?
When k is on the order of 1e16, you are finally approaching double precision, thus eps. But your computer is not fast enough to compute 1e16 terms in any reasonable amount of time. And don't forget that it takes a significant amount of computer time to compute anything in a thousand digits of precision.
But, just imagine that you can compute one term of that sum in a millionth of asecond. (That is being very gracious on my part, too. I just wish I had a computer that was that powerful, but I don't work for the NSA.)
So every second, one million terms. 31 million seconds per yar or so. So 3.1e13 terms per year. Or, roughly 300 years before you compute 1e16 terms. And that only gets you to roughly double precision. But you want to compute all this in 1000 digits. So you will need to compute on the order of 1e1000 terms, before you start to get close to that.
You should be able to do it, just around the time the universe suffers heat death. Of course, by then you will have a better computer.
However, you did ask how you might do this. You can use my HPF toolbox. Or you can use the symbolic toolbox. Done in HPF (you need to download it from the File Exchange first, and install it on your search path.)
DefaultNumberOfDigits 1000theta = hpf('1.306377883863080690468614492602605712916784585156713644368053759966434053766826598821501403701197395707296960938103086882238861447816353486887133922146194353457871100331881405093575355831932648017213832361522359062218601610856679057215197976095161992952797079925631721527841237130765849112456317518426331056521535131866841550790793723859233522084218420405320517689026025793443008695290636205698968726212274997876664385157661914387728449820775905648255609150041237885247936260880466881540643744253401310736114409413765036437930126767211713103026522838661546668804874760951441079075406984172603473107746775740640078109350834214374426542040853111654904209930908557470583487937577695233363648583054929273872814934167412502732669268404681540626763113223748823800118041206286013841914438857151609189388944789912125543384749359092744422082802260203323027106375022288131064778444817003723336406042118742608383328221769687812353049623008802672211104016065088809718347778314022490821844106377494000232824192701');
Now, write a function to compute the sum. First I need to make sure that I've interpreted that iterated exponent properly.
Do you intend it as theta^(3^n), or or as (theta^3)^n? The difference is of course significant. By default, MATLAB would treat the computation of theta^3^n as the latter form.
Before I go too far, let me see how fast HPF is, working in 1000 digits.
timeit(@() theta^3^n)ans = 0.0015156 1/ansans = 659.82
So HPF can do 660 such computations per second, at least on my computer.
How fast is the symbolic toolboxhere?
digits 1000theta = sym('1.306377883863080690468614492602605712916784585156713644368053759966434053766826598821501403701197395707296960938103086882238861447816353486887133922146194353457871100331881405093575355831932648017213832361522359062218601610856679057215197976095161992952797079925631721527841237130765849112456317518426331056521535131866841550790793723859233522084218420405320517689026025793443008695290636205698968726212274997876664385157661914387728449820775905648255609150041237885247936260880466881540643744253401310736114409413765036437930126767211713103026522838661546668804874760951441079075406984172603473107746775740640078109350834214374426542040853111654904209930908557470583487937577695233363648583054929273872814934167412502732669268404681540626763113223748823800118041206286013841914438857151609189388944789912125543384749359092744422082802260203323027106375022288131064778444817003723336406042118742608383328221769687812353049623008802672211104016065088809718347778314022490821844106377494000232824192701');timeit(@() theta^3^n)ans = 0.00138 1/ansans = 724.62
So the symbolic TB does 725 such computations per second, thus about the same as HPF. And, yes, my computer is getting a little long in the tooth, but it was quite fast when I bought it some years ago, so it is still decent. Not even close to a mllion per second. I think you are in deep trouble. Really deep. Massively deep. Humongously deep.
Sigh. anyway. I'll use the symbolic toolbox here.
function S = bigsum(n,kterms,theta) % precompute this number
theta3n = theta^3^n; % initialize the sum. Note that 1/2 is representable exactly by
% either HPF or by the symbolic toolbox, so I can just subtract 1/2.
S = theta3n - 1/2; pie = sym('pi'); for k = 1:kterms S = S + sin(2*k*pie*theta3n)/k; end S = vpa(S/pie);end
It is not too bad, as long as I restrict the number of terms.
tic,S = bigsum(1,100,theta);tocElapsed time is 0.325404 seconds.
So 100 terms in 1/3 of a second. But do we even know the very first digit of that number yet?
for kt = 100:105vpa(bigsum(1,kt,theta),20)endans =0.81880906421480201873ans =0.82165197521120346388ans =0.82334942686820709411ans =0.82099366721044812494ans =0.8187293340428497687ans =0.82046391141319398969
It seems to be converging. SLOWLY. The first digit might be an 8, that is, if this is a convergent series. After a fair amount of time,10000 terms gives:
vpa(bigsum(1,10000,theta),20)ans =0.82099823595744050401
Personally, I recommend buying a VERY fast computer. Better yet, you need to reconsider if you can do this computation in a finite amount of time, unless you are willing to wait for a few gazillion years.
You need to understand double precision. So far, I've seen two posts by you that show you are not doing so.
First, your example.
digits 100vpa(sinh(50))ans =2592352764293536022528.0
Now, I'll compute the same result, using my own HPF tool.
sinh(hpf(50,100))ans = 2592352764293536232043.726661466742692413734453854430802756139316036287616046268961598412632838901239
As you can see, HPF has no problem. What was the difference? There, I created the number 50, as an HPF number. THEN I computed sinh of that value.
What did you do, and why was it different? You started with the number 50. This is a double precision value. THEN you computed the sinh, as a double precision number, thus limited to about 16 digits of precision. And then, and only then, did you tell MATLAB to convert this to a symbolic result! What should you expect?
Just because you executed the command
digits 100
this does not tell MATLAB that ALL computations will be done in 100 digits of precision. No matter what, MATLAB does double precision computations. digits is a symbolic toolbox command, and only applies to computations done there. But even then, if you pass in a result that is only in double precision, then the symbolic toolbox cannot do magic, and undo what you have done.
So, now try this:
vpa(sinh(sym(50)))ans =2592352764293536232043.726661466742692413734453854430802756139316036287616046268961598412632838901239
It looks just like what HPF returned, and for good reason. The difference between the various computations above is important, as well as the reason why HPF and the symbolic TB were able to generate the correct result with no problem.
Finally, be careful with these things. Both HPF and sym had no problem with the number 50, because it is EXACTLY representable as an integer, even in double precision. So you need to understand the difference between the next two statements, and why they have a different result.
vpa(sym(50.12324545))ans =50.1232454499999988684066920541226863861083984375vpa(sym('50.12324545'))ans =50.12324545
Related Question
- How to use large numbers
- Exp(infty1) * erfc(infty2) = NaN
- Issue with gammainc(x,a) for small x and larger a
- Factorial in matlab R2010a
- [GIS] ArcGIS Javascript API with Windows Authentication
- Facing a problem in calculating the sum of digits of 100!.
- Adding all the digits in a large number gives me an incorrect result
Best Answer