Solving Matlab Homework Problem - Index Out of Bounds Error

  • Thread starter gsb
  • Start date
  • Tags
    Matlab
In summary, the conversation is about a problem with a code in Matlab. The code involves variables such as t, H, h, n, qqq, a, b, k, Lo, and g. The person is getting an error when trying to access q(2), and is seeking help to fix the issue. A solution provided is to replace certain lines of the code and use the [ code ] tags for better readability.
  • #1
gsb
2
0

Homework Statement


hi, i am having some problem with a code in matlab. my code is:

t = 30;
H= 30;
h= 3;
n = H/h;
qqq= 10;
a= 3;
b = .2;
k= 0.04;
Lo= 100;
g= 9.81;

for i = 1:t
q(i) = 0;

for j = 1:n
nl = qqq + (j*h)/(a + b*(j*h));
nlt = nl*(0.0172*log(i) + 1);
q(i) = q(i) + 2*k*(Lo/100)*(nlt/3)*g*exp(-k*(i));

end

plot((1:t), q);

end




Homework Equations


this is the code and i keep getting this error- "Attempted to access q(2); index out of bounds because numel(q)=1."

help would be greatly appreciated.
thanks



The Attempt at a Solution

 
Physics news on Phys.org

1. What does the "Index Out of Bounds" error mean in Matlab?

The "Index Out of Bounds" error in Matlab means that you are trying to access an element of an array or matrix that does not exist. This can happen when you try to access an index that is larger than the size of the array, or when you try to access a negative index.

2. How can I debug an "Index Out of Bounds" error in Matlab?

To debug an "Index Out of Bounds" error in Matlab, you can use the "dbstop if error" command, which will pause the execution of your code when an error occurs. This will allow you to inspect the values of your variables and see where the error is occurring.

3. What are some common causes of "Index Out of Bounds" errors in Matlab?

Some common causes of "Index Out of Bounds" errors in Matlab include trying to access an index that is larger than the size of the array, using a negative index, or using non-integer indices. These errors can also occur when working with multidimensional arrays and matrices.

4. How can I avoid "Index Out of Bounds" errors in Matlab?

To avoid "Index Out of Bounds" errors in Matlab, you should make sure to double check your indices when accessing elements of arrays or matrices. It can also be helpful to use built-in functions such as "length" or "size" to determine the size of your array before trying to access specific indices.

5. Can I fix an "Index Out of Bounds" error without changing my code?

In most cases, an "Index Out of Bounds" error in Matlab can only be fixed by changing your code. However, you can also use the "exist" function to check if a certain index exists before trying to access it, and handle the error accordingly. Additionally, using try-catch statements can help you handle these errors in a more efficient way.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
806
  • Engineering and Comp Sci Homework Help
Replies
2
Views
825
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
939
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
881
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
954
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
Back
Top