incorrect number or types of inputs or outputs for function 'int'
조회 수: 149 (최근 30일)
이전 댓글 표시
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));
% 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)]);
댓글 수: 0
채택된 답변
Torsten
2023년 3월 30일
이동: Torsten
2023년 3월 30일
% 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 = matlabFunction(power)
y = matlabFunction(y)
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)]);
댓글 수: 0
추가 답변 (1개)
Alan Stevens
2023년 3월 29일
편집: Alan Stevens
2023년 3월 29일
Replace your power calculation with
power = (1/T_0) * trapz(t,y.^2);
Edited - Walter is correct.
댓글 수: 4
Steven Lord
2023년 3월 30일
The int function to perform integration is only defined for symbolic inputs, not numeric inputs. So this works:
syms x
int(x^2)
but this doesn't.
y = (1:10).^2;
int(y)
In your code y is a vector of values not a symbolic expression.
참고 항목
카테고리
Help Center 및 File Exchange에서 Number Theory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!