====== Differences ====== This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
gibson:teaching:spring-2014:iam950:hw1 [2014/02/27 08:54] gibson |
gibson:teaching:spring-2014:iam950:hw1 [2014/02/27 09:39] (current) gibson |
||
---|---|---|---|
Line 111: | Line 111: | ||
in that the 8 represents the sum of the k=4 and k=-4 cosine modes, with no representation | in that the 8 represents the sum of the k=4 and k=-4 cosine modes, with no representation | ||
of the k=4 and k=-4 sin modes. Without going into too much detail on this, Fourier spectral | of the k=4 and k=-4 sin modes. Without going into too much detail on this, Fourier spectral | ||
- | PDE codes typically just zero the highest-order mode at every time step. | + | PDE codes typically just zero the highest-order mode at every time step. We can ignore this |
+ | subtlety. | ||
+ | Based on the above, here's a short example of how to do Fourier differentiation in Matlab on | ||
+ | a [0,L] periodic domain. This should be plenty to get you started for the time-stepping codes. | ||
+ | |||
+ | <code> | ||
+ | % A demonstration of Fourier differentiation in Matlab | ||
+ | |||
+ | % Parameters | ||
+ | L = 10; | ||
+ | N = 16; | ||
+ | x = L/N*(0:N-1); | ||
+ | |||
+ | % Set up u(x) and du/dx for some known L-periodic function | ||
+ | a = 2*pi/L | ||
+ | u = sin(a*x) + 0.2*cos(a*2*x); | ||
+ | dudx = a*cos(a*x) - 0.4*a*sin(a*2*x); | ||
+ | |||
+ | % Now compute dudx via FFT | ||
+ | k = [0:N/2-1 -N/2:-1]; | ||
+ | alpha = 2*pi*k/L; | ||
+ | D = i*alpha; | ||
+ | dudx_fft = real(ifft(D.*fft(u))); | ||
+ | |||
+ | % Plot everybody | ||
+ | plot(x,u, 'b', x,dudx, 'g', x, dudx_fft, 'r.') | ||
+ | legend('u','du/dx','du/dx via FFT','location','southwest') | ||
+ | xlabel('x') | ||
+ | </code> | ||