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

조회 수: 1 (최근 30일)
Jesus Marcial
Jesus Marcial 2018년 9월 24일
편집: OCDER 2018년 9월 24일
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