-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdemo_07_stiff.m
60 lines (54 loc) · 1.33 KB
/
demo_07_stiff.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
%% Example of a stiff problem
% forward Euler and backward Euler applied to:
% u' = -100(u-cos(t)) - sin(t)
% The soln is slowly varying but the numerical time-step for FE is
% effected by the -100 ("fast scale" in the problem, not in the
% solution).
Hf = figure(1); clf;
%hold on
%H = get(Hf, 'children'); set(H, 'fontsize', 16);
ms = 'markersize';
tmax = 2; exact = cos(tmax);
%% First, use forward euler
logkset = 1:10;
for logk = logkset
k = .5^logk
nsteps = tmax/k;
t = 0; v = cos(t); f = -sin(t);
for n = 1:nsteps
t = (n-1)*k;
%% try this:
%f = -sin(t);
%% then this:
f = -sin(t) -100*(v - cos(t));
vnew = v + k*f;
v = vnew;
end
error = abs(v(end) - exact);
loglog(k,error,'.k',ms,34)
H = get(Hf, 'children'); set(H, 'fontsize', 16);
%axis([.5e-3 .5 1e-7 1e11]), grid on, hold on
hold on;
xlabel('step size k')
ylabel('error')
%pause
end
%% Next, backward Euler
for logk = logkset
k = .5^logk;
nsteps = tmax/k;
t = 0; v = cos(t); f = -sin(t);
for n = 1:nsteps
t = (n-1)*k;
% backward Euler: vnew = v + k*f(t+k, vnew)
%% first problem
%vnew = v + k*(-sin(t+k));
%% 2nd problem
vnew = (v + k*(-sin(t+k) + 100*cos(t+k))) / (1+k*100);
v = vnew;
end
error = abs(v(end) - exact);
loglog(k,error,'.r', ms, 24)
%pause
end
title('FE: black, BE: red');