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

1 Ansicht (letzte 30 Tage)
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(sqrt(2)*b*x)-C2*exp(-sqrt(2)*b*x))*sqrt(-1i.*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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?

Akzeptierte Antwort

Walter Roberson
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 Kommentare
Walter Roberson
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.
I would offer this advice:
  • 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
  • avoid using i and j as index variables: if you accidentally miss initializing them in some flow, then they will have the value sqrt(-1) and it can be hard to figure out what is happening. If you use a different variable name, even if it is I and J instead, then you would instead get a clear message about the variable not being defined in that circumstance. Besides, other people reading your code might expect i and j to be the imaginary unit when they are reading your code. You should assume that someone else will need to read your code, or that you will need to read your own code at some later time.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

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