Help with Matlab HW Problem

Okay, so I'm a beginner and I really need some help on a homework problem that I'm not understanding. I haven't gotten very far, so I apologize, but any guidance would be extremely appreciated. The task is this:
A Fourier series is an infinite series representation of a periodic function in terms of sines and cosines at a fundamental frequency (matching the period of the waveform) and multiples of that frequency. For example, consider a square wave function of period L, whose amplitude is 1 for 0 L/2, –1 for L/2 L, 1 for L 3L/2, and so forth. This function is plotted in Figure 5.15(the function is just a squarewave function). This function can be represented by the Fourier series f(x)= (sigma from i=1,3,5... to n) (1/n)*sin(n*pi*x)/L (5.15) Plot the original function assuming L=1, and calculate and plot Fourier series approximations to that function containing 3, 5, and 10 terms.
I know that we're supposed to create a fourier series with 3, 5, or 10 terms by implementing a for loop. My attempt at a solution goes something like this:
for ii = 1:2:5
fourier_n = (1/ii)*sin(ii*pi*x);
end
In my head, I don't understand how that would NOT create the fourier series. However, MATLAB gives me this whenever I try to see my results: "Undefined function or variable 'x'." How else am I supposed to create a fourier series with a few terms if it won't allow me to include x in the terms? If I simply leave the 'x' out of the for loop, it will be incorrect because it will give a value for the expression when it should output a function instead. Any tips or help at all will be appreciated immensely. Thank you!

Answers (1)

You need to define x before the loop, like if you want x to go between -2*pi and 2*pi in 500 steps:
x = linspace(-2*pi, 2*pi, 500);

5 Comments

ok, so I included what you suggested and it did get rid of the error message. however, the plot is not a square wave. the plot is a sin wave, which makes me think that it's simply plotting the final value for fourier_n which would would essentially be (1/5)*sin(5*pi*x)... what i'm looking for is a function in the form of f1(x)+f3(x)+f5(x)... my initial thought is to make a recursive sum within the for loop and then plot that final value... how would i go about doing this? thank you!
The sum idea is the way to go:
fourier_n = zeros(size(x)); %initialize fourier_n to zeros
for ii = 1:2:5
fourier_n = fourier_n + (1/ii)*sin(ii*pi*x); %add next term
end
thank you so much. it works perfectly now. you guys are awesome
You're welcome. I think you were supposed to observe that as you include more and more terms (ending value goes from 5 to 9 to 15 or 31....) that the shape gets closer and closer to a square wave. Please mark answer as Accepted if we're done now.
Please mark answer as Accepted if we're done now.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 21 Oct 2013

Commented:

on 23 Oct 2013

Community Treasure Hunt

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

Start Hunting!