MATLAB Verifying Fourier Series In MATLAB

AI Thread Summary
The discussion revolves around a MATLAB code snippet intended for calculating a series and its Fourier coefficients. The initial code attempts to compute the sum of a series and the Fourier series representation of the hyperbolic cosine function. However, users point out that while the calculation of the series sum is correct, it could be simplified. Specifically, the sum can be initialized with `s=1` instead of starting from zero. Additionally, there are errors in the Fourier series calculation. It is suggested to initialize the Fourier series with `fs = a0/2` and to compute the Fourier coefficients `an` and `bn` directly within the loop instead of pre-storing them, which would enhance efficiency. Overall, the discussion emphasizes correcting and optimizing the MATLAB code for better performance and accuracy.
Jovany_17
Messages
2
Reaction score
0
HI please help me this could someone verify it for me please find attachement

clc;
clear all;
k=0;
s=0;
N=inf;
for i=1:N
s=s+(1/(k^2+1));
k=k+1;
end

syms x n
a0=1/pi*int(cosh(x),-pi,pi);
an=1/pi*int(cosh(x)*cos(n*x),-pi,pi);
bn=1/pi*int(cosh(x)*sin(n*x),-pi,pi);

fs=0;

for l=0:100
fs=fs+(an*cos(l*x)+bn*sin(l*x))
end

fs-fs+a0/2;
 

Attachments

  • TD.jpg
    TD.jpg
    6.2 KB · Views: 618
Physics news on Phys.org
Jovany_17 said:
HI please help me this could someone verify it for me please find attachement

clc;
clear all;
k=0;
s=0;
N=inf;
for i=1:N
s=s+(1/(k^2+1));
k=k+1;
end

syms x n
a0=1/pi*int(cosh(x),-pi,pi);
an=1/pi*int(cosh(x)*cos(n*x),-pi,pi);
bn=1/pi*int(cosh(x)*sin(n*x),-pi,pi);

fs=0;

for l=0:100
fs=fs+(an*cos(l*x)+bn*sin(l*x))
end

fs-fs+a0/2;
The calculation of s looks OK. It could have been done slightly easier.
N=inf;
s=1;
for k = 1:N
s=s+(1/(k^2+1));
end
The Fourier series sum has a couple of errors. I would do the following:
fs = a0/2;
N = inf;
for n = 1:N
fs=fs+(an*cos(n*x)+bn*sin(n*x));
end
 
Thanks
 
A further simplification: Except for a0, the values of an and bn can be calculated inside the loop, so they don't have to be stored in advance.
 

Similar threads

Replies
8
Views
2K
Replies
2
Views
1K
Replies
5
Views
3K
Replies
1
Views
2K
Replies
10
Views
3K
Replies
1
Views
2K
Back
Top