Calculating PI Values with Vector Input

In summary: Why is the output of this code different than pi?In summary, the difference between the series and pi is:-j=abs(s-l);where s is the sum of all the terms in n, and l is the length of n.
  • #1
whynot314
76
0
I have everything correct, that is when I put in a value for n=10 I get the right results however I want to enter in more than one number at a time to give me three different results like a vector like [ 10 20 30] but I am just clueless as to how to go about this. the dots are there because i thought if i did that it might treat it the input as vector but i guess not I am stuck.

n=input('Enter the number of terms as a vector:');
l=0;
s=pi;
for k=0:1:n
l=l+sqrt(12)*(((-3).^(-k))./(2*k+1));
a=l;
j=abs(s-a);

end
fprintf('\n the diffrence between the series and pi is:%i\n\n',j)
fprintf('\n the diffrence: %g\n\n',j)
 
Physics news on Phys.org
  • #2
Wrap your code in a for loop that counts to three allowing you to enter three and only three inputs.

If you want a variable number of inputs then wrap it in a while loop that stops when say 0 is entered.
 
  • #3
so your saying place this into another for loop? I am a total noob at this stuff so bare with me.
 
  • #4
whynot314 said:
so your saying place this into another for loop? I am a total noob at this stuff so bare with me.

Doesn't that make sense? You can use loops to print hello 10 times so why not loop to get your input and do your computations too.
 
  • #5
I not not supposed to use a while loop just as yet,
I just did this
disp('problem 2')
for z=1:3
n=input('Enter the number of terms as a vector:');
l=0;
s=pi;
for k=0:1:n
l=l+sqrt(12)*(((-3).^(-k))./(2*k+1));
a=l;
j=abs(s-a);

end
fprintf('\n the diffrence between the series and pi is:%i\n\n',j)
end
and I can put in 10, 20, 30 individually and it works nice. but is there a way i do this by just saying=[10 20 30]. if not ill stick with what i have.
 
  • #6
the problem is a series and i want to use those values where it sums up 0 to 10, then 0 to 20...
 
  • #7
1. You're spelling 'diffrence' wrong in the output. It is spelled 'difference'.

2. What is the purpose of these lines:

Code:
l=l+sqrt(12)*(((-3).^(-k))./(2*k+1));
a=l;
j=abs(s-a);

Couldn't they be replaced with,

Code:
l=l+sqrt(12)*(((-3).^(-k))./(2*k+1));
j=abs(s-l);

3. Can you explain more about what you're trying to do here? I'm not sure I get it.

4. If you want this to evaluate each entry of a vector, try this:

Code:
n=input('Enter the number of terms as a vector:');
l=0;
s=pi;
for q=1:length(n)
    for k=0:1:n(q)
        l=l+sqrt(12)*(((-3).^(-k))./(2*k+1));
        j=abs(s-l);
    end
    fprintf('\n the difference between the series and pi is:%i\n\n',j)
end
 

Related to Calculating PI Values with Vector Input

1. What is PI and why is it important in science?

PI, also known as the mathematical constant π, is the ratio of a circle's circumference to its diameter. It is important in science because it is a fundamental constant that is used in many mathematical equations and has numerous applications in physics, engineering, and other scientific fields.

2. How do you calculate PI values with vector input?

To calculate PI values with vector input, you can use the Monte Carlo method. This involves using random numbers to approximate the area of a circle. By repeating this process with a large number of random points, you can calculate an accurate value for PI.

3. Can you provide an example of calculating PI values with vector input?

Sure, let's say we have a circle with a radius of 10 units. We generate a large number of random points within a square with sides of 20 units (2 times the radius). We then count the number of points that fall inside the circle and divide that by the total number of points. This value multiplied by 4 will give us an approximation of PI.

4. What is the benefit of using vector input to calculate PI values?

Using vector input allows for a more efficient and accurate calculation of PI compared to traditional methods. It also allows for a greater level of precision and can be easily adapted for higher dimensions.

5. Are there any limitations to calculating PI values with vector input?

Yes, the accuracy of the calculation is dependent on the quality of the random number generator used and the number of points generated. Additionally, this method can only approximate PI and not provide an exact value.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
708
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
5K
  • Programming and Computer Science
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top