-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerating_function2.m
More file actions
55 lines (43 loc) · 1.13 KB
/
Copy pathgenerating_function2.m
File metadata and controls
55 lines (43 loc) · 1.13 KB
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
function f = generating_function2(r, gamma, ts, x0, y0)
% solve \dot{f} = [r(x-y)^2 + x(y-1)] f_x + \gamma (1-y) f_y
% by method of characteristics:
% \dot{x} = x(1-y) - r(x-y)^2
% \dot{y} = \gamma (y-1)
% starting at t = t0, x = x0, y = y0
% ending at t = 0; initial condition: f(0, x, y) = x
% two modifications:
% 1. t -> -t; the equations are t-translation invariant, and we would like to
% be able to return the answer for all ts
% 2. use exact solution to y
assert(ts(1) >= 0, 'must start at a positive time');
assert(~(numel(ts) == 2 && ts(1) == 0), 'not allowed to give ts=[0 t]');
[rows,~] = size(ts);
if rows ~= 1
ts = ts';
end
if numel(ts) == 1
times = [0 ts/2 ts];
elseif ts(1) > 0
times = [0 ts];
else
times = ts;
end
[times,~,ind] = unique(times);
function yt = y(t)
yt = exp(-gamma * t) * (y0-1) + 1;
end
function dX = deriv(t, X)
dX = r * (X - y(t))^2 + X * (y(t) - 1);
end
X0 = x0;
[~,X] = ode45(@deriv, times, X0, ...
odeset('RelTol', 3e-14, 'AbsTol', 1e-14));
X = X(ind);
if numel(ts) == 1
f = X(end);
elseif ts(1) > 0
f = X(2:end);
else
f = X;
end
end