Need help creating a loop

Have to create a function with a loop that formulates pi using Leibniz's formula. It has to ask the user for a positive integer n&then calculate pi to n terms (terms being 4=1 , 4/3= 2, 4/5= 3, 4/7= 4, etc..)
Leibniz's formula says that pi= 4-4/3 +4/5 - 4/7 + 4/9 - 4/11 ....
OR
pi/4= 1 - 1/3 +1 /5 - 1/7 + 1/9 - 1/11...
so far, i thought to put:
function pi= pleibniz (n)
for i= 1:n pi/4=
but then i don't know what to do! please help!

 Risposta accettata

Daniel Shub
Daniel Shub il 19 Ott 2011

2 voti

I wouldn't use a loop to do this (and I love loops).
  1. How could you make z equal the sequence 1, 3, 5, 7, ..., N?
  2. How could you make y equal to 4 times the reciprocal of z?
  3. How could you make x equal to the sum of the "odd" terms of y (i.e., 4/1, 4/5, 4/9, ...) and w equal to the sum of the "even terms of y (i.e., 4/3, 4/7, ...)?
  4. How can you make v equal to the sum of x and u equal to the sum of w?
  5. How can you make t be the difference between v and u?

5 Commenti

Wayne King
Wayne King il 19 Ott 2011
@daniel yes, you're right. Yours was a much better tactic. I deleted mine in case it wasn't seen yet.
Daniel Shub
Daniel Shub il 19 Ott 2011
Sometimes it is hard to pick up on the homework problems, especially when the answer is simple once you know MATLAB.
shanon
shanon il 19 Ott 2011
Strong bad is the man, but I don't know, my t.a. told us to use a loop. I'm an education major, I have no idea what I'm doing.
Basically what I have to do is ask the user for n (a positive integer), and then calculate pi to that many n. so if n=1, pi=4, for n=2, pi= 4-4/3, for n=3, pi= 4-4/3+4/5, etc.
I was thinking that if i= 1:n, then somehow I could make it that if n=1 the loop went in once&then pi=4. Then if n=2, pi= 4-4/3. I am not on the computer that I have Matlab on, so I am not sure if Steven's solution works yet.
shanon
shanon il 19 Ott 2011
Saw it, sorry, Wayne King, writing it out for myself, so I can see it
n= 0:13;x= ((-1).^n)./(2*n+1);
%approximate pi/4
sum(x)
x1= 4.*x;
%approximate pi
sum(x1)
I don't understand why n is 0:13 though?
@Daniel, thanks for trying to make my life more difficult.... The lab is much more complex than this, all I want is a little help on one thing. You have to go, and ask me five questions? Not what I was looking for.
Daniel Shub
Daniel Shub il 19 Ott 2011
The problem with using a loop for this question is it doesn't really teach you that much about MATLAB. In fact, I would argue that following the steps that I laid out will teach you a lot more about MATLAB than the loop solution.
If you cannot look at Steven's solution and see that it works without running it, you really haven't solved the problem and the homework is only going to get harder.

Accedi per commentare.

Più risposte (2)

Steven
Steven il 19 Ott 2011
clear all; clc;
eps = 50; % precision
piOn4 = 1;
for i = 1:eps
piOn4 = piOn4 + (-1)^(i)*(1/(2*i+1))
end
piOn4

3 Commenti

Sean de Wolski
Sean de Wolski il 19 Ott 2011
Don't overwrite eps!!!!
Jan
Jan il 19 Ott 2011
About the useless "clear all", see: http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301
Daniel Shub
Daniel Shub il 19 Ott 2011
Don't overwrite i!!!!!

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by