View Full Version : matlab
sara_87
Oct28-09, 10:44 AM
1. The problem statement, all variables and given/known data
I want to type in matlab form, the following:
a=3\sum^{i-1}_{j=1}p(t_i,t_j)y(t_j)
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?
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.
sara_87
Oct28-09, 11:12 AM
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.
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).
sara_87
Oct28-09, 02:23 PM
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.
This should do the trick for you. You need to put the values you need in the t() array.
% 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.
i initialized them to 0 and the error is now gone
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.
p=t^2+3t
p(t(i+1),t(i+1))=('p',t(i))
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.
sara_87
Oct28-09, 03:20 PM
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)).
sara_87
Oct28-09, 03:38 PM
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.
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.
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.
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).
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.
'p' means it is using the function p (defined elsewhere) to evaluate p(t(i+1),t(i+1)).
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.
sara_87
Oct28-09, 03:57 PM
sorry about many confusions, i think im confused too. but...basically, this is what i need:
a(t(i))=b+c
where:
b=\sum^{i-1}_{j=1}p(t_i,t_j)a(t_j)
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?
sorry about many confusions, i think im confused too.
:smile:
but...basically, this is what i need:
a(t(i))=b+c
where:
b=\sum^{i-1}_{j=1}p(t_i,t_j)a(t_j)
and
c=p(t(i),t(i))
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?
p(t(i),t(i))=t(i)^2+3t(i).
p(t(i),t(j))=t(i)^2+3t(j).
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.
that's why i need both of them.
am i making a mistake?
sara_87
Oct28-09, 04:31 PM
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.
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.
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.
vBulletin® v3.7.6, Copyright ©2000-2009, Jelsoft Enterprises Ltd.