Fixing MATLAB Error: P(1,0) Accessing Issue

In summary, the code is trying to evaluate a matrix equation where the column index is not a positive integer.
  • #1
sara_87
763
0

Homework Statement



i'm trying to implement a code into MATLAB but I am getting an error that i think i understand but don't 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.

Homework Equations





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.
 
Physics news on Phys.org
  • #2
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.
 
  • #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.

?
 
  • #4
sara_87 said:
? Attempted to access P(1,0); index must be a positive integer or logical.
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.
 
  • #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?
 
  • #6
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.
 
  • #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?
 
  • #8
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.
 
  • #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
?
 
  • #10
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.
 
  • #11
yes, N was specified, and t(i) are integers from 1 to N+1.
 

1. What does the error message "P(1,0) Accessing Issue" mean?

This error message indicates that there is an issue with accessing the value at position (1,0) in a matrix or array in MATLAB.

2. How can I fix this error?

To fix this error, you will need to check your code and make sure that the dimensions of your matrix or array are correct. If the dimensions are correct, then you may need to check for any indexing errors or syntax mistakes in your code.

3. Why am I getting this error?

This error can occur for a few different reasons. It could be due to a typo or syntax error in your code, incorrect dimensions of your matrix or array, or trying to access a value that does not exist in the matrix or array.

4. Can I prevent this error from happening?

Yes, you can prevent this error by double checking your code for any typos or syntax errors, and making sure that the dimensions of your matrix or array are correct before trying to access values from it.

5. Are there any common mistakes that can cause this error?

Yes, some common mistakes that can cause this error include using incorrect indices to access values from a matrix or array, not properly defining the dimensions of a matrix or array, and forgetting to use parentheses when accessing values from a matrix or array.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
805
  • Engineering and Comp Sci Homework Help
Replies
1
Views
930
  • Introductory Physics Homework Help
Replies
1
Views
980
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
794
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
9
Views
5K
Back
Top