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.
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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
per isakson
on 17 Jan 2013
- allocate memory for dx
- what value has r?
Umit
on 17 Jan 2013
per isakson
on 17 Jan 2013
Edited: per isakson
on 17 Jan 2013
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
Umit
on 17 Jan 2013
Umit
on 17 Jan 2013
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?
Umit
on 19 Jan 2013
Azzi Abdelmalek
on 17 Jan 2013
0 votes
You are using the same counter n in both two for loop
4 Comments
Umit
on 17 Jan 2013
Azzi Abdelmalek
on 17 Jan 2013
Edited: Azzi Abdelmalek
on 17 Jan 2013
In the second loop you are using y for n=2:99, while in the first loop, you've calculated just y(1)
Image Analyst
on 17 Jan 2013
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.
Umit
on 18 Jan 2013
per isakson
on 17 Jan 2013
Edited: per isakson
on 17 Jan 2013
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
Umit
on 18 Jan 2013
per isakson
on 21 Jan 2013
In the on-line help there is a section titled: "Debugging Process and Features".
See http://www.mathworks.se/help/matlab/matlab_prog/debugging-process-and-features.htm or search the local help if you do not run R2012b
per isakson
on 21 Jan 2013
Edited: per isakson
on 21 Jan 2013
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.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!