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!

답변 (1개)

Image Analyst
Image Analyst 2013년 10월 21일

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

Juan
Juan 2013년 10월 21일
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 2013년 10월 21일
편집: Matt Kindig 2013년 10월 21일
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 2013년 10월 21일
thank you so much. it works perfectly now. you guys are awesome
Image Analyst
Image Analyst 2013년 10월 21일
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 2013년 10월 23일
Please mark answer as Accepted if we're done now.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2013년 10월 21일

댓글:

2013년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by