 In this video, we're going to find the Fourier series for the so-called square wave function. So the square wave function is defined by the following rule here, f of x. It's a piecewise function for which it's going to be 2 pi periodic, so it repeats itself every 2 pi. And so as long as you know what happens for the interval negative pi to pi, you know what it does everywhere because it'll just repeat itself. So what we're going to do is that when x is between negative pi and 0, the function is equal to 0. But when x is between 0 and pi, it's equal to 1. And so if you then look at the principal branch here, you're going to get this picture right here. So when x is between negative pi to 0, where 0 is excluded in that, you get that it's 0 itself. So between negative pi and 0, the function is just on the x-axis. But between 0 and pi, the function is equal to 1. So you see this jump right here. At 0, it is equal to 1. At pi, it is undefined. Well, at pi, it jumps back down here since it's 2 pi periodic. And so that's what happens. You see this jump right here, then it repeats itself over and over and over again. Where it gets the name is if we add in the asymptotes, because there's, I shouldn't say asymptotes. So if you add in a line for these jump discontinuities, then our function, in fact, looks like a wave. But it looks like there's these square bumps inside of our wave, which would be kind of weird if the ocean did that. But sure enough, it's a perfectly good wave function. Some people call this the square tooth saw, because it's like the teeth of a saw. But again, they come to squares as opposed to points. Anyways, we have our square wave function right here. Notice this function is a 2 pi periodic piecewise continuous function. It does have discontinuities, but every connected piece is itself continuous. So by the Fourier convergence theorem that we learned about in the previous video, we know that this function does have a Fourier series representation. We have the formulas to compute the Fourier coefficients of the Fourier series representation. And in fact, the Fourier series will equal the function at all points except at the discontinuities. At the jump discontinuities, it's actually going to grab the midpoint. So since it jumps from the left hand limit is always 1 and the right hand limit is always 0, the Fourier series is always going to grab 1 half. The y-cord is going to be 1 half for the Fourier series. At those jump discontinuities everywhere else, it's going to be in complete agreement. Alright, so how do you compute the Fourier coefficients here? So we have to utilize those formulas that we learned about in the earlier video there. So to find the coefficient of a sub 0, because after all we're looking for a function, sorry, the Fourier series representation is going to look like f of x equals a sub 0, some constant term. Then you're going to have a sum where n ranges from 1 to infinity here. You're going to have some an times cosine of nx. You're also going to have a bunch of sine terms in there as well, bn times sine of nx as well. Alright, so we're looking for something like that. We have to find these coefficients a0, an, and bn. So a0 we have to find separately the constant term. The formula for a0 was the following 1 over 2 pi times the integral from negative pi to pi of f of x dx. Now because our function is a piecewise function, let's look at this. When you're between negative pi and 0, it doesn't do anything, it's just 0. So we actually can ignore that part of the interval. And then when you're between 0 and pi, it's just 1. So the integral of f of x from negative pi to pi is actually just the integral of 1 from 0 to pi. For a piecewise function, you can break it up into two pieces. We break it into the 0 piece and the 1 piece. The 0 piece has no area under the curve since it's 0. And so this integral then becomes this one right here. As you integrate from 0 to pi of a constant function, the antiderivative would be x. You plug in pi, you plug in 0, take the difference. You're just going to get pi right there. So this integral is equal to pi. So you get pi over 1 half pi. That simplifies just to give you 1 half. And that actually isn't surprising because at x equals 0, your Fourier series should equal 1 half, the midpoint of that jump. That's what our convergence theorem told us. So let's then consider the coefficients of the cosines. So the a sub n's by formula is going to equal 1 over pi times the integral from negative pi to pi of f of x times cosine of nx dx. And the same trick we did before by the piecewise nature of f of x. When you go from negative pi to 0, f is 0 and just disappears. Therefore, and then from 0 to pi, it's equal to 1. So this integral involving f can be simplified to this integral right here where we only have to integrate from 0 to pi. And then f of x is 1 in that situation. So we get from 0 to pi cosine of nx. Now the antiderivative of cosine is sine. And then because of the period change, the antiderivative of cosine nx will be 1 over n sine of nx. So since you already have a 1 over pi, you get 1 over pi times 1 over n. So you get 1 over n pi. This becomes sine of nx here. Then let's plug things in. As you plug in 0, you get sine of 0, which is 0. But then notice when you plug in pi, we don't know what n is. All we know that n is is going to be some positive integer. And so therefore we're taking sine of a multiple of pi. So things like sine of pi, sine of 2 pi, sine of 3 pi, sine of 4 pi, sine of 5 pi, et cetera, et cetera, et cetera. Sine of any multiple pi is always equal to 0. So this thing is always going to equal 0. So this tells us that the a sub n's are equal to 0. So in our Fourier series, it turns out that all of the cosines vanish. They don't show up for this square wave function. That's kind of interesting. Let's look at the coefficients of the sine. Similar calculation, b sub n by formula. This is 1 over pi times the integral from negative pi to pi of f of x sine of nx dx. Because f of x is the square wave function, this integral simplifies to be the integral from 0 to pi of sine of nx dx. The antiderivative of sine of nx similar to what we did before would be negative 1 over n times cosine of nx times the negative 1 over n times 1 over pi gives you negative 1 over n pi times cosine of nx. If you plug in 0 in this situation, cosine of 0 is actually equal to 1. So you're going to get a minus 1 there. And then the other one gets a little bit more tricky here. When you plug in the pi, you get cosine of n pi. So what happens to cosine of n of multiple of pi? Well, it depends which multiple of pi you're at. So cosine of 0 pi is 1, like we already mentioned. Cosine of pi is equal to negative 1. Cosine of 2 pi is equal to 1. Cosine of 3 pi is equal to negative 1. Cosine of 4 pi is equal to positive 1. So you're always getting 1 or negative 1. In fact, cosine of n pi is equal to negative 1 to the n. Where you're going to get 1 or negative 1, and it depends entirely on this coefficient. So factoring out the negative 1 over n pi like so, cosine of n pi minus cosine of 0 becomes negative 1 to the n minus 1. And what happens there? Well, when the negative 1 to the n becomes positive 1, you end up with 1 minus 1, which is 0, which then kills off this part too. But when is negative 1 to the n going to be positive 1? What happens when you have an even power? And so when n is an even number, n equals 2k, this is going to turn out to be 0. So a lot of things are vanishing here. But when n is an odd number, say something like n equals 2k minus 1. If 2k is an even number, then 2k minus 1 will be an odd number there. In that situation, you're going to end up with a negative 1 minus 1. That gives you a negative 2. You have negative 1 times negative 2. That's a positive 2. And then you have an n pi on the bottom there. And so when you have odd, when n is odd, you're going to get this coefficient of 2 over n pi for the bn. So b sub an odd number is 2 over n pi. b sub an even number is 0. a sub any number is going to be 0, and we're going to end up with the following. f of x equals 1 half is our constant term. All of the cosine terms are vanished. The only sine terms that stick around are those which have odd multiples. So you're going to have to get sine of 2k minus 1x showing up in there. And so k is going to range from 1 to infinity here. Notice when k equals 1, you're going to end up with 2 times 1 minus 1, which is equal to 1. Then when k equals 2, you'll end up with 3. When k equals 3, you'll end up with 5, et cetera, et cetera. And then your coefficients are 2 over 2k minus 1 times pi. So if you write that in a more expanded formula, you're going to get 1 half plus 2 over pi sine of x plus 2 over 3 pi times sine of 3x plus 2 over 5 pi sine of 5x. Notice that these coefficients always are the same. 5 and 5 there. There's always a 2 on the top with the exception of the constant term. So the next term, if I were to write it out here, would be 2 over 7 pi times sine of 7x, et cetera, et cetera. So that's what our thing looks like. That's the Fourier series for our function. So I want to look at the graph of this thing and show you what it looks like. So if we only use this approximation of the Fourier series, so if you only go up to 5 for n equals 5, in that situation, you're going to get a graph that looks exactly like the following. So you can still see in yellow, the original square wave function is on the screen right here. But then in magenta, you see this curly business happening so that the function, it jumps from one to the other. It kind of wiggles on the line for a while. Then it jumps down and then it wiggles and then jumps, wiggles, jumps, wiggles, and then continues doing this over and over again. It's trying to mimic what's happening there because it needs to be perfectly flat. A single sine by itself can't be flat. Even a finite amount of sines can't be flat, but it's really trying. It gives you a pretty good approximation that's going on here. And of course, as you allow n to go towards infinity, these things are going to get flatter and flatter and flatter. The jump will be closer and closer to where the actual jump should be, and you're going to start to better approximate this thing. So what I'm going to do next is going to switch over to Desmos. So I have an app already set up on Desmos.com. You can find the link for this in the description of the video here. And so what you now see on the screen is our square wave function from before. So here is zero to pi. Here is pi to three pi, et cetera, et cetera. So what I'm going to do is I'm going to turn on my foyer series representation. So I have it already coded up in this box right here. So you can see everything. It is a finite sum. And so what I've done is I've set it so that there's a parameter so I could increase this value of n, because k is going to go up until n. For right now, n is equals zero, so it's only giving you the constant term. So you see just this line y equals 1 half. Notice it captures the midpoints of each of these jumps, just like we expected. So now as we increase this, so we increase it to one, this will then give you the, this will give us one half plus two over pi sine of x. I'm going to zoom out a little bit here so we can see more of the periods, more of the repeats. So this looks like just a basic sine wave right here. It's capturing, it captures the jumps pretty good. Again, at each point where you have a jump, you're going to get one half. That's always going to be the case of these jumps here. All right? So then if we increase this again, so now we're looking at one half plus two over pi sine of x plus two over three pi times sine of three x, you can see there's kind of like three little bumps and that's because we changed it from sine of, well, we still have the sine of x, but we've also thrown a sine of three x, that gives us more bumps. As we change the period with these signs, it does allow us to, allows us more wiggle room, literally. And therefore we can stay on the flat line longer. As we move up to three, we get the following up to four, up to five. All right? This was the graph we were looking at earlier on the slides. And then we can allow this to get bigger, bigger, bigger. Notice as our power gets larger, larger, larger, it's harder to see the wiggles because they're getting flatter and they're better approximating these things. I'm going to zoom back in to our principal piece right here. So notice we now have N16. So we have, we currently have 16 different signs interacting here. And if we keep on increasing this all the way up to 25, you can see that in the middle, man, that is almost a flat line. You can barely see the wiggles whatsoever. Like if I was to zoom out again, like at this scale, you can't see any of the vibration happening whatsoever. It almost looks like a perfect fit. Of course, if you zoom in, you can start to see the vibrations. It does really good when you are in the middle, but when you get close to the jump, it does fall apart a little bit. But notice the jump kind of only happens near the end. The foyer series is doing really great at doing that. Now, this is not the foyer series. It's just a partial sum. We're doing, of course, N equals 25. If you allow N to go towards infinity, this actually will become perfection in the end. And so this then illustrates how effectively this foyer series is, in fact, modeling this periodic function.