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

2 views (last 30 days)
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.

Answers (2)

VBBV
VBBV on 15 Mar 2023
Edited: VBBV on 16 Mar 2023
% 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 Comment
VBBV
VBBV on 16 Mar 2023
Edited: VBBV on 16 Mar 2023
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

Sign in to comment.


Torsten
Torsten on 16 Mar 2023
Edited: Torsten on 16 Mar 2023
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

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!