# error occur with comment "Subscript indices must either be real positive integers or logicals" while plotting

1 Ansicht (letzte 30 Tage)
Wajahat am 14 Sep. 2018
Kommentiert: Walter Roberson am 15 Sep. 2018
clear all;
close all;
X=-10:1:10;
T=-10:1:10;
C1=2;
C2=1;
b=-1.5;
[x,t]=meshgrid(X,T);
x1=C1*(C1*exp(sqrt(2)*b*x)+C2*exp(-sqrt(2)*b*x)).*sin(1/2*sqrt(-1i.^4*b.^2+4)*t).*(C1*exp(sqrt(2)*b*x)+C2*exp(-sqrt(2)*b*x))+C2*(C1*exp(sqrt(2)*b*x)+C2*exp(-sqrt(2)*b*x)).*cos(1/2*sqrt(-1i.^4*b.^2+4)*t).*(C1*exp(sqrt(2)*b*x)+C2*exp(-sqrt(2)*b*x));
y1=-1/2*C1*(C1*sqrt(2)*exp(sqrt(2)*b*x)-C2*sqrt(2)*exp(-sqrt(2)*b*x)-C1*exp(sqrt(2)*b*x)-C2*exp(-sqrt(2)*b*x)).*cos(1/2*sqrt(-1i.^4*b^2+4)*t).*(C1*sqrt(2)*exp(sqrt(2)*b*x)-C2*sqrt(2)*exp(-sqrt(2)*b*x)-C1*exp(sqrt(2)*b*x)-C2*exp(-sqrt(2)*b*x))*sqrt(-1i*(C1*sqrt(2)*exp(sqrt(2)*b*x)-C2*sqrt(2)*exp(-sqrt(2)*b*x)-C1*exp(sqrt(2)*b*x)-C2*exp(-sqrt(2)*b*x)).^4*b(C1*sqrt(2)*exp(sqrt(2)*b*x)-C2*sqrt(2)*exp(-sqrt(2)*b*x)-C1*exp(sqrt(2)*b*x)-C2*exp(-sqrt(2)*b*x)).^2+4)+1/2*C2*(C1*sqrt(2)*exp(sqrt(2)*b*x)-C2*sqrt(2)*exp(-sqrt(2)*b*x)-C1*exp(sqrt(2)*b*x)-C2*exp(-sqrt(2)*b*x)).*sin(1/2*sqrt(-1i.^4*b^2+4)*t).*(C1*sqrt(2)*exp(sqrt(2)*b*x)-C2*sqrt(2)*exp(-sqrt(2)*b*x)-C1*exp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)-C2*sqrt(2)*exp(-sqrt(2)*b*x)-C1*exp(sqrt(2)*b*x)-C2*exp(-sqrt(2)*b*x));
A=-2*1i*b*x1.*conj(y1);
B=x1.*conj(x1)+y1.*conj(y1);
R=(A./B);
surf(x,t,abs(R))
When I evaluate it, error occur with comment "Subscript indices must either be real positive integers or logicals".
How to remove it?
##### 1 KommentarKeine anzeigenKeine ausblenden
KSSV am 14 Sep. 2018
As y1 expression is very lon..you would have done a typo error. Check your expression carefully.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Walter Roberson am 14 Sep. 2018
C2*exp(-sqrt(2)*b*x)).^4*b(C1*sqrt(2)*exp(sqrt(2)*b*x) is missing an operation between the b and the (C1
##### 2 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Walter Roberson am 15 Sep. 2018
"Subscript indices must either be real positive integers or logicals" has two major causes: either there is a legitimate subscript that is out of range, or else an operation is missing between a variable and the following '(' .
I found the problem by scanning along the expression, examining each () subexpression in turn, looking for places where subscripts were used (if found I would have had to work backwards to determine if they were valid or not) or places where an operation was missing.
I tend to find this kind of work easier by counting open brackets. For example in the initial subexpression
x1=C1*(C1*exp(sqrt(2)*b*x)+C2*exp(-sqrt(2)*b*x)).*sin(1/2*sqrt(-1i.^4*b.^2+4)*t)
I would proceed by either mentally counting or writing down the number of open brackets at each point. In the below, the digit below each ( or ) is the count of the of open brackets in effect at that point
x1=C1*(C1 * exp(sqrt(2)*b*x) + C2*exp(-sqrt(2)*b*x)) .* sin(1/2*sqrt(-1i.^4*b.^2+4)*t)
1 2 3 2 1 2 3 2 10 1 2 1 0
This forces me to look at how the individual ( and ) are being used, bringing my attention to whether each NAME(EXPRESSION) form is involving a standard function such as exp or sqrt, or instead involves a variable . By the time I encountered the b(C1*sqrt(2)*exp(sqrt(2)*b*x) expression, I had seen b enough times to know that it was not expected to be indexed.
Sometimes it can still be difficult to find. At that point it can be worth breaking the line up into subexpressions over multiple lines. You would tend to start that at the level of fully bracketed subexpressions, such as
x1_1 = (C1 * exp(sqrt(2)*b*x) + C2*exp(-sqrt(2)*b*x));
x1_2 = sin(1/2*sqrt(-1i.^4*b.^2+4)*t);
x1 = C1 * x1_1 .* x1_2
This helps isolate the location with the error -- and people find it easier to read long expressions if they are broken up into parts like this.
If you still cannot find it, you might need to go deeper, like
x1_1 = (C1 * exp(sqrt(2)*b*x) + C2*exp(-sqrt(2)*b*x));
x1_2_1 = sqrt(-1i.^4*b.^2+4);
x1_2 = sin(1/2*x1_2_1*t);
x1 = C1 * x1_1 .* x1_2
Eventually you isolate down to figure out where the problem is occurring, after which you can start working to figure out why the problem is occurring.
In the case of legitimate indexing, it is not always easy to figure out why your indices got to be what they are, or to figure out why the indexing is going out of bounds when you were sure you worked everything out logically.
• avoid using variables named sum or min or max or length: if you use any of those (especially sum!) chances are strong that you will accidentally try to apply the relevant operation to a vector or array and end up indexing the variable instead

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on Data Type Identification in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by