Info

This question is closed. Reopen it to edit or answer.

Error:Subscript indices must either be real positive integers or logicals

1 view (last 30 days)
I am fairly new to MATLAB and I am still learning the tricks to it. In this program I am trying to make, I want the program to calculate average power in two separate (but similar in design) AC circuits with 2 impedance and then plot these average power values vs. x. One impedance is a variable impedance where one impedance has its phase (x) varying from -50 degrees to 50 degrees and the other circuit contains the complex conjugate of this variable impedance. To keep these 2 circuits separate, I defined 2 different average power values to be calculated,(Pavg1 and Pavg2). I defined x to be a range from -50 to 50 with increments at 0.01. When I run my program, I get the error seen above and I am not certain as to why. Below is my code:
x = -50:0.01:50;
Zs = 20 + (20i);
Z01(x) = 20 + (x*1i);
Z02(x) = 20 - (x*1i);
Ztotal1(x) = Zs + Z01;
Ztotal2(x) = Zs + Z02;
I1(x) = 240/Ztotal1;
I2(x) = 240/Ztotal2;
Pavg1(x) = (240*I1(x))/2;
Pavg2(x) = (240*I2(x))/2;
Plot(x,Pavg1(x));
Plot(x,Pavg2(x))
And MATLAB says that my error is on Line 3 (Z01(x)).
Any help for a semi-beginner to MATLAB is appreciated. Thank you ahead of time for any help.

Answers (2)

Matt J
Matt J on 22 Sep 2014
Edited: Matt J on 22 Sep 2014
x contains non-integer and negative numbers, so the indexing expression Z01(x) makes no sense. Presumably you really meant,
Z01 = 20 + (x*1i);
  1 Comment
John
John on 22 Sep 2014
Edited: John on 22 Sep 2014
Thank you. I got rid of all of my (x) indexes and fixed a few things on the code. My new code looks likes this:
x = -50:0.01:50;
Zs = 20 + (20i);
Z01 = 20 + (x*1i);
Z02 = 20 - (x*1i);
Ztotal1 = Zs + Z01;
Ztotal2 = Zs + Z02;
I1 = 240./Ztotal1;
I2 = 240./Ztotal2;
Pavg1 = (240*I1)/2;
figure;
plot(Pavg1);
Pavg2 = (240*I2)/2;
figure;
plot(Pavg2);
It runs now, but I am still having a problem. I'm only getting one graph, not two. I need a graph of Pavg1 and a graph of Pavg2. Also, I need both of the graphs to be plotted against x. Can you explain to me how I do that? Thank you again for your help.

Iain
Iain on 22 Sep 2014
Edited: Iain on 22 Sep 2014
The simple answer, which should work is to drop all of the (x) you've got.
The reason being that it looks like you're confusing a "function" with a "vector".
To define a function, on the fly:
Z01 = @(x) (20+x*i); %The second load of () are optional, but I find they make it clearer.
Z01(-5.45641) will then give you an answer
  3 Comments
John
John on 22 Sep 2014
Thank you. I am still having a problem though. Please see my response to the above answer from "Matt J" for a description of the problem.

Community Treasure Hunt

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

Start Hunting!