Help with Matlab HW Problem

14 views (last 30 days)
Juan
Juan on 21 Oct 2013
Commented: Image Analyst on 23 Oct 2013
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)

Image Analyst
Image Analyst on 21 Oct 2013
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
Image Analyst
Image Analyst on 21 Oct 2013
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.
Image Analyst
Image Analyst on 23 Oct 2013
Please mark answer as Accepted if we're done now.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!