Why does my program not graph my function (line 17)?

1 次查看(过去 30 天)
t = [-2:.001:2];
y = 0.5*square(2*pi*t,50)+0.5;
plot(t,y);
xlabel('Time');
title('Square wave with 50% duty cycle Marcial');
grid on;
axis([-2 2 -1 2]);
x0=.5;
syms n t;
h1 = x0 + (1-cos(pi)*(sin(2*pi*t))/pi); %1st harmonic
h3 = x0 + symsum((1-cos(pi*n)*(sin(2*pi*t))/pi*n),n,1,3); %third harmonic
h5 = x0 + symsum((1-cos(pi*n)*(sin(2*pi*t))/pi*n),n,1,5); %fifth harmonic
h7 = x0 + symsum((1-cos(pi*n)*(sin(2*pi*t))/pi*n),n,1,7); %seventh harmonic
h20 = x0 + symsum((1-cos(pi*n)*(sin(2*pi*t))/pi*n),n,1,20); %20th harmonic
h100 = x0 + symsum((1-cos(pi*n)*(sin(2*pi*t))/pi*n),n,1,100); %100th harmonic
hold on;
plot(t,h1);
I am trying to approximate a square wave with it's fourier series. The final line is giving me an error: "Data must be numeric, datetime, duration or an array convertible to double." I am not sure what this means. I have attempted to plot both the square wave and the function h1 using the same line (and subsequent plotting data after calculating harmonics), but that does not work either. Thank you for your time.

采纳的回答

OCDER
OCDER 2018-9-24
编辑:OCDER 2018-9-24
Use fplot to plot symbolic equations, and use plot to plot vectors x and y. See: https://www.mathworks.com/help/symbolic/fplot.html
t = [-2:.001:2];
...
syms n t %you overwritten t as a symbolic variable
plot(t, h1) %can't plot it, as t is not a vector anymore
fplot(h1, [-2, 2]) %This should work, as h1 is a symbolic function that you want to plot t = -2 to 2

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by