Reputation: 135
I apologize in advance, I'm sure the answer to this question is quite trivial, unfortunately I am just not sure what is going on.
I am trying to run the following code
a(1) = -2;
b(1) = 2;
c(1) = 0;
for i = 1:15
if cubicPoly(a(i))*cubicPoly(c(i)) < 0
a(i+1) = a(i);
b(i+1) = c(i);
else
a(i+1) = c(i);
b(i+1) = b(i);
end
end
but when I do I receive the error message
Index exceeds matrix dimensions.
Error in Assignment2 (line 31)
if cubicPoly(a(i))*cubicPoly(c(i)) < 0
If I evaluate my cubic polynomial function cubicPoly normally, everything works just fine. But inside the for/if loop when evaluated at the sequences a(i) and c(i) it results in an error.
Any help would be appreciated.
Upvotes: 1
Views: 79
Reputation: 2978
It doesn't look like you ever add more elements to c
; when i = 2
, you're trying to access c(2)
in if cubicPoly(a(i))*cubicPoly(c(i)) < 0
, which takes you out of bounds.
You might want to include something like c(i+1) = ...
if you intend to add elements to c
while looping.
Upvotes: 3