Index must be a positive integer or logical

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?

Answers (1)

Mischa Kim
Mischa Kim on 12 Jun 2014
Edited: Mischa Kim on 12 Jun 2014
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.

This question is closed.

Asked:

on 12 Jun 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!