How can I use Matlab's symbolic method to solve Fourier series?

Click For Summary

Discussion Overview

The discussion revolves around using MATLAB's symbolic method to solve Fourier series problems, including coding techniques and specific function implementations. Participants share their approaches, code snippets, and seek assistance with plotting and programming issues related to Fourier series.

Discussion Character

  • Technical explanation
  • Homework-related
  • Exploratory

Main Points Raised

  • One participant expresses difficulty with 'for' and 'while' loops in MATLAB and seeks guidance on starting a program for Fourier series.
  • Another participant provides a code snippet for a Fourier series function, detailing the structure of the loop and plotting commands.
  • A different participant describes a specific Fourier series function and shares their code, asking for help with plotting issues and ensuring the entire period is displayed correctly.
  • One participant mentions using MATLAB's symbolic method for their Fourier series assignment, providing the mathematical definitions of the square wave function and its Fourier coefficients, along with their implementation in code.
  • The same participant explains how to adjust the summation limit in their code and shares their complete plotting code for the Fourier series.

Areas of Agreement / Disagreement

Participants generally share their individual approaches and code without reaching a consensus on a single method or solution. There are multiple competing views on how to implement the Fourier series in MATLAB, and some participants are seeking clarification or assistance with specific issues.

Contextual Notes

Some participants' code snippets may contain assumptions about the functions being plotted or the range of values used, which are not explicitly stated. There may also be unresolved issues regarding the accuracy of the plots and the implementation of the Fourier series.

Who May Find This Useful

Readers interested in MATLAB programming, Fourier series analysis, and symbolic computation may find this discussion beneficial.

caduceus
Messages
16
Reaction score
0
I have some problems on 'for' and 'while' loop so I could not write a MATLAB program for this question;
Gdq_Clipboard01.jpg
.

Well, how can I start?
 
Physics news on Phys.org
First of all, I assume you are writing a .m script file i.e. you start off with:
Code:
function y = f(x)

%%The loop would look like:

y=0.0;

for n = 1:50
  
   y = y + 2(-1)^(n+1) ( (pi^2)/n - (6/(n^3)) sin(n x);

end

%%After which you could write

x = 1.0 : 0.1 : 2*pi
y = f(x);
plot(y,x)
 
Oh, I got the point! Thanks for replying..
 
Hi, I am trying to plot a Fourier series in MATLAB for the function

x(t)

= 0 when -pi < wt < 0
= (4/pi)*wt when 0 < wt < pi/2
= 2 when pi < wt < 2*pi

I know that a0 = 3/2, an = (1/pi)*(4/(pi*i^2))*(cos((pi*i/2)-1)), and bn = (1/pi)*((4/(pi*i^2))*(sin(pi*i/2-1))-2/pi*(cos(pi*i)))

so far I have the code as:

N = 11;
x = [0:100]/100;
f = zeros(1,101)*1/2;
for i = 1:2:N
a = (1/pi)*(4/(pi*i^2))*(cos((pi*i/2)-1));
b = (1/pi)*((4/(pi*i^2))*(sin(pi*i/2-1))-2/pi*(cos(pi*i)));
f = f + a*cos(i*x)+ b*sin(i*x);
end
f = .75 + f;

plot(x,f); any help?? I tried tweaking the window so that it would plot the entire period, but it's not coming out correctly. any help??
 
Hi, this is the code I did for my first assignment on Fourier series. It might be considered "cheating" in terms of programming since I used the Matlab's symbolic method to do all the grunt work. I figured if Matlab offered such a nice/easy feature then I might as well use it, lol :P

For a square wave:
V(x)=\left\{\begin{matrix} 1 &amp; 0&lt;x&lt;\frac{L}{2}\\ -1 &amp; \frac{L}{2}&lt;x&lt;L \end{matrix}\right.

Fourier Series
V_{N}(x)=\sum_{n=0}^{N}a_{n}\sqrt{\frac{2}{L}}cos(\frac{n2\pi x}{L}) + \sum_{n=1}^{N}b_{n}\sqrt{\frac{2}{L}}sin(\frac{n2\pi x}{L})

where
a_{n} = \frac{2L}{n\pi}\sqrt{\frac{2}{L}}sin^2(\frac{n\pi}{2})sin(n\pi)

b_{n} = -\frac{2L}{n\pi}\sqrt{\frac{2}{L}}sin^2(\frac{n\pi}{2})cos(n\pi)

One can change the summation limit N in the code below by changing the value 20, the last parameter in the function

V_x = symsum(..., n, 1, 20);

Code:
L = 8;
syms n x;

a_n = sqrt(2/L)*( (2*L)/(n*pi) )*( (sin((n*pi)/2))^2 )*sin(n*pi); %fourier coefficient a_n
b_n = -sqrt(2/L)*( (2*L)/(n*pi) )*( (sin((n*pi)/2))^2 )*cos(n*pi); %fourier coefficient b_n
x = 0:0.2:8; %range
V_x = symsum( a_n*sqrt(2/L)*cos( (n*2*pi*x)/L ) + b_n*sqrt(2/L)*sin( (n*2*pi*x)/L ), n, 1, 9 ); %fourier series

plot(x, V_x);
grid on;
axis([ 0 8 -1.4 1.4]);
hold on;
 
Last edited:

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
5
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
7K