# algorithm for computing ? is due to Archimedes: how steps can be repeated

2 Ansichten (letzte 30 Tage)
Anastasia Kyriakou am 22 Feb. 2020
Beantwortet: Pravin Jagtap am 25 Feb. 2020
The following algorithm for computing ? is due to Archimedes:
2. Replace ? by 2?.
3. Replace ? by sqrt(2 − sqrt(4 − ?))
4. Let ? = ??/2.
5. Let ? = na/2
6. Let ? = (? + ?)/2 (estimate of ?)
7. Let ? = (? − ?)/2 (estimate of error)
8. Repeat steps 2 – 7 until ? becomes smaller than a given tolerance ???.
9. Output ? and ?
Write a function that implements this algorithm. Use your function to determine ? and ? for ??? = 10^(=k)
, ? = 2, 3, … , 10.
I have written a code but i do not know how to write it so that if the value of e is larger than tol then steps 2-7 repeated. I have written this :
```
function [p,e] = algorithmPi(tol)
a = 1;
n = 6;
e = inf;
n = 2*n;
a = sqrt(2-sqrt(4-(a^2)));
l = (n*a)/2;
u = l/(sqrt((1-(a)^2)/2));
p = (u+l)/2; % estimate of pi
e = (u-l)/2; % estimate of error
if e < tol
done = true;
disp('Complete: Error below tolerance')
end
end
##### 9 Kommentare7 ältere Kommentare anzeigen7 ältere Kommentare ausblenden
Anastasia Kyriakou am 23 Feb. 2020
function [p,e] = algorithmPi(tol)
a = 1;
n = 6;
e = inf;
n = 2*n;
a = sqrt(2-sqrt(4-(a^2)));
l = (n*a)/2;
u = l/(sqrt((1-(a)^2)/2));
p = (u+l)/2; % estimate of pi
e = (u-l)/2; % estimate of error
if e < tol
done = true;
disp('Complete: Error below tolerance')
while cond
%code to be repeated
end
end
end
% end
Like that?
Rik am 23 Feb. 2020
What code do you want to repeat? Make sure that is inside the while loop.
I would strongly urge you to read the documentation for the functions you're using if you don't understand what they do.

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Pravin Jagtap am 25 Feb. 2020
Hello Anastasia,
As mentioned in the above comments, I would recommend you to follow the documentation for understanding the loops. Refer to the following template which will help you to implement the algorithm:
function [p,e] = algorithmPi(tol)
% step 1 - Initialize a, n and e(to inf)
% step 2 - Iterative process from 2 -7
while (e > tol)
% step 3 - Replace n and a
% step 4 - Compute l, u, p and e
end
end
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Number Theory finden Sie in Help Center und File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by