cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

calculation of pi

SOLVED
Highlighted
Newbie

calculation of pi

Hello everyone! I have some problem in cycle that I made in mathcd 15. I wrote short code to calculate pi number but only 14 numbers after point I can only see in my array and dont know what to do. Can anyone can help to solve this problem?    @

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: calculation of pi

First: The loop with j as upper limit seems to be a bit useless. At least its not necessary to calculate pi 100 times with increasing precision. Calculate it once with max precision and then do your num2str thing.

The reason for the error is, that you are limited to numerical precison in a program, too.

So no matter how large you choose N1 to be, you will only get a result for pi with approx. 14 or 15 digits.

This result is then  converted to a string with just 16 or 17 entries.

The error is when you demand to take element with indes number j+1 and j+1 is bigger than the last index in the array (max 16).

To get more decimals of pi you have a couple of choces:

Use a different algorithm which allows you to calculate specific decimals without calculating the decimals before it (spigot algorithm).

Implement your own long number arithmetic.

or use Mathcads symbolics to do the job:

Regards

Werner

View solution in original post

19 REPLIES 19
Highlighted

Re: calculation of pi

Highlighted

Re: calculation of pi

Highlighted

Re: calculation of pi

Sorry, but thats nonsense!

You stop the calculation the first time the loop is executed and return an approximated value (the value you would get originally with N1=10) which is completely independend of N1.

What you suggest is exactly the same as

Sure not, what the OP had in mind!

Highlighted

Re: calculation of pi

First: The loop with j as upper limit seems to be a bit useless. At least its not necessary to calculate pi 100 times with increasing precision. Calculate it once with max precision and then do your num2str thing.

The reason for the error is, that you are limited to numerical precison in a program, too.

So no matter how large you choose N1 to be, you will only get a result for pi with approx. 14 or 15 digits.

This result is then  converted to a string with just 16 or 17 entries.

The error is when you demand to take element with indes number j+1 and j+1 is bigger than the last index in the array (max 16).

To get more decimals of pi you have a couple of choces:

Use a different algorithm which allows you to calculate specific decimals without calculating the decimals before it (spigot algorithm).

Implement your own long number arithmetic.

or use Mathcads symbolics to do the job:

Regards

Werner

View solution in original post

Highlighted

Re: calculation of pi

Thank you, I will try!
But one thing I need every digit in array not in one float number.

Highlighted

Re: calculation of pi

Can you "cheat" by copy/pasting the symbolic result into a new string variable?

You could then easily isolate each digit by using the "substr" function.

Highlighted

Re: calculation of pi

Yes, I thought about it but if 1000 digits it is not working

Highlighted

Re: calculation of pi

I didn't realize how many digits you wanted.  As Werner noted below, the limit for the Mathcad symbolics is 250 significant digits.

If you don't consider this method "cheating," then perhaps you could just copy/paste from an online source?

Highlighted

Re: calculation of pi

Okay, took some looking:

The num2str function has a limit--it creates a string of only a certain length.  (note num2str(pi) stops at ...79 while pi goes on ...793)

So regardless of how big N1 is, the substr function will fail when it tries to go past the last character in string c.   

One fix:

This solves that problem, but it doesn't get you past the basic numerical precision issue.

Highlighted

Re: calculation of pi

Thank you!

Highlighted

Re: calculation of pi

The main problem is that you are trying to calculate using the numeric calculation engine. Its accuracy is very limited (17 decimal places , showing 15/16).

if you want more accuracy , the only way as I understand is to use the symbolic calculation engine.Capture.PNG

Example shows N1-10, this can continue up to approx. N1=80 after that the symbolic engine fails to generate a result.

This is also possible:

I'm not sure how far you can take this ,1000 places appears to be OK.

But as soon as you convert it to a numeric result, the 17 places limit will apply again.

If you want to create a digit by digit array, the numer and denom function will be useful.

The first steps in a manual version are attached, whether it is possible to automate this , I don't know.

Regards

Andy

Highlighted

Re: calculation of pi

Thank you, I will try to automate this!

Highlighted

Re: calculation of pi

> I'm not sure how far you can take this ,1000 places appears to be OK.

250 significant digits are the upper limit

Highlighted

Re: calculation of pi

Well, may be I use spigot algorithm to generate array

Highlighted

Re: calculation of pi

If 249 decimals are enough, copying the result of the symbolic evaluation (with float,250) in a string and then convert it in an array of digits would be easier.

Highlighted

Re: calculation of pi


Well, may be I use spigot algorithm to generate array

I am not sure about that algorithm. It can be that it needs big number arithmetic, too.

Highlighted

Re: calculation of pi

Thank you, I will try with 250 decimals may be it is enough to my little experiment

Highlighted

Re: calculation of pi

Following is a numerical procedure to get decimals for Pi. But Mathcad, as numerical tool, can get more than 47. If you want more digits, guess must to take them from other software, and read digits from a file. Attached, also first million of pi digits as plain text.

Best regards.

Alvaro.

espita.gif

Highlighted

Re: calculation of pi

Thank you so much!!!! 1 million is enough!

Announcements