-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpdepe_no_Spatial_Derivative_example.m
60 lines (48 loc) · 1.35 KB
/
pdepe_no_Spatial_Derivative_example.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
x = linspace(0,1,30);
t = linspace(0,1,10);
% define s coefficient in PDE
A = @(x) cos(3*pi*x);
% define initial condition
B = @(x) sin(pi*x);
% calculate analytical solution
ua=uAnal(x,t,A, B);
pdeFunc = @(x,t,u,DuDx) pde(x,t,u,DuDx,A);
icFunc = @(x) ic(x,B);
bcFunc = @(xl,ul,xr,ur,t) bc(xl,ul,xr,ur,t);
m=0;
u = pdepe(m, pdeFunc,icFunc,bcFunc,x,t);
err=ua(:)-u(:);
fprintf('Solution error: max=%12.3e, average=%12.3e\n', ...
max(abs(err)), norm(err)/length(err));
figure; plot(t, u(:,end), t, ua(:,end), 'o'); grid on;
xlabel('Time'); ylabel('u');
legend('pdepe', 'analytical', 'Location','northwest' );
title('Solution at right end as a function of time');
figure; plot(t, u(:,1), t, ua(:,1), 'o'); grid on;
xlabel('Time'); ylabel('u');
legend('pdepe', 'analytical', 'Location','northwest' );
title('Solution at left end as a function of time');
figure; plot(x, u(end,:), x, ua(end,:), 'o'); grid on;
xlabel('x'); ylabel('u');
legend('pdepe', 'analytical', 'Location','northwest' );
title('Solution along the length at final time');
function [c,f,s] = pde(x,t,u,DuDx,A)
nx=length(x);
c = ones(1,nx);
f = zeros(1,nx);
s = A(x);
end
function u0 = ic(x,B)
u0 = B(x);
end
function [pl,ql,pr,qr] = bc(xl,ul,xr,ur,t)
pl = 0;
ql = 1;
pr = 0;
qr = 1;
end
function u=uAnal(x,t,A, B)
nt = length(t);
u=t'*A(x) + repmat(B(x),nt,1);
end
%% Copyright (C) 2018 William H. Greene