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
2013년 10월 21일
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
2013년 10월 21일
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
2013년 10월 21일
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
2013년 10월 23일
Please mark answer as Accepted if we're done now.
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!