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

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

OCDER
OCDER il 24 Set 2018
Modificato: OCDER il 24 Set 2018
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

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by