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
per isakson
2013년 1월 17일
- allocate memory for dx
- what value has r?
Umit
2013년 1월 17일
per isakson
2013년 1월 17일
편집: per isakson
2013년 1월 17일
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)
답변 (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
Umit
2013년 1월 17일
Umit
2013년 1월 17일
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
2013년 1월 19일
Azzi Abdelmalek
2013년 1월 17일
0 개 추천
You are using the same counter n in both two for loop
댓글 수: 4
Umit
2013년 1월 17일
Azzi Abdelmalek
2013년 1월 17일
편집: Azzi Abdelmalek
2013년 1월 17일
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
2013년 1월 17일
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
2013년 1월 18일
per isakson
2013년 1월 17일
편집: per isakson
2013년 1월 17일
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
Umit
2013년 1월 18일
per isakson
2013년 1월 21일
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
2013년 1월 21일
편집: per isakson
2013년 1월 21일
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
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!