Calculating PI Values with Vector Input

  • Context: MATLAB 
  • Thread starter Thread starter whynot314
  • Start date Start date
  • Tags Tags
    Input Pi Vector
Click For Summary

Discussion Overview

The discussion revolves around calculating values of π using a series expansion, specifically focusing on how to modify the code to accept vector inputs for multiple calculations simultaneously. Participants explore different programming approaches to achieve this goal.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant expresses a desire to input multiple values at once as a vector, such as [10 20 30], rather than entering them individually.
  • Another suggests wrapping the existing code in a for loop to handle a fixed number of inputs or using a while loop for a variable number of inputs.
  • There is a discussion about the necessity of additional loops to process each input value, with one participant seeking clarification on this approach.
  • A participant shares a modified version of the code that allows for individual inputs but questions whether it can be further simplified to accept a vector directly.
  • Concerns are raised about the spelling of 'difference' in the output and the efficiency of certain lines of code, suggesting potential simplifications.
  • One participant proposes a code structure that evaluates each entry of a vector, indicating a possible solution to the original problem.

Areas of Agreement / Disagreement

Participants generally agree on the need to modify the code to handle vector inputs, but there are differing opinions on the best approach to implement this. The discussion remains unresolved regarding the optimal solution.

Contextual Notes

Some participants express uncertainty about the purpose of specific lines of code and whether they can be simplified. There is also a lack of consensus on the best method to implement vector input handling.

whynot314
Messages
76
Reaction score
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
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.
 
so your saying place this into another for loop? I am a total noob at this stuff so bare with me.
 
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.
 
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.
 
the problem is a series and i want to use those values where it sums up 0 to 10, then 0 to 20...
 
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
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K