Solving Matlab Homework Problem - Index Out of Bounds Error

  • Thread starter Thread starter gsb
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion addresses an "Index Out of Bounds" error encountered in MATLAB code during a homework assignment. The user attempts to access elements of the array 'q' without properly initializing it, leading to the error message: "Attempted to access q(2); index out of bounds because numel(q)=1." The solution provided involves replacing the initialization of 'q' with the command 'q=zeros(1,t)', ensuring that 'q' is correctly defined as an array with 't' elements before the loop begins.

PREREQUISITES
  • Understanding of MATLAB programming syntax and structure.
  • Familiarity with array initialization in MATLAB.
  • Basic knowledge of loops and indexing in MATLAB.
  • Experience with plotting functions in MATLAB.
NEXT STEPS
  • Learn about MATLAB array initialization techniques.
  • Explore MATLAB error handling and debugging methods.
  • Study the use of loops and indexing in MATLAB for efficient coding.
  • Investigate MATLAB plotting functions for visualizing data effectively.
USEFUL FOR

Students learning MATLAB, educators teaching programming concepts, and anyone troubleshooting MATLAB code for homework or projects.

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

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K