Matlab help

  • MATLAB
  • Thread starter whynot314
  • Start date
  • #1
76
0

Main Question or Discussion Point

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)
 

Answers and Replies

  • #2
11,799
5,405
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
76
0
so your saying place this into another for loop? I am a total noob at this stuff so bare with me.
 
  • #4
11,799
5,405
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
76
0
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
76
0
the problem is a series and i want to use those values where it sums up 0 to 10, then 0 to 20...
 
  • #7
kreil
Insights Author
Gold Member
668
67
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 Threads on Matlab help

  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
7
Views
3K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
3
Views
4K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
6
Views
7K
  • Last Post
Replies
9
Views
3K
  • Last Post
Replies
1
Views
2K
Top