Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab help

  1. Mar 1, 2013 #1
    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)
     
  2. jcsd
  3. Mar 1, 2013 #2

    jedishrfu

    Staff: Mentor

    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.
     
  4. Mar 1, 2013 #3
    so your saying place this into another for loop? I am a total noob at this stuff so bare with me.
     
  5. Mar 1, 2013 #4

    jedishrfu

    Staff: Mentor

    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.
     
  6. Mar 1, 2013 #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.
     
  7. Mar 1, 2013 #6
    the problem is a series and i want to use those values where it sums up 0 to 10, then 0 to 20...
     
  8. Mar 3, 2013 #7

    kreil

    User Avatar
    Gold Member

    1. You're spelling 'diffrence' wrong in the output. It is spelled 'difference'.

    2. What is the purpose of these lines:

    Code (Text):
    l=l+sqrt(12)*(((-3).^(-k))./(2*k+1));
    a=l;
    j=abs(s-a);
    Couldn't they be replaced with,

    Code (Text):
    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 (Text):
    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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab help
  1. Matlab Help (Replies: 0)

  2. MATLAB help (Replies: 9)

  3. MATLAB help (Replies: 2)

  4. MATLAB help (Replies: 0)

  5. MATLAB help (Replies: 7)

Loading...