And then you just have to keep in mind, when you use that fft you'd better know
that the fft is doing the shifting. And you could ask, well, why not, why the, why
make it, why not shift it back? Why, why make me shift it back?" Cuz a lot of times
when you're doing computation, you're not gonna necessarily go look at the spectrum
all the time. You, the reason it stays shifted is because, remember, speed is
everything. Speed is everything in terms of the computing. You want, if you got, if
you got n log n speed, you wanna add you know, stuff on top of it. You can do that
on your own time. With this computing, keep it at n log n, fast. Okay? In my last
class, I talked in my last course, 581. I always tell people you may get the right
answer. But it's wrong if you did it the slow way. Okay? Part of computing now is
not just about oh, I can compute and I got this really, I got this method that can
compute it right. It's not about that anymore, it's about not only can I compute
it right, I can compute it faster than you. Okay. It's not that, it's not that I
guess you know, competitive. But, but actually, you know it kind of is. If
you're trying to write a paper in this area, that's what you're trying to say
actually all the time. It's like oh, I can compute this faster than you can. And here
is why. Okay? That's, that's how you write a paper in scientific computing. You tell
people how much awesomer you are. Okay? Alright. But, see, the nice thing with
your data analysis skills, you might be working, I don't know, in atmospheric
sciences or geology geologoy or something. With your skills you'll be able to say how
much better you are also. See, then I'll, then we'll all have won. Okay? Alright. So
there you go. that's the simple implementation. Now I'm gonna use it to do
a trick. Okay? Yes? So, the actual coefficients are positive and negative
alternating. This is. Yeah. This is the absolute value. If it's complex you know,
most people, by the way, when you get a complex spectrum, what you normally do
what people call the power spectrum. I don't know if you've heard this. Okay?
Well that's an aggressive word just like the competition. It's all, it's all like
smackdown for comp, computing. So the power spectrum is always the absolute
value of the spectral content, cuz that's what you want to know is how much
frequency content, not necessarily what phase it's in. Okay? Alright. So, there's
that. And now, for my next trick, I will differentiate a function, computation. Are
you ready? I, I don't know if you guys are ready. Let's, can I have the lights on
real quick, upfront? There's this very nice relationship, that's actually worked
out in the notes, which says the following. It says the following, the hat
represent Fourier transform by the way, and this represents the nth derivative. If
I take the nth derivative of a function and I wanna find what that is, the Fourier
transform of the nth derivative is the same as taking the function itself, and
Fourier transforming it, and multiplying by ik to the nth power. Okay? So if I want
to compute the third derivative, all I gotta do is take the function, Fourier
transform it, multiply this by k^3 inverse Fourier transform it. That's it. By the
way, this is one of the most, if, if your boundary conditions work for you, like
here the boundaries go to zero, right? And there's supposed to be a periodic
function, but here the boundaries are pretty much going to zero. This is
probably one of the most accurate ways you can compute a derivative, spectrally
accurate. Okay? So let's compute a derivative, and here's the thing I'm gonna
compute. oh actually, we can go back with the lights down. Okay. So I'm gonna take
this function u here. and I'm gonna do the dollowing function. Here is my favorite
function, hyperbolic secant x. I don't know if you have a favorite function yet.
It's just a little bit of time before you graduate to pick one out. This one's taken
by the way, don't even try. and by the way. its derivative, it's called ud is
sech * tanh. Now what I'm going to do is try to compute t hat derivative
accurately. And by the way, second derivative, let's call it ud2 for the
second derivative will be sech x - two * sech x^3. something like that, pretty
close. I think I actually have it in the notes here. sorry, give me just a second
here. yeah, actually yeah, that's right. Okay. and what I'm gonna do is, now,
compute these derivatives using my Fourier transform. So let me write at the top.
There is u, second or first derivative. And first of all, if I wanna calculate the
derivatives, first I have to have Fourier transform of the function. There it is and
I'm gonna calculate now the first derivative. Okay? So, to calculate the
first derivative, remember, what I gotta do is take i * k and multiply it by
Fourier transform. There it is. And then, what I gotta do with this is inverse
Fourier transform it. Right?'Cuz Cuz what this is, is the derivative of the it's the
Fourier transform of the first derivative. So what I can do is just say, okay, if I
want the first derivative, let's call it my ud s for spectral approximations, first
derivative is spectral is ifft. By the way that's how you invert a Fourier transform,
ifft and fft are the pairs. Okay? And by the way, the second derivative, let's call
it ud2s is equal to ifft of i times k. That being squared now, times ut. There
you go. I, I want to call this u, right? So I'm kind of done. So, let's make some
plots. I'm going to plot here the function and for short here I'm going to say, call
it ks for k shift will be the fftshift(k) and so that way I can make this be just,
first I'm gonna plot the function. And then I'll plot, in addition to the
function, I'm gonna plot the derivative of the function, so x versus ud. And my
approximation, let's plot that in a red line and then I'm gonna plot my
approximation to the derivative uds, and I'll plot that with magenta circles. what
I'm plotting is here is the exact answer, right there, x, ud, that's the exact
answer and I'm plotting my approximation to the exact answer. Oh, that's just the
original function. We don't have to have that in there. Would you like to take it
away? We can just do that. What's that? Oh that was the fft, yeah we don't want the
fft of the original function. We just want the function. Oh, by the way. There you
go, look at that. The red line is exact, the magenta line is my approximation. Who
likes that approximation? Give it up you all. Okay. So listen, I know it's the
first day of class, we're just warming up. We'll have a dance party about midway. We
can get disco balls and you know, we can shine that light on there. It's pretty
sweet. Okay. Alright so there is my approximation. By the way, let's zoom in a
little bit. Let's just see how well we really did. Let's see how long it takes
for that magenta ball to fall off. Oh, oh, oh, oh, come on, fall off. I'm zooming in,
I'm zooming in, I'm zooming in. Look, it's pretty dang accurate. Look at that. Oh,
it's finally coming off there. I know it, I can sense it. Oh, look at that. But look
over here on the left, it's like down to ten minus five or negative six. Pretty
amazing accuracy. Now, you can screw up. Let me just show you one other thing.
Suppose I take a smaller domain. Oh, not quite hold on a little smaller just real
quick. Just give me two seconds for the final. Oh, crap. Okay. Come on. Let's do
four. wanna, I wanna, I wanna show you what happens when you kind of start to
violate, yeah, there you go, you start seeing it. Now, notice what happens here?
Here, remember it's supposed to be a two pie periodic function. Right? This is
clearly not periodic. Before they both went to zero, you could say kinda looks
periodic. Here, it's up here above point two here. It's below negative point two.
There's a jump and any time you have a jump in a Fourier transform, when you try
to represent, you get what's called a Gibbs' phenomenon, which is oscillations
near the jump and this is what you see over here. So it does amazingly well here
and over here it starts to oscillate, not so well. Okay. We're gonna use this on
Friday to do radar problems. Okay. Ha ve a good Wednesday. See you guys soon.