使用来自不同数据框的值作为嵌套函数的输入 [英] use values from different dataframes as input for nested function
问题描述
如何使用数据框 I
中的值运行下面的一段代码,即函数 ftn4
,由 newtonraphson
函数结合数据框 R0
中的值使用。我想将输出存储在相同维度(ncol = 5,nrow = 5)的新数据框中( R
)为 I
an R0
。
How do I run the following piece of code that the output of function ftn4
using values from data frame I
, is used by the newtonraphson
function in combination with values from data frame R0
. I would like to store the output in a new data frame (R
) of the same dimensions (ncol = 5, nrow = 5) as I
an R0
.
我卡住了,有什么想法?任何提示都非常感谢
I am stuck, any ideas? Any hint greatly appreciated
c = c(0.3)
Ccf = c(0.3)
Acf = c(3*10^-6)
E = c(0.00006)
Ke = c(1000)
I = as.data.frame(matrix(sample(seq(150,300,0.1),25), ncol = 5, nrow = 5))
R0 = as.data.frame(matrix(sample(seq(0.01,0.2,0.001),25), ncol = 5, nrow = 5))
library(spuRs)
ftn4 <- function(x) {
f = { x^2*exp(c*x)- (Ccf*Acf*E*(I/Ke+I))}
f1 = { 2 * x * exp(c * x) + x^2 * (exp(c * x) * c)}
return(c(f,f1))
}
R = as.data.frame(newtonraphson(ftn4, R0, tol = 1e-09 ,max.iter = 1000))
推荐答案
传递给函数ftn4的参数产生一个值。所以ftn4应该被改变:
As appointed by the comments, you should pass arguments to the function ftn4 that yields to a value. So ftn4, should be changed by:
ftn4 <- function(x,I) {
f = { x^2*exp(c*x)- (Ccf*Acf*E*(I/Ke+I))}
f1 = { 2 * x * exp(c * x) + x^2 * (exp(c * x) * c)}
return(c(f,f1))
}
现在迭代你的矩阵I和R0。
And now iterate over your matrix I and R0.
R <- data.frame(matrix(NA,nrow=nrow(I), ncol(I)))
for(i in 1:nrow(I)){
for(j in 1:ncol(I)){
R[i,j] <- newtonraphson(ftn = function(x) ftn4(x, I[i,j]),
x0 = R0[i,j],
tol = 1e-09,
max.iter = 1000)
}
}
这篇关于使用来自不同数据框的值作为嵌套函数的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!