Skip to main content
1-Visitor
July 7, 2016
Solved

calculation of pi

  • July 7, 2016
  • 5 replies
  • 7991 views

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?    @

Best answer by Werner_E

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

5 replies

24-Ruby IV
July 7, 2016

24-Ruby IV
July 7, 2016

25-Diamond I
July 7, 2016

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!

Werner_E25-Diamond IAnswer
25-Diamond I
July 7, 2016

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

1-Visitor
July 7, 2016

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

1-Visitor
July 7, 2016

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.

23-Emerald I
July 7, 2016

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.

1-Visitor
July 7, 2016

Thank you!

12-Amethyst
July 7, 2016

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

1-Visitor
July 7, 2016

Thank you, I will try to automate this!

12-Amethyst
July 7, 2016

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

1-Visitor
July 8, 2016

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