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
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
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 Kommentare
per isakson
am 17 Jan. 2013
- allocate memory for dx
- what value has r?
Umit
am 17 Jan. 2013
per isakson
am 17 Jan. 2013
Bearbeitet: per isakson
am 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)
Antworten (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 Kommentare
Umit
am 17 Jan. 2013
Umit
am 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
am 19 Jan. 2013
Azzi Abdelmalek
am 17 Jan. 2013
0 Stimmen
You are using the same counter n in both two for loop
4 Kommentare
Umit
am 17 Jan. 2013
Azzi Abdelmalek
am 17 Jan. 2013
Bearbeitet: Azzi Abdelmalek
am 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
am 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
am 18 Jan. 2013
per isakson
am 17 Jan. 2013
Bearbeitet: per isakson
am 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 Kommentare
Umit
am 18 Jan. 2013
per isakson
am 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
am 21 Jan. 2013
Bearbeitet: per isakson
am 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
Diese Frage ist geschlossen.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!