index must be positive integer or logical

1 次查看(过去 30 天)
I am using an explicit/implicit scheme for Gravity waves in one dimension and I'm using the following code.
ntot = 100;
jmax = 100;
j = 50;
h=10; dx = 1000; dt = 0.5;
a = (dt*h)/dx;
for j = 1:((n+1)) %Initial conditions
U(:,1)=0;
U(jmax/2,1) =100;
end
for n=1:ntot+1 %Boundary conditions
U(1,n) = 1;
U(n+1,n) = 0;
end
for n = 1:ntot;
for j = 1:jmax-1;
N(j,n+1) = N(j,n)-a*(U(j+1/2,n)-U(j-1/2,n));
U(j+1/2,n) =U(j+1/2,n+1) - B*(N(j,n+1)-N(j+1,n+1));
end
end
hold on
plot(N(:,n+1));
plot (U(:,n));
However, when running it, an error "Attempted to access U(1.5,1); index must be a positive integer or logical." is being displayed. I understand why it is displaying such error (because j is located in the middle of each cell i.e. j is found at 0.5, 1.5, 2.5 etc...and matlab does not read such index. Is there a way how I can solve this problem please. Thanks
  1 个评论
Jan
Jan 2016-3-31
Please use the "{} Code" button to format your code. Currently it is hard to read.

请先登录,再进行评论。

回答(3 个)

KSSV
KSSV 2016-3-31
If you have a array/ matrix, the indices (i,j) must be integers. They cannot be fractions, zeros or negatives. You are accessing U(1.5,1). It is not allowed.
  1 个评论
MF
MF 2016-3-31
Yes I know that, but how can I modify my code so that it reads j (which falls in the middle of n) since j is a disturbance in the middle of the domain..

请先登录,再进行评论。


Jan
Jan 2016-3-31
The intention of your code is not clear. E.g. this is meaningless:
for j = 1:((n+1)) %Initial conditions
U(:,1) = 0;
U(jmax/2,1) = 100;
end
The body of the loop does not depend on the loop counter j, such that you set the the elements n times to the same value. Why?
It is hard to suggest how to solve the problem of the non-integer indices, because the code does not contain any comments which explain the purpose. All we see is a failing code, so how could we fix it? Perhaps you should simply multiply all indices by 2. Or you could replace U(1.5) by (U(1)+U(2))/2, if this satisfies your needs.
  2 个评论
MF
MF 2016-3-31
Ok, it looks like I didn't explained properly.
I've attached the equations I' working on. After arranging them I got these:
N(j,n+1) = N(j,n)-a*(U(j+1/2,n)-U(j-1/2,n)); (a = dt*/dx)
U(j+1/2,n) =U(j+1/2,n+1) - B*(N(j,n+1)-N(j+1,n+1)); (B = dt*g/dx).
Then I had to implement an explicit/implicit scheme and initialize the water level (i.e. j = 50) with an initial disturbance in the middle of the domain.
ntot = 100; %number of time steps
jmax = 100; %size of domain
j = 50; %initial perturbation
h=10; dx = 1000; dt = 0.5;
Since j in the equations (as attached in the image) contains a 1/2, I have no idea how I can solve it. Hope I explained it better now. Thanks
Torsten
Torsten 2016-3-31
I don't understand your problem.
Just save the values you would have stored in U(j+1/2,n) now in U(j+1,n) (i.e. shift the j-index by 1/2 to the right).
Best wishes
Torsten.

请先登录,再进行评论。


Image Analyst
Image Analyst 2016-3-31

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by