incorrect number or types of inputs or outputs for function 'int'

148 visualizaciones (últimos 30 días)
Gary
Gary el 29 de Mzo. de 2023
Comentada: Gary el 31 de Mzo. de 2023
When I put this code with int function, it always says in red: Wrong type or number in function int. Tried to change units, tried all which I could do.
Any advices?
% Define the parameters of the signal
A = 1; % Amplitude
T_0 = 4*pi; % Period
t = linspace(0, T_0, 1000); % Time vector
% Generate the signal
y = A*cos(4*pi*t/T_0);
% Calculate the power of the signal using integration
power = (1/T_0) * int((y).^2, t(1), t(end));
Incorrect number or types of inputs or outputs for function 'int'.
% Plot the signal
plot(t, y);
xlabel("Time (s)");
ylabel("Amplitude");
title("A*cos(4*pi*t/T_0)");
% Display the signal power
disp(['Signal power = ' num2str(power)]);

Respuesta aceptada

Torsten
Torsten el 30 de Mzo. de 2023
Movida: Torsten el 30 de Mzo. de 2023
% Define the parameters of the signal
A = 1; % Amplitude
T_0 = 4*pi; % Period
syms t
% Generate the signal
y = A*cos(4*pi*t/T_0);
% Calculate the power of the signal using integration
power = (1/T_0) * int(y^2)
power = 
power = matlabFunction(power)
power = function_handle with value:
@(t)t.*3.978873577297384e-2+sin(t.*2.0).*1.989436788648692e-2
y = matlabFunction(y)
y = function_handle with value:
@(t)cos(t)
t_num = linspace(0, T_0, 1000); % Time vector
y_num = y(t_num);
% Plot the signal
plot(t_num, y_num);
xlabel("Time (s)");
ylabel("Amplitude");
title("A*cos(4*pi*t/T_0)");
% Display the signal power
power_num = power(t_num(end))-power(t_num(1));
disp(['Signal power = ' num2str(power_num)]);
Signal power = 0.5

Más respuestas (1)

Alan Stevens
Alan Stevens el 29 de Mzo. de 2023
Editada: Alan Stevens el 29 de Mzo. de 2023
Replace your power calculation with
power = (1/T_0) * trapz(t,y.^2);
Edited - Walter is correct.
  4 comentarios
Steven Lord
Steven Lord el 30 de Mzo. de 2023
The int function to perform integration is only defined for symbolic inputs, not numeric inputs. So this works:
syms x
int(x^2)
ans = 
but this doesn't.
y = (1:10).^2;
int(y)
Incorrect number or types of inputs or outputs for function 'int'.
In your code y is a vector of values not a symbolic expression.
Gary
Gary el 31 de Mzo. de 2023
Thank you for your grateful, thoughtful words!!

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by