I keep on getting the error "Attempted to access y(2); index out of bounds because numel(y)=1. Error in intoff (line 11) e=y(n-1)+y(n);" some one please help.

This is my code:
function y=intoff(r)
dx1 = 100/((1 + r.*(1-r.^(100-1)))/(1-r));
for n=2:100;
dx(n)=r.^(n-1).*dx1;
c=dx(n)+dx(n-1);
y(n-1)=1./(1+c);
for n=2:99;
e=y(n-1)+y(n);
a(n)=1/2*dx(n)*(e+f)
end
end
end

3 Comments

How exactly would I do this as Im very new to Matlab. r is a constant and it could be anything between 1 - 1.2. The function is to find the area under the curve of 1/x by using the trapezium rule for a geometrically increasing width of dx.
It would be enough to say that the error occur with the call
y = intoff( 1.1 );
My quick guess was that r was a vector. That's because of your use of ".*" and ".^". It is common practise to use "*" and "^" with scalars. You do that in
a(n)=1/2*dx(n)*(e+f)

Answers (3)

Your error comes from the fact that you put the closing end of the 1st for loop after the second loop.
Also, are you sure that what you name dx1 is not in fact dx(1), and what is a(n) in the second loop? It seems that you perform a computation that doesn't modify y, which is the only variable that is output-ed by your function.
As mentioned by isakson, you should consider allocating memory for dx and y.

4 Comments

*I have tried closing the 2nd loop after the 1st but it still have me the same error. *dx1 is in fact dx(1) in the second part but it doesn't have an effect on the rest - tried dx(1) as well but still gave me the same error.
I have tried the functon without the second loop and it gives me all the values for y but for some reason it shows error for "e=y(n-1)+y(n);"
What I still cannot figure out is whether you have nested loops or not (I assumed not as you were using the same index for the two loops). So what does your statement or algorithm description say? Do you have to execute two completely separate loops, or is there an inner loop that runs within an outer loop?
Thank you for your help, I tried a lot of things but couldn't get it working, managed to set up a different code with only one loop and get it working.
You are using the same counter n in both two for loop

4 Comments

In the second loop you are using y for n=2:99, while in the first loop, you've calculated just y(1)
To help spot errors like that, type control-a then control-i. It will properly indent and align your code so you can spot nesting errors.
It tells me to change the name of the index so n to something else but I need n and my y values to calculate the value of e?
With n==2 in the inner loop
e=y(n-1)+y(n);
the error occurs because y(2) is not defined at this stage; y is a scalar.
Had you pre-allocated y with zeros you might not have seen the problem as quickly. Pre-allocating with nan exposes such mistakes better.
Matlab has good debugging features. Try
dbstop if error

3 Comments

In the on-line help there is a section titled: "Debugging Process and Features".
To use the same name for the counter in the outer and in the inner loop is very confusing to me. Try
for n=2:5
fprintf( 'outer loop %d\n', n )
for n=2:4
fprintf( 'inner loop %d\n', n )
end
fprintf( 'outer loop %d\n', n )
end

This question is closed.

Asked:

on 17 Jan 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!