Solving Matlab Homework Problem - Index Out of Bounds Error

  • Thread starter Thread starter gsb
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The user is encountering an "index out of bounds" error in their MATLAB code when trying to access elements of the array q. The error arises because q is not initialized correctly, leading to its size being one instead of t. A suggested solution is to replace the initialization of q with "q=zeros(1,t)" to ensure it has the correct dimensions. Additionally, it's recommended to use code formatting for better readability in future posts. Proper initialization of arrays is crucial to avoid such errors in MATLAB programming.
gsb
Messages
2
Reaction score
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
Back
Top