在 R 中求解线性系统
Solving Linear System in R
我目前正在尝试在 R 中求解二维线性系统。我有以下函数:
times <- seq(0,25,.01)
state.linear <- c(X = 2, Y = 0) # Initial State
A <- matrix(c(-.1, 2, -2, -.1), 2, 2)
b <- c(2,0)
linear2D <- function(t, A, b) {
with(as.list(c(A, b)), {
dX <- A*b
list(c(dX))
})
}
out.linear2D <- ode(y = state.linear, times = times, func = linear2D, parms = A)
我想通过 deSolve 包中的 ODE 求解器来解决这个问题,但是 运行 在使用初始函数时遇到了麻烦。我正在尝试从 MATLAB 翻译代码:
n = 2; % 2D system
A = [-.1 2; -2 -.1]; % dynamics
rhs = @(x)A*x; % right hand side
tspan=[0:.01:25]; % time span
x0 = [2; 0]; % initial conditions
options = odeset('RelTol',1e-10,'AbsTol',1e-10*ones(1,n));
[t,x]=ode45(@(t,x)rhs(x),tspan,x0,options);
但是我不太清楚应该如何翻译。如果有人可以帮助将 MATLAB 代码翻译成 R 以便通过 R 中的 ODE 函数将其放入,那将是一个很大的帮助。
提前致谢。
以下是否回答了您的问题?注意以下几点:
%*%
是矩阵乘积
- 默认的
method = "lsoda"
会比 method = "ode45"
好
library("deSolve")
times <- seq(0, 25, .01)
state.linear <- c(X = 2, Y = 0) # Initial State
A <- matrix(c(-.1, 2, -2, -.1), 2, 2)
b <- c(2, 0)
linear2D <- function(t, A, b) {
with(as.list(c(A, b)), {
dX <- A %*% b
list(c(dX))
})
}
out.linear2D <- ode(y = state.linear, times = times,
func = linear2D, parms = A, method="ode45",
atol = 1e-10, rtol = 1e-10)
## time series
plot(out.linear2D)
## phase plot
plot(out.linear2D[, -1], type="l")
我目前正在尝试在 R 中求解二维线性系统。我有以下函数:
times <- seq(0,25,.01)
state.linear <- c(X = 2, Y = 0) # Initial State
A <- matrix(c(-.1, 2, -2, -.1), 2, 2)
b <- c(2,0)
linear2D <- function(t, A, b) {
with(as.list(c(A, b)), {
dX <- A*b
list(c(dX))
})
}
out.linear2D <- ode(y = state.linear, times = times, func = linear2D, parms = A)
我想通过 deSolve 包中的 ODE 求解器来解决这个问题,但是 运行 在使用初始函数时遇到了麻烦。我正在尝试从 MATLAB 翻译代码:
n = 2; % 2D system
A = [-.1 2; -2 -.1]; % dynamics
rhs = @(x)A*x; % right hand side
tspan=[0:.01:25]; % time span
x0 = [2; 0]; % initial conditions
options = odeset('RelTol',1e-10,'AbsTol',1e-10*ones(1,n));
[t,x]=ode45(@(t,x)rhs(x),tspan,x0,options);
但是我不太清楚应该如何翻译。如果有人可以帮助将 MATLAB 代码翻译成 R 以便通过 R 中的 ODE 函数将其放入,那将是一个很大的帮助。
提前致谢。
以下是否回答了您的问题?注意以下几点:
%*%
是矩阵乘积- 默认的
method = "lsoda"
会比method = "ode45"
好
library("deSolve")
times <- seq(0, 25, .01)
state.linear <- c(X = 2, Y = 0) # Initial State
A <- matrix(c(-.1, 2, -2, -.1), 2, 2)
b <- c(2, 0)
linear2D <- function(t, A, b) {
with(as.list(c(A, b)), {
dX <- A %*% b
list(c(dX))
})
}
out.linear2D <- ode(y = state.linear, times = times,
func = linear2D, parms = A, method="ode45",
atol = 1e-10, rtol = 1e-10)
## time series
plot(out.linear2D)
## phase plot
plot(out.linear2D[, -1], type="l")