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

6 ビュー (過去 30 日間)
Alexa
Alexa 2023 年 3 月 15 日
編集済み: VBBV 2023 年 3 月 16 日
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 Center および File ExchangeLine Plots についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by