I keep getting this error Error using sym/subsindex Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variable

t = linspace(-5,5,1000);
f = heaviside(t+1) - heaviside(t-1);
h = f;
% Define tau and compute h(t-tau)
tau = 0.5;
htau = heaviside(t-tau+1) - heaviside(t-tau-1);
% Compute the product f(tau) * h(t-tau)
prod = f .* htau;
% Plot the functions and their product
figure;
plot(t,f,'LineWidth',2);
hold on;
plot(t,htau,'LineWidth',2);
plot(t,prod,'LineWidth',2);
xlabel('t');
ylabel('Amplitude');
title('Convolution Integral Visualization');
legend('f(\tau)', 'h(t-\tau)', 'f(\tau) * h(t-\tau)');
% Define f(t) and h(t)
t = linspace(-5,5,1000);
f = heaviside(t+1) - heaviside(t-1);
h = f;
% Define tau and compute h(t-tau)
tau = 0.5;
htau = heaviside(t-tau+1) - heaviside(t-tau-1);
% Evaluate the convolution integral symbolically
syms tau1;
y = int(f(tau1)*h(t-tau1),tau1,-inf,+inf);
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.
% Evaluate the convolution integral numerically
y_num = conv(f,htau)*(t(2)-t(1));
% Evaluate y for t=0.5
t_index = find(t==0.5);
y_t = y_num(t_index);
% Display the results
disp(['Symbolic result: y(t) = ', char(y)]);
disp(['Numerical result: y(0.5) = ', num2str(y_t)]);

답변 (1개)

f and h are not funtions. They are vectors. Not sure exactly what you are intending your code to do, but you cannot index a vector with a symbolic variable
syms b
a = 1:5;
a(b)
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.

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품

릴리스

R2023a

태그

질문:

2023년 4월 29일

편집:

2023년 4월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by