1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Type in matlab form

  1. Oct 28, 2009 #1
    1. The problem statement, all variables and given/known data

    I want to type in matlab form, the following:

    [tex]a=3\sum^{i-1}_{j=1}p(t_i,t_j)y(t_j)[/tex]

    2. Relevant equations


    3. The attempt at a solution

    for i=1:n
    t(i+1)=3*i
    for j=1:i-1
    a=3*p(t(i),t(j))*y(t(j))
    end
    end


    Im getting the error:
    ??? Attempted to access p(1,0); index must be a positive integer or logical.

    how can i fix this?
     
  2. jcsd
  3. Oct 28, 2009 #2

    Mark44

    Staff: Mentor

    Re: matlab

    This is very similar to the other problem you posted in the Calculus and Beyond section, and the error you're getting here is exactly the same. The reason you're getting this error is that t(j) = 0 for some value of j.

    Your inner loop runs from j = 1 to j = i -1. When i = 1 in the outer loop, j runs from 1 to 0, which is probably not the behavior you want. One way around this might be to treat i = 1 as a special case, and do what needs to be done, then have your outer loop run from i = 2 to n.
     
  4. Oct 28, 2009 #3
    Re: matlab

    im understanding the problem, but cant fix it somehow.
    i changed i to:
    for i=2:n
    t(i+1)=3*i
    for j=1:i-1
    a=3*p(t(i),t(j))*y(t(j))
    end
    end

    but still get the error:

    ??? Attempted to access p(1,0); index must be a positive integer or logical.
     
  5. Oct 28, 2009 #4

    Mark44

    Staff: Mentor

    Re: matlab

    The problem has to be that for some value of j, t(j) = 0, and you're trying to access p(<something>, t(j)).

    Let's hand-trace what's going on.
    i = 2
    t(3) = 3*2 = 6
    j = 1
    a = 3*p(t(2), t(1)) * y(t(1))
    <inner loop ends>



    It looks to me like t(1) never got properly initialized, so is probably zero. Since you outer loop initializes only t(3), t(4), ..., t(n + 1), you'll need to explicitly assign values to t(1) and t(2).
     
  6. Oct 28, 2009 #5
    Re: matlab

    oh right, that makes sense. i initialized them to 0 and the error is now gone :)

    I have:

    p=t^2+3t
    p(t(i+1),t(i+1))=('p',t(i))

    and when i execute this, i get:
    a matrix with 0 everywhere except at the diagonal, matrix a is:
    0,0,0,0,0
    0,4,0,0,0
    0,0,10,0,0
    0,0,0,18,0
    0,0,0,0,0

    so the diagonal terms are correct.. but i dont want this matrix, i want:

    4,7,10,...
    7,10,18,...

    etc. so basically i want [t(i)]^2+3[t(j)] where here i is the row number and j is the column number.
    how can i fix this?

    thank you by the way.
     
  7. Oct 28, 2009 #6

    Mark44

    Staff: Mentor

    Re: matlab

    This should do the trick for you. You need to put the values you need in the t() array.
    Code (Text):

    % Code to initialize the t(i) array not shown

    N = 5  % or whatever
    for i = 1:N
       for j = 1:N
          P(i, j) = t(i)^2 + 3*t(j)
       end
    end
     
    Some things you said in your last post I don't understand.
    If you initialize t(1) and t(2) to zero, I don't see how that fixes the problem. When you try to access p(<something>, t(1)), you're still trying to access something in the zero column, which was the problem all along.

    In the first line above, I don't know enough about matlab to know if you can assign the value of some expression (t^2 + 3t) to a matrix.
    In the second line, what does 'p' mean? I would think it's just a single character, 'p', completely unrelated to your matrix.
     
  8. Oct 28, 2009 #7
    Re: matlab

    because when i initialised only t(1) to zero and did: p(something, 1) as you advised from before...and this got rid of the error.

    i think you're right about assigning some expression to a matrix...thats my problem now: i want to define: p=t(i)^2+3t(i).
    and i also need: p=t(i)^2+3t(j) (which is a matrix).

    'p' means it is using the function p (defined elsewhere) to evaluate p(t(i+1),t(i+1)).
     
  9. Oct 28, 2009 #8
    Re: matlab

    i did:
    Code:
    % Code to initialize the t(i) array not shown

    N = 5 % or whatever
    for i = 1:N
    for j = 1:N
    P(i, j) = t(i)^2 + 3*t(j)
    end
    end

    and it gave an error:
    ??? Attempted to access t(2); index out of bounds because numel(t)=1.

    i have no idea what this means.
     
  10. Oct 28, 2009 #9

    Mark44

    Staff: Mentor

    Re: matlab

    So initializing t(1) had nothing to do with getting rid of the error. Instead of p(something, t(1)), you changed to p(something, 1). That's what got rid of the error.
    Why do you want to do both of these things? Your matrix can hold only one set of values at a time. The code I provided should do the second thing, above. Decide which it is that you want to do, and I can help you with the code.
    OK, now I'm confused. Is p a function or a matrix? I mentioned in the other thread that I thought you might be confusing these two very different concepts. If you actually have a function that evaluates your matrix, you should at the very least give it a name that's different from your matrix.

    I mentioned before, that I know very little about matlab, but a lot about programming. If you don't have some reference manual to look at, here's a link to one I've been using: http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/getstart.pdf. Section 2-16 ff talks about arrays, as do sections 4-9 and 4-11. Sections 4-20, 4-21, and 4-22 would be a good start on understanding functions.
     
  11. Oct 28, 2009 #10
    Re: matlab

    sorry about many confusions, i think im confused too. but...basically, this is what i need:


    a(t(i))=b+c
    where:
    [tex]b=\sum^{i-1}_{j=1}p(t_i,t_j)a(t_j)[/tex]
    and
    c=p(t(i),t(i))

    p(t(i),t(i))=t(i)^2+3t(i).
    p(t(i),t(j))=t(i)^2+3t(j).

    that's why i need both of them.
    am i making a mistake?
     
  12. Oct 28, 2009 #11

    Mark44

    Staff: Mentor

    Re: matlab

    :smile:
    So b and c are just scalars, right? I.e., they aren't lists of numbers or matrices with numbers in them. And a is just a one-dimensional array, right?
    Your first formula is redundant. At the diagonal elements of your matrix, t(i) = t(j), so the second formula automatically gives you what the first formula does at any matrix element p(i, j) where the row number equals the column number. Or am I completely missing what you're trying to say.

    The code I gave earlier accomplishes what I think you want.
     
  13. Oct 28, 2009 #12
    Re: matlab

    yes, you're understanding my problem.
    but,

    for i=1:n
    for j=1:i-1
    p=t(i)^2 + 3*t(j)
    b=p(t(i),t(j))*a(t(j))
    end
    end

    gives b as a vector of all zeros.
     
  14. Oct 28, 2009 #13

    Mark44

    Staff: Mentor

    Re: matlab

    First, I'm not sure what this line means: p=t(i)^2 + 3*t(j)
    I think what you want is this: p(i, j) = t(i)^2 + 3*t(j)
    Second, the line above assumes that you have known values in your array t. IOW, you need to put some values in the array t before you use it to calculate values to be stored in another array.
    Third, b should be an array (dimension 1), otherwise the value stored in a previous iteration of the loop will be overwritten. More importantly, the array a needs to have values stored in it before you use it to compute other values.
    Fourth, you are storing something -- 0 apparently -- in b somewhere around n^2 times, which is way too many times. I don't believe that line should be inside your inner loop, but maybe should be in the outer loop after the first end statement.

    Can you show me your complete script? I need to know how every variable is being initialized.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Type in matlab form
  1. Type A Chopper (Replies: 10)

  2. Serializing A Type (Replies: 2)

  3. Resistor Types (Replies: 3)

Loading...