-
Notifications
You must be signed in to change notification settings - Fork 0
/
heatEq1D.m
36 lines (25 loc) · 946 Bytes
/
heatEq1D.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function FPS_heat_equation
% 1D-Heat Equation: Fourier Pseudospectral
% RBF Course: HW#2
% Demo code of ode45/Fourier-PS solution of
% PDE u_t = u_xx
% IC u(x,0) = 0.05/(1.05-cos(pi*x))
% for time t=0 to t=0.2
clear all
close all
clc
t0 = 0;
tf = 0.2;
n = 32; % set space resolution
x = linspace(-1,1,n+1); x[end] = []; %layout datapoints; skip last.
u0 = (0.05./(1.05-cos(pi*x)) )'; % give Init. cond.
v = (-pi^2*[0:n/2, n/2-1:-1:1].^2)'; % vector to multiply in fourier space
% to get second derivatives.
[t,u] = ode45(@dudt, t0:0.01:tf, u0, [], v); % use ode45 to advance in time
u(:,n+1) = u(:,1); % Fill in right-edge in graphics.
mesh([x,1],t,u); colormap([0,0,0]) %Plot result
function uxx = dudt(t,u,v)
'''Calculate uxx by Fourier Pseudospectral Method'''
uf = fft(u);
uf = uf.*v;
uxx = real(ifft(uf));