-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector field.R
More file actions
71 lines (58 loc) · 1.68 KB
/
Copy pathVector field.R
File metadata and controls
71 lines (58 loc) · 1.68 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Function to plot vector fields and approximate solutions for a given linear
# system matrix
plot_vf <- function(e_vals) {
# Gradient function
Fn <- function(x, y, e_vals) {
if (is.complex(e_vals)) {
H <- 1 / sqrt(2) * cbind(c(1i, 1), c(1, 1i))
Re(t(H %*% diag(e_vals) %*% t(Conj(H)) %*% rbind(x, y)))
}
else t(diag(e_vals) %*% rbind(x, y))
}
# Mesh for plot
l <- 11
m <- seq(-1, 1, l = l)
y <- rep(m, l)
x <- rep(m, each = l)
# Time step
dt <- 0.03
# Arrow head length
arwl <- 0.04
# Gradient over mesh
Fxy <- Fn(x, y, e_vals)
# Plot vector field - gives warnings for points with zero gradient
plot(x, y, type = 'n', axes = F)
box()
arrows(x, y, x + dt * Fxy[, 1], y + dt * Fxy[, 2], length = arwl)
# Number of time steps
T <- 100
# Number of solutions
n_s <- 10
# Randomly initialize solutions
x_s <- y_s <- matrix(nrow = T, ncol = n_s)
x_s[1, ] <- sample(m, n_s)
y_s[1, ] <- sample(m, n_s)
# Approximate solutions with Euler's method
for (t in 2:T) {
Fxys <- Fn(x_s[t - 1, ], y_s[t - 1, ], e_vals)
x_s[t, ] <- x_s[t - 1, ] + dt * Fxys[, 1]
y_s[t, ] <- y_s[t - 1, ] + dt * Fxys[, 2]
}
# Plot solutions
matlines(x_s, y_s, col = 'blue', lty = 1)
arrows(x_s[T - 1, ], y_s[T - 1, ], x_s[T, ], y_s[T, ], length = arwl,
col = 'blue')
}
# Plot vector fields and approximate solutions for a given linear system matrix
par(mfrow = c(3, 3), mar = c(0, 0, 0, 0))
plot_vf(c(0, 0))
plot_vf(c(1, 0))
plot_vf(c(-1, 0))
plot_vf(c(1, 1))
plot_vf(c(-1, -1))
plot_vf(c(1, -1))
plot_vf(c(1, 2))
plot_vf(c(-1, -2))
plot_vf(c(1i, -1i))
plot_vf(c(1 + 1i, 1 - 1i))
plot_vf(c(-1 + 1i, -1 - 1i))