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

The following algorithm for computing ? is due to Archimedes:
1. Start with ? = 1 and ? = 6.
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 commentaires

To repeat steps 2-7 until a tolerance is reached, wrap them in a while loop with an if test inside that issues a break command when the tolerance is reached. Read the documentation for while, if, and break. Try starting here: https://www.mathworks.com/help/matlab/matlab_prog/loop-control-statements.html
I'm pretty sure that there are serious problems with the algorithm as you describe it, however. For example, items 4 and 5 show u and l being defined as the same (n*a/2). Your code does not match the outlined algorithm, either. The code defining a has an extra square root or power of 1/2 and lots of extra and questionably placed parentheses. It definitely doesn't match the definition in item 3 which is relatively simple.
Read the docs for looping help and double check the definition of your algorithm. Respond with a comment if you still have questions.
It is also good practice to tag your homework questions as such.
i have edited it..is it correct now?
You forgot to put in the while loop, so steps 2-7 don't repeat in your code.
Personally I would chose to make the breaking condition obvious:
while e<tol
It is fairly easy to test your code: run in. You know the true value of pi, so you can run your function and see if pi comes out. I would also suggest you take another look at how you define your tolerance. To me, the assignment looks as if the input is k and you need to calculate the value of tol.
i have to run it with the value tol=0.01, and some more,,,
Also, when you say i have to write the while, do u mean i have to delete ''if'' and write ''while''? will not work
Have you read the documentation for while? It will repeat a block of code while some condition is true. That means you can do two thing:
while true
%code to be repeated
if cond
break
end
end
or
while cond
%code to be repeated
end
Personally I think the second is easier to understand. The indentation of your code suggests you already had a while in your function, but removed it before posting here. I would suggest putting the while there and see what happens.
Where exactly i have to write this? '' while cond %code to be repeated end ? In which line ?
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?
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.

Connectez-vous pour commenter.

Réponses (1)

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

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by