Error using indexing Invalid indexing or function definition. Indexing must follow MATLAB indexing.

12 views (last 30 days)
I encounter "Error using indexing
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic
variables, and function body must be sym expression.
Error in modified_signal (line 54)
B(w) = fourier(b(t), w);" with this script when using symbolic function, the signal A(w), a(t), and b(t) do not have any issue, but B(w) has caused the error. Any solution to overcome this issue? Thank you.
%create symbolic functions x, a, b, c, d, e, f_c with independent variable t
syms x(t) a(t) h(t) b(t) c(t) d(t) e f_c(t) f_c1(t) f_c2(t) t tau
%create symbolic functions A, B, C, D, and E with independent variable w
syms A(w) B(w) C(w) D(w) E(w) w
x(t) = cos(100*pi*t);
a(t) = x(0.4*t);
h(t) = dirac(t-0.02);
b(t) = int(a(tau)*h(t-tau), 'tau', -inf, inf);
f_c(t) = 10*cos(2500*pi*t);
f_c1(t) = f_c(t);
f_c2(t) = f_c(t);
c(t) = b(t)*f_c1(t);
d(t) = c(t)*f_c2(t);
figure
subplot (2,1,1)
fplot(a(t))
xlim([-0.05 0.05]),ylim([-1.5 1.5])
title ('Time domain of signal a(t)')
xlabel('Time, t')
ylabel('Amplitude, a(t)')
grid on
A(w) = fourier(a(t), w);
w = -60*pi:0.1*pi:60*pi;
subsA = A(w);
% Replace Inf value with suitable value
idx = subsA == Inf;
subsA(idx) = pi; % choose suitable value from the expression of fourier transform.
subplot (2,1,2)
plot(w,real(subsA));
ylim([0 3.5])
title ('Time domain of signal A(\omega)')
ylabel("\Re(A(\omega))");
xlabel("\omega");
xticks(-40*pi:20*pi:40*pi);
xticklabels({'-40\pi', '-20\pi', '0', '20\pi', '40\pi'})
figure
subplot(2,1,1)
fplot(b(t))
xlim([-0.05 0.05]),ylim([-1.5 1.5])
title ('Time domain of signal b(t)')
xlabel('Time, t')
ylabel('Amplitude, b(t)')
grid on
B(w) = fourier(b(t), w);
Error using indexing
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
w = -60*pi:0.1*pi:60*pi;
subsB = B(w);
% Replace Inf value with suitable value
idx = abs(subsB) == Inf;
subsB(idx) = pi; % choose suitable value from the expression of fourier transform.
subplot (2,1,2)
plot(w,real(subsB));
ylim([0 3.5])
title ('Time domain of signal B(\omega)')
ylabel("\Re(B(\omega))");
xlabel("\omega");
xticks(-40*pi:20*pi:40*pi);
xticklabels({'-40\pi', '-20\pi', '0', '20\pi', '40\pi'})
figure
subplot(2,1,1)
fplot(c(t))
xlim([-0.05 0.05]),ylim([-12 12])
title ('Time domain of signal c(t)')
xlabel('Time, t')
ylabel('Amplitude, c(t)')
grid on
figure
subplot(2,1,1)
fplot(d(t))
xlim([-0.1 0.1]),ylim([-110 110])
title ('Time domain of signal d(t)')
xlabel('Time, t')
ylabel('Amplitude, d(t)')
grid on

Answers (1)

VBBV
VBBV on 27 Dec 2022
%create symbolic functions x, a, b, c, d, e, f_c with independent variable t
syms x(t) a(t) h(t) b(t) c(t) d(t) e f_c(t) f_c1(t) f_c2(t) t tau
%create symbolic functions A, B, C, D, and E with independent variable w
syms A(w) B(w) C(w) D(w) E(w) w
x(t) = cos(100*pi*t);
a(t) = x(0.4*t);
h(t) = dirac(t-0.02);
b(t) = int(a(tau)*h(t-tau), 'tau', -inf, inf)
b(t) = 
f_c(t) = 10*cos(2500*pi*t);
f_c1(t) = f_c(t);
f_c2(t) = f_c(t);
c(t) = b(t)*f_c1(t);
d(t) = c(t)*f_c2(t);
figure
subplot (2,1,1)
fplot(a(t))
xlim([-0.05 0.05]),ylim([-1.5 1.5])
title ('Time domain of signal a(t)')
xlabel('Time, t')
ylabel('Amplitude, a(t)')
grid on
A(w) = fourier(a(t), w);
w = -60*pi:0.1*pi:60*pi;
subsA = A(w);
% Replace Inf value with suitable value
idx = subsA == Inf;
subsA(idx) = pi; % choose suitable value from the expression of fourier transform.
subplot (2,1,2)
plot(w,real(subsA));
ylim([0 3.5])
title ('Time domain of signal A(\omega)')
ylabel("\Re(A(\omega))");
xlabel("\omega");
xticks(-40*pi:20*pi:40*pi);
xticklabels({'-40\pi', '-20\pi', '0', '20\pi', '40\pi'})
figure
subplot(2,1,1)
fplot(b(t))
xlim([-0.05 0.05]),ylim([-1.5 1.5])
title ('Time domain of signal b(t)')
xlabel('Time, t')
ylabel('Amplitude, b(t)')
grid on
syms w
B(w) = fourier(b(t), w);
w = -60*pi:0.1*pi:60*pi;
subsB = B(w);
% Replace Inf value with suitable value
idx = abs(subsB) == Inf;
subsB(idx) = pi; % choose suitable value from the expression of fourier transform.
subplot (2,1,2)
plot(w,real(subsB));
ylim([0 3.5])
title ('Time domain of signal B(\omega)')
ylabel("\Re(B(\omega))");
xlabel("\omega");
xticks(-40*pi:20*pi:40*pi);
xticklabels({'-40\pi', '-20\pi', '0', '20\pi', '40\pi'})
figure
subplot(2,1,1)
fplot(c(t))
xlim([-0.05 0.05]),ylim([-12 12])
title ('Time domain of signal c(t)')
xlabel('Time, t')
ylabel('Amplitude, c(t)')
grid on
figure
subplot(2,1,1)
fplot(d(t))
xlim([-0.1 0.1]),ylim([-110 110])
title ('Time domain of signal d(t)')
xlabel('Time, t')
ylabel('Amplitude, d(t)')
grid on

Community Treasure Hunt

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

Start Hunting!