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!

Error in matlab

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

    i'm trying to implement a code into matlab but im getting an error that i think i understand but dont know how to fix it:

    for i=1:n
    f(x)=t(i)-*P(t(i),t0)*f(t0)
    end

    t0 is defined to be 0.
    this is not the whole code, but the error lies in that line, it says:

    ??? Attempted to access P(1,0); index must be a positive integer or logical.

    2. Relevant equations



    3. The attempt at a solution

    I understand that the matrix P has an error because it's trying to evaluate at 0 when it should be a positive integer...? but this is the formula i want to evaluate.
     
  2. jcsd
  3. Oct 26, 2009 #2

    Mark44

    Staff: Mentor

    Is your matrix defined so that the columns are indexed from 1 through some number? If so, that's a problem if you try to access column 0.
     
  4. Oct 27, 2009 #3
    what do you mean?
    how do i define the matrix such that it starts from 1?
    I only said:
    P=zeros(N+1,N+1)
    so that i initialize the matrix to zero.

    ?
     
  5. Oct 27, 2009 #4

    Mark44

    Staff: Mentor

    Based on your error message, the problem seems to be the 0 index. If so, this suggests to me that the statement P=zeros(N+1,N+1) allocates a zero-filled N + 1 by N + 1 matrix. The entry at the upper left corner would be P(1, 1), and the entry at the lower right corner would be P(N+1, N+1). The first row has an index of 1, and the first column has an index of 1, so attempting to access P(0, *) or P(*, 0) would generate an error.
     
  6. Oct 27, 2009 #5
    oh right, i see what you mean..there's no P(0,*) element.

    but my function P is:
    P=t^2+3T
    so for f(x)=t(i)-*P(t(i),t0)*f(t0) i need to evaluate in the matrix P when t is t(i) and T is t0 and t0=0, so how can i do this in matlab?
     
  7. Oct 27, 2009 #6

    Mark44

    Staff: Mentor

    From what you have said, P is a matrix, not a function. Your function appears to be f(x), and the value of f(x) is obtained from a specific cell in your matrix.

    The matrix itself can have whatever values you choose to put in your matrix, but my quick reading of two matlab tutorials lead be to believe that matlab array indexes are 1-based, not 0-based. That means that when you access a matrix, both indexes have to be integers that are 1 or larger. So in particular, if you access your matrix like so -- P(t(i), t0) -- you are attempting to evaluate the matrix entry in the t(i)-th row and t0-th column. If either t(i) or t0 is 0, you'll get the error you showed.

    It seems to me that you're going to need to rethink what you're storing in your matrix. With some more details about what you're doing, I might be able to help. My knowledge of matlab is extremely limited, but I am very proficient in a number of programming languages.
     
  8. Oct 27, 2009 #7
    so if i have:
    f(t(i))=t(i)-P(t(i),t0)*f(t0)
    where t0=0, so the first few terms are:
    f(t(1))=t(1)-P(t(1),0)f(t0)
    f(t(2))=t(2)-P(t(2),0)f(t0)
    etc...
    but how can i put this into matlab?
     
  9. Oct 27, 2009 #8

    Mark44

    Staff: Mentor

    How about this?
    f(t(1))=t(1)-P(t(1),1)*f(t0)
    f(t(2))=t(2)-P(t(2),1)*f(t0)
    etc...
    I.e., use column 1 instead of column 0 (=t0). It would also be helpful to know what the values of t(i) are.
     
  10. Oct 27, 2009 #9
    oh right i see, so P(t(1),1) means that matlab will evaluate the matrix when t=
    so if my matrix P is:

    P=t(i)^2+3T,
    this will evaluate: P(t(i),1st element)=t(i)^2
    ?
     
  11. Oct 27, 2009 #10

    Mark44

    Staff: Mentor

    I think you might be confusing the ideas of function and matrix. Unlike a function, a matrix doesn't do any computations. It is merely a container that can hold numbers (in your case). To store things in the matrix or to retrieve them, you need to provide the indexes.

    Suppose I have this matrix, where P = [1.1 2.0 0; 0 4.5 -1.6; 10.3 -5.1 2.3]

    [tex]P~=~\left[
    \begin{array}{ccc}
    1.1&2.0&0.0\\
    0.0&4.5&-1.6\\
    10.3&-5.1&2.3\\
    \end{array}
    \right]
    [/tex]

    This is a 3X3 matrix. I can access the entry in the upper left corner using P(1, 1). The first index indicates the row (from 1 to 3); the second index indicates the column (from 1 to 3). As an example, P(3, 2) = -5.1.

    It's not clear to me what your indexes are, since I don't know what t(1), t(2), etc. represent. For your problem, these have to be integers from 1 through N + 1, where presumably N was specified earlier.
     
  12. Oct 27, 2009 #11
    yes, N was specified, and t(i) are integers from 1 to N+1.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Error in matlab
  1. Error in integration (Replies: 2)

  2. Integral error (Replies: 4)

  3. Error of a Series (Replies: 2)

Loading...