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!

Respuestas (1)

Image Analyst
Image Analyst el 21 de Oct. de 2013

1 voto

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 comentarios

Juan
Juan el 21 de Oct. de 2013
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!
Matt Kindig
Matt Kindig el 21 de Oct. de 2013
Editada: Matt Kindig el 21 de Oct. de 2013
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
Juan
Juan el 21 de Oct. de 2013
thank you so much. it works perfectly now. you guys are awesome
Image Analyst
Image Analyst el 21 de Oct. de 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 el 23 de Oct. de 2013
Please mark answer as Accepted if we're done now.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 21 de Oct. de 2013

Comentada:

el 23 de Oct. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by