MATLAB Calculating PI Values with Vector Input

  • Thread starter Thread starter whynot314
  • Start date Start date
  • Tags Tags
    Input Pi Vector
AI Thread Summary
The discussion focuses on calculating PI values using vector input in a programming context. The user successfully computes results for single values but seeks to input multiple values simultaneously, such as a vector like [10, 20, 30]. Suggestions include using nested loops to iterate through the vector elements for calculations. The user is also advised to correct spelling errors in the output and consider simplifying their code for efficiency. Ultimately, a solution is provided to evaluate each entry of the vector and compute the differences from PI.
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

Back
Top