"Array indicies must be positive integers" error

2 visualizzazioni (ultimi 30 giorni)
Kevin Bodell
Kevin Bodell il 8 Apr 2021
Commentato: Kevin Bodell il 8 Apr 2021
Below is my code, which returns the "Index in position 1 is invalid. Array indices must be positive integers or logical values." This error stems from the last lines of code under the label "%Boundary Conditions." Currently, I have the for loop running t from 0 to 200 by increments of 1. I hoped to have it run from 0 to 0.2 by increments of 0.001, but changed it to whole number values hoping to eliminate the error but unfortunately it did not help. Any suggestions are appreciated!
clc
clear all
close all
%Setup:
N = 101;
cells = N-1;
dx = 0.01;
dt = 0.001;
t = 0:dt:0.2;
ui = 1;
uf = 0;
a = 2.5;
alpha = 0.005;
tf = 0.2;
c = (alpha*dt)/dx;
d = (alpha*dt)/(dx^2);
Re = (a*dx)/alpha;
n = 0:0.05:1;
L = 1;
%Stability Conditions (FTCS):
mustBeLessThan(d,0.5);
mustBeLessThan(Re, L/c);
%Initial Conditions:
if n<=0.2
U(n,0) = 1;
end
if n == 0.2
U(n,0) = 0.5;
end
if n>=0.2
U(n,0) = 0;
end
%Boundary Conditions:
%The 200 values of "t" below represent the times starting from time t=0 sec
%increasing by an interval of 0.001 sec up until 0.2 sec.
for t = 0:1:200
U(0,t) = ui;
U(L,t) = uf;
end

Risposte (3)

Steven Lord
Steven Lord il 8 Apr 2021
The first element / row / column / etc. of an array in MATLAB is element / row / column / etc. 1.
There is no element / row / column / etc. 0 in an array in MATLAB.
You should also be very wary about doing exact equality testing of floating point numbers in MATLAB using ==.
n1 = 0.1;
n = n1 + n1 + n1;
if n == 0.3
disp('Yes')
else
disp('No')
end
No
difference = n - 0.3 % very small but not exactly 0
difference = 5.5511e-17
0.1 is not exactly one-tenth much like 0.33333333333333333333 is not exactly one-third.

Star Strider
Star Strider il 8 Apr 2021
Note that 0 is not a positive integer. Positive integers are integers greater than 0.
That is likely also going to be a problem with ‘U’ earlier in the code as well. (I also don’t understand other parts of the posted code. I’m somewhat surprised that it doesn’t throw more errors that those reported.)

James Tursa
James Tursa il 8 Apr 2021
You can't have 0 or fractional indexing in MATLAB. Take this section of code for example:
for t = 0:1:200
U(0,t) = ui;
U(L,t) = uf;
end
You can't use the t=0 value as an index. And if you wanted to change your t vector to have fractional values, you couldn't use those as indexes either. The correct way to code this would be to create a t vector and then size U accordingly. Similar situation applies to the 0 and L. There would be an x array of some size, and the first dimension of U would correspond to that. E.g.,
t = 0:1:200;
nt = numel(t);
x = 0:whatever:L;
nx = numel(x);
for k=1:nt
U(1,k) = ui;
U(nx,k) = uf;
end
Or you could do this without a loop:
U = zeros(nx,nt);
U(1,:) = ui;
U(end,:) = uf;
  1 Commento
Kevin Bodell
Kevin Bodell il 8 Apr 2021
Using the numer of elements worked, I appreciate it! I originally wanted to do something similar to this but was unaware how. Cheers!

Accedi per commentare.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by