簡體   English   中英

R-使用全局變量來求解ode45,類似於MATLAB / GNU Octave

[英]R - use global variables to solve ode45 similar to MATLAB/GNU Octave

我正在嘗試從R版本3.3.1中的GNU Octave / MATLAB重寫代碼。 在原始代碼中,將A和B設置為函數中的全局變量,然后在腳本文件中將A和B均設置為全局變量。

在R中,這是我嘗試使用ode45函數時收到的錯誤消息:

eval(expr,envir,enclos)中的錯誤:找不到對象“ z1”

誰能像在GNU Octave / MATLAB代碼中一樣建議如何在R中設置全局變量?

謝謝。


R代碼如下

#list.R is a wrapper for list used to replicate the GNU Octave/MATLAB syntax


source("https://raw.githubusercontent.com/ggrothendieck/gsubfn/master/R/list.R")

install.load::load_package("ramify", "pracma")

GRT <- function (t, x) {
         A  <- A
         B  <- B
         z1 <- x[1, 1]; z2 <- x[2, 1]; z3 <- x[3, 1]; X <- mat("z1; z2; z3")
         xd <- A * X + B * exp(-t) * sin(400 * t)
}


A <- -mat("2, 3, 2; 1, 5, 3; 2, 3, 1")
B <- mat("1; 3; 2")

ts <- 0.0
tf <- 10
X0 <- c(1, 0, -1)

list[t, x] <- ode45(GRT, ts, tf, X0)

P <- mat("t, x")

matplot(t, x, xlab = "time - (s)", ylab = "x")

我正在使用GNU Octave版本3.8.1來運行代碼。 我在上面試圖復制的是GNU Octave / MATLAB中的以下代碼:

function xd=GRT(t,x)
global A B
z1=x(1,1); z2=x(2,1);z3=x(3,1);X=[z1;z2;z3];
xd =A*X+B*exp(-t)*sin(400*t);
endfunction % only needed for GNU Octave

global A B
A = -[2,3,2;1,5,3;2,3,1];
B = [1;3;2];
ts = 0.0;
tf = 10;
T=[ts,tf]; X0=[1,0,-1];
[t,x] = ode45(@GRT,T,X0)
P = [t,x];
plot(t,x)
xlabel('time - (s)');
ylabel('x');

這是X

X =

1.0000
1.0016
1.0043

t的大小是809行1列。 這是t的局部t

t =

0.00000
0.00305
0.00632
0.00928
0.01226
0.01524
0.01840
0.02186
0.02482
0.02778
0.03079
0.03391
0.03750
0.04046
0.04344
0.04646
0.04959
0.05321
0.05618

x的大小為809行3列。 這是x的局部x

x =

1.0000e+00   0.0000e+00  -1.0000e+00
1.0016e+00   1.0937e-02  -9.9982e-01
1.0043e+00   2.5810e-02  -9.9752e-01
1.0040e+00   3.1460e-02  -1.0007e+00
1.0012e+00   2.9337e-02  -1.0090e+00
9.9908e-01   2.9132e-02  -1.0161e+00
1.0001e+00   3.8823e-02  -1.0170e+00
1.0028e+00   5.4307e-02  -1.0148e+00
1.0026e+00   6.0219e-02  -1.0178e+00
9.9979e-01   5.8198e-02  -1.0260e+00
9.9739e-01   5.7425e-02  -1.0336e+00
9.9809e-01   6.6159e-02  -1.0351e+00
1.0007e+00   8.1786e-02  -1.0331e+00
1.0004e+00   8.7669e-02  -1.0361e+00
9.9753e-01   8.5608e-02  -1.0444e+00
9.9500e-01   8.4599e-02  -1.0522e+00

這是預期的情節:

GRT圖

我相信這就是您想要的:

source("https://raw.githubusercontent.com/ggrothendieck/gsubfn/master/R/list.R")

pacman::p_load(ramify, pracma) # I use pacman, you don't have to

GRT <- function (t, x) {
  X <- mat("z1; z2; z3")
  xd <- A %*% X + B %*% exp(-t) * sin(400 * t)
  return(z1)
}


A <- -mat("2, 3, 2; 1, 5, 3; 2, 3, 1")
B <- mat("1; 3; 2")

ts <- 0.0
tf <- 10
X0 <- c(1, 0, -1)
z1 <- X0[1]
z2 <- X0[2]
z3 <- X0[3] 

GRT(t=ts,x=X0)

list[t, x] <- ode45(GRT, ts, tf, X0)

P <- mat("t, x")

matplot(t, x, xlab = "time - (s)", ylab = "x")

所做的更改:

  1. GRT使用矩陣乘法運算符代替標量乘法
  2. 修復了X0的索引(在函數中稱為x
  3. 注釋掉了GRT 2條不必要的內容
  4. GRT添加了return語句

我必須對語法錯誤的地方做些假設,例如對X0進行索引。 由於我沒有從Octave到參考的示例輸出曲線圖(而且我無法讓您的代碼在Octave CLI中運行),因此我無法確定這些假設是否正確,如果不是,我的曲線圖可能會有所不同。

這是上面代碼的結果圖:

在此處輸入圖片說明

最后的注意:看來您從未使用過P <- mat("t, x")的結果,但是我不認為它會根據結果對象來做您認為正在做的事情。

以下答案使用了各種注釋中的一些元素以及Hack-R的答案。

source("https://raw.githubusercontent.com/ggrothendieck/gsubfn/master/R/list.R")

install.load::load_package("ramify", "pracma")

GRT <- function (t, x) {

z1 <- x[1, 1]; z2 <- x[2, 1]; z3 <- x[3, 1]; X <- matrix(data = c(z1,  
z2, z3), nrow = 3, ncol = 1)

xd <- A %*% X + B * exp(-t) * sin(400 * t)

return(xd)
}


A <- -mat("2, 3, 2; 1, 5, 3; 2, 3, 1")

B <- mat("1; 3; 2")

ts <- 0.0

tf <- 10

X0 <- c(1, 0, -1)

list[t, x] <- ode45(GRT, ts, tf, X0, atol = 0.000001, hmax = 1.0)

matplot(t, x, xlab = "time - (s)", ylab = "x", type = "l")

R中的ode45圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM