MATLAB: ??? Attempted to access G(2); index out of bounds because numel(G)=1.

??? attempted to access g(2); index out of bounds because numel(g)=1.

Hello, I am trying to do the below calculation and I keep getting this error. I need it to compare the difference of subsuquent calculations and repeat the loop until their difference is less than 0.0001. clear all; R=2.4; P=.1; eta= 10e-19; k=1; G(k)= R* eta /.1 Q=.5 k=k+1 while(G(k) – G(k- 1) > .0001) G(k) = G(k+1)*{1-(G(k)+1)/(G(k+1)+G(k))}+ Q k=k+1;
end
??? Attempted to access G(2); index out of bounds because numel(G)=1.
Error in ==> homework4 at 9 while(G(k) – G(k- 1) > .0001)

Best Answer

do you know how to bebug? highlight the line before the while loop in editor and hit F12 on keyboard. Run your code. Inspect your variables - see if you can understand the error message.
As this is homework I'm not going to tell you the answer - this is an attempt to guide you to find the answer yourself - you will learn more that way...