Error using plot. Data must be numeric, datetime, duration or an array convertible to double.

조회 수: 6 (최근 30일)
I have the following code I obtained from a function:
syms n t
A0 = 5/4;
An= 0;
Bn = (5*(-1)^n)/(n*pi);
T = 2;
Wo = pi;
Arm = 5;
for n=1:1:Arm
syms t
f(n,:) = sum ((A0) + (An * cos(n*t)) + (Bn * sin (n*t)));
t=linspace(0,5*T,1000);
subplot(2,1,1);
plot(t,subs(f(n,:), 't', t));
grid on
xlabel('\bfTIEMPO');
ylabel('\bfAMPLITUD');
title('\bfCOMPONENTE');
hold on
subplot(2,1,2);
plot(t, subs(sum(f), 't', t), 'r', 'Linewidth', 1.5);
xlabel('\bfTIEMPO');
ylabel('\bfAMPLITUD');
title('\bfSERIE DE FOURIER');
pause(1)
end
I got the Fourier coefficients and now I want to graph these results, but I'm getting the following error:
Error using plot
Data must be numeric, datetime, duration or an array convertible to double.
Error in Armonico_Fourier_Proyecto_parcial1 (line 16)
plot(t,subs(f(n,:), 't', t));
I'm really newbie into MATLAB, any help will be greatly appreaciated.

답변 (2개)

VBBV
VBBV 2023년 3월 15일
편집: VBBV 2023년 3월 16일
% syms n
A0 = 5/4;
An= 0;
T = 2;
Wo = pi;
t=linspace(0,5*T,1000);
Arm = 5;
hold on
for n=1:1:Arm
Bn = (5*(-1)^n)/(n*pi);
f(n,:) = (An * cos(n*t)) + (Bn * sin (n*t));
figure(1)
plot(t,f(n,:));
grid on
xlabel('\bfTIEMPO');
ylabel('\bfAMPLITUD');
title('\bfCOMPONENTE');
end
figure(2)
plot(t, A0+sum(f), 'r', 'Linewidth', 1.5);
xlabel('\bfTIEMPO');
ylabel('\bfAMPLITUD');
title('\bfSERIE DE FOURIER');
  댓글 수: 1
VBBV
VBBV 2023년 3월 16일
편집: VBBV 2023년 3월 16일
The error suggests that you are trying to use symbolic variable for plot function. But plot function uses numeric arrays to graph, Further info how to use plot function can be found below link
if you want to plot symbolic expressions, try using fplot

댓글을 달려면 로그인하십시오.


Torsten
Torsten 2023년 3월 16일
편집: Torsten 2023년 3월 16일
Arm = 5;
T = 2;
t = linspace(0,5*T,1000);
t = t.';
hold on
for N=0:Arm
n = 1:N;
A0 = 5/4*ones(size(t));
An= 0*ones(size(t));
Bn = (5*(-1).^n)./(n*pi);
f = A0 + sum (An.*cos(n.*t) + Bn.*sin(n.*t),2);
plot(t,f)
end
hold off
grid on

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by