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

1 view (last 30 days)
In code below, I get error "Subscript indices must either be real positive integers or logicals." when I run MAIN CODE from command line of customrlc(1,2,3). However, when I just run from command line this code:
%R=1;L=2e-3;C=3e-6;
G = tf([1/(L*C)],[1 R/L 1/(L*C)])
bode(G), grid
It works. I'm not sure what is causing the error, any thoughts? Thanks.
MAIN CODE
function customrlc(R,L,C)
close all
%clear all
%clc
A = [0 1/C; -1/L -R/L];
B = [0; 1/L];
Cc = [1 -1];
D = [0];
%Create a Matlab state-space model.
sys = ss(A, B, Cc, D);
close 'all';
%Compute and plot the step response.
%figure;
[y, t] = step(sys);
subplot(2,2,1)
plot(t, y);
grid
xlabel('Time (sec)');
ylabel('Amplitude ');
title('Step Response');
%Compute and plot the impulse response.
%figure;
[y, t] = impulse(sys);
subplot(2,2,2)
plot(t, y);
grid
xlabel('Time (sec)');
ylabel('Amplitude ');
title('Impulse Response');
%Compute and plot the zero-state response to a sinusoidal input.
%figure;
dt = 0.002;
tf = 4;
t = 0 : dt : tf;
u = sin(t);
[ysine, t] = lsim(sys, u, t);
subplot(2,2,3)
plot(t, ysine);
grid
xlabel('Time (sec)');
ylabel('Amplitude ');
title('Sine Response');
%bode response
hold on
figure
%R=1;L=2e-3;C=3e-6;
G = tf([1/(L*C)],[1 R/L 1/(L*C)])
bode(G), grid
end
  2 Comments
Elia
Elia on 7 Jul 2013
G = tf([1/(L*C)],[1 R/L 1/(L*C)]) was the line where the error came from.
After looking at it again, I saw tf is mentioned twice, once in G and once in t=0:dt:tf;
I changed that latter line to t=0:dt:tff and it ran. I'm not sure why it did since the tf is a transfer function call while tf is just something I made up. Anybody have any thoughts? Thanks.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 7 Jul 2013
Edited: Matt J on 7 Jul 2013
I saw tf is mentioned twice
No, it's mentioned 3 times. Presumably you also changed the line
tf=4;
to
tff=4;
  2 Comments
Elia
Elia on 7 Jul 2013
Yes that is correct. Any thoughts as to why changing that affects the tf function itself? When you name a variable as an ordinary function like I just did even without arguments, the variable replaces the function? It seemed like it does.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!