How do I plot this Fourier series funcion?

조회 수: 12 (최근 30일)
Hasan Al Tarify
Hasan Al Tarify 2021년 10월 8일
댓글: Star Strider 2021년 10월 8일
Hello everyone,
I have tried to plot the function below from (0,pi) for a0 and the first six terms of the series on matlab. I did not know how to include the sin and cos function together so I tried to plot the cos term first, However, it seems like it does not work with the followig code becaue of (cos, sin) error. I would appriciate your help.
a0 = pi/4
an = 1/pi(cos(n*pi)-1)
bn = (-1*cos(n*pi))/n
f(t) = a0 + sum(an*cos(n*t))+ sum(bn*sin(n*t))
t = 0:0.1:2*pi;
n = 6;
ycos = cos(t,n);
plot(x,ycos),grid
xlabel('t'),ylabel('cos function')
% Define functions
function f = fcos(t,n)
f = zeros(1,numel(t));
f = pi/4;
for i = 1:n
an = -1/pi(cos(i*pi)-1) ;
f = f + an*cos(i*t);
end

답변 (1개)

Star Strider
Star Strider 2021년 10월 8일
I am not exactly certain what you want to do, however the loop is likely not necessary, sinc it is prefgerable to use MATLAB’s vectorisation capabilities —
t = 0:0.1:2*pi;
n = 1E-8+(0:6).'; % Adding 1E-8 Prevents NaN Values In The Output, Transposed To Column Vector
a0 = pi/4;
an = 1./(pi*(cos(n*pi)-1))
an = 7×1
1.0e+14 * -7.1677 -0.0000 -7.1677 -0.0000 -7.1677 -0.0000 -7.1677
bn = (-1*cos(n*pi))./n
bn = 7×1
1.0e+08 * -1.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000
f = @(t) a0 + sum(an.*cos(n.*t))+ sum(bn.*sin(n.*t));
figure
plot(t, f(t))
grid
Make appropriate changes to get different results.
.
  댓글 수: 6
Hasan Al Tarify
Hasan Al Tarify 2021년 10월 8일
Oh I have figured it out. There were old variables with different values of the same ones you entered. I solved it now. I really appriciate your help and patience.
Star Strider
Star Strider 2021년 10월 8일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by