Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Index must be a positive integer or logical

조회 수: 1 (최근 30일)
Sebastiaan Klaver
Sebastiaan Klaver 2014년 6월 12일
마감: MATLAB Answer Bot 2021년 8월 20일
I am getting the following error while running the code:
Attempted to access r(8.5); index must be a positive integer or logical.
Error in L158 (line 15) A(1,1)=r(n+0.5)/(h^2)+r(n-0.5)/(h^2)+a*r(n)
This is the line of code:
h=0.2
n=1.8/h -1
a=1
c=0.25
r=linspace(0.2+h,2,n+1)
A=zeros(n+1,n+1)
b=zeros(n+1,2)
A(1,1)=r(n+0.5)/(h^2)+r(n-0.5)/(h^2)+a*r(n)
Can someone help me with this?

답변 (1개)

Mischa Kim
Mischa Kim 2014년 6월 12일
편집: Mischa Kim 2014년 6월 12일
Sebastiaan, the problem is that in
A(1,1) = r(n+0.5)/(h^2)+r(n-0.5)/(h^2)+a*r(n)
you are trying to access
r(8.5) % n = 8
Array indices need to be integers (or logicals, as pointed out in the error msg), so you could use:
A(1,1) = r(n+1)/(h^2)+r(n-1)/(h^2)+a*r(n)
provided it does what you need it to do.

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by