使用三个不同矩阵的值求方程的根
Finding root of an equation by using value of three different matrices
我有三个矩阵,分别名为 a_b
、 pop
和 event
df<- data_frame(a=c(-5.8221,-6.4678,-6.1213,-5.4518,-5.3527,-5.3517,-5.3169),
b= c(.0234,.0199,.0162,.0151,.0148,.0138,.0123))
a_b<-as.matrix(df[1:7, 1:2])
df1<- data_frame("2000" =c(716209.27,642504.69,644640.11,632239.35,552437.34,554395.34,539358.87),
"2005" = c(653040.76,637752.95,651635.40,647572.55,641183.99,570938.83,554092.75),
"2010" =c(5643.6,876123,665412.5,98643,89012,776321,8863))
pop<-as.matrix(df1[1:7, 1:3])
df2<- data_frame("2000"=c(9968.50418,2605.16992,2937.01261,4286.23376,5718.80904,4843.19769,5077.52440),
"2005"= c(6037.16994,2336.72681,2391.55031,4085.62207,5000.77218,4425.02331,5097.25052),
"2010" =c(19123.45,87765.54,102345.44,98000,12396,90076,10133))
event <-as.matrix(df2[1:7, 1:3])
使用以下函数,我想应用 uniroot
来查找 event
和 pop
的每一行的根。 a
和 b
in a_b
对于 event
和 pop
的每一列都是常量。
fun<-function(x) (event - (exp(a+b* x )* pop))
u1 <- uniroot(fun, c(-10000,10000),extendInt="yes", trace=1)$root
我的问题是我应该如何将我的函数应用于 a
、b
、event
和 pop
中的相应值。因为我有三年时间,所以我的预期输出是这样的:
2000 2005 2010
66.1349 48.6493 300.9612
48.2373 43.1457 209.395
45.0617 31.7133 262.299
30.3271 25.5657 360.6132
52.8459 33.7145 228.4664
44.3041 35.6299 231.7237
52.9546 51.0787 443.1554
这可能不是最有效的实施方式,但七行只用了我的计算机大约 0.03 秒。我遍历所有不同的参数选项并每次都使用 uniroot()
。输出与您的示例输出相当吻合。
df<- data.frame(a=c(-5.8221,-6.4678,-6.1213,-5.4518,-5.3527,-5.3517,-5.3169),
b= c(.0234,.0199,.0162,.0151,.0148,.0138,.0123))
a_b<-as.matrix(df[1:7, 1:2])
df1<- data.frame("2000" =c(716209.27,642504.69,644640.11,632239.35,552437.34,554395.34,539358.87),
"2005" = c(653040.76,637752.95,651635.40,647572.55,641183.99,570938.83,554092.75),
"2010" =c(5643.6,876123,665412.5,98643,89012,776321,8863))
pop<-as.matrix(df1[1:7, 1:3])
df2<- data.frame("2000"=c(9968.50418,2605.16992,2937.01261,4286.23376,5718.80904,4843.19769,5077.52440),
"2005"= c(6037.16994,2336.72681,2391.55031,4085.62207,5000.77218,4425.02331,5097.25052),
"2010" =c(19123.45,87765.54,102345.44,98000,12396,90076,10133))
event <-as.matrix(df2[1:7, 1:3])
fun<-function(x) (event_t - (exp(a_t+b_t* x )* pop_t))
output <- data.frame(matrix(ncol=3,nrow=0, dimnames=list(NULL, c("2000", "2005", "2010"))))
start_time <- Sys.time()
for(i in 1:nrow(pop)){
row_vals = rep(NA,3)
for(j in 1:ncol(pop)){
a_t=a_b[i,1]
b_t=a_b[i,2]
event_t=event[i,j]
pop_t=pop[i,j]
u1 <- uniroot(fun,interval=c(-10000,10000),extendInt="yes", trace=1)$root
row_vals[j] <- u1
}
output <- rbind(output, row_vals)
}
end_time <- Sys.time()
print(end_time - start_time)
names(output) <- c("2000", "2005", "2010")
output
输出为:
2000 2005 2010
1 66.13496 48.64939 300.9612
2 48.23738 43.14574 209.3951
3 45.06177 31.71337 262.2992
4 30.32718 25.56577 360.6133
5 52.84598 33.71459 228.4664
6 44.30414 35.62997 231.7237
7 52.95467 51.07877 443.1555
我有三个矩阵,分别名为 a_b
、 pop
和 event
df<- data_frame(a=c(-5.8221,-6.4678,-6.1213,-5.4518,-5.3527,-5.3517,-5.3169),
b= c(.0234,.0199,.0162,.0151,.0148,.0138,.0123))
a_b<-as.matrix(df[1:7, 1:2])
df1<- data_frame("2000" =c(716209.27,642504.69,644640.11,632239.35,552437.34,554395.34,539358.87),
"2005" = c(653040.76,637752.95,651635.40,647572.55,641183.99,570938.83,554092.75),
"2010" =c(5643.6,876123,665412.5,98643,89012,776321,8863))
pop<-as.matrix(df1[1:7, 1:3])
df2<- data_frame("2000"=c(9968.50418,2605.16992,2937.01261,4286.23376,5718.80904,4843.19769,5077.52440),
"2005"= c(6037.16994,2336.72681,2391.55031,4085.62207,5000.77218,4425.02331,5097.25052),
"2010" =c(19123.45,87765.54,102345.44,98000,12396,90076,10133))
event <-as.matrix(df2[1:7, 1:3])
使用以下函数,我想应用 uniroot
来查找 event
和 pop
的每一行的根。 a
和 b
in a_b
对于 event
和 pop
的每一列都是常量。
fun<-function(x) (event - (exp(a+b* x )* pop))
u1 <- uniroot(fun, c(-10000,10000),extendInt="yes", trace=1)$root
我的问题是我应该如何将我的函数应用于 a
、b
、event
和 pop
中的相应值。因为我有三年时间,所以我的预期输出是这样的:
2000 2005 2010
66.1349 48.6493 300.9612
48.2373 43.1457 209.395
45.0617 31.7133 262.299
30.3271 25.5657 360.6132
52.8459 33.7145 228.4664
44.3041 35.6299 231.7237
52.9546 51.0787 443.1554
这可能不是最有效的实施方式,但七行只用了我的计算机大约 0.03 秒。我遍历所有不同的参数选项并每次都使用 uniroot()
。输出与您的示例输出相当吻合。
df<- data.frame(a=c(-5.8221,-6.4678,-6.1213,-5.4518,-5.3527,-5.3517,-5.3169),
b= c(.0234,.0199,.0162,.0151,.0148,.0138,.0123))
a_b<-as.matrix(df[1:7, 1:2])
df1<- data.frame("2000" =c(716209.27,642504.69,644640.11,632239.35,552437.34,554395.34,539358.87),
"2005" = c(653040.76,637752.95,651635.40,647572.55,641183.99,570938.83,554092.75),
"2010" =c(5643.6,876123,665412.5,98643,89012,776321,8863))
pop<-as.matrix(df1[1:7, 1:3])
df2<- data.frame("2000"=c(9968.50418,2605.16992,2937.01261,4286.23376,5718.80904,4843.19769,5077.52440),
"2005"= c(6037.16994,2336.72681,2391.55031,4085.62207,5000.77218,4425.02331,5097.25052),
"2010" =c(19123.45,87765.54,102345.44,98000,12396,90076,10133))
event <-as.matrix(df2[1:7, 1:3])
fun<-function(x) (event_t - (exp(a_t+b_t* x )* pop_t))
output <- data.frame(matrix(ncol=3,nrow=0, dimnames=list(NULL, c("2000", "2005", "2010"))))
start_time <- Sys.time()
for(i in 1:nrow(pop)){
row_vals = rep(NA,3)
for(j in 1:ncol(pop)){
a_t=a_b[i,1]
b_t=a_b[i,2]
event_t=event[i,j]
pop_t=pop[i,j]
u1 <- uniroot(fun,interval=c(-10000,10000),extendInt="yes", trace=1)$root
row_vals[j] <- u1
}
output <- rbind(output, row_vals)
}
end_time <- Sys.time()
print(end_time - start_time)
names(output) <- c("2000", "2005", "2010")
output
输出为:
2000 2005 2010
1 66.13496 48.64939 300.9612
2 48.23738 43.14574 209.3951
3 45.06177 31.71337 262.2992
4 30.32718 25.56577 360.6133
5 52.84598 33.71459 228.4664
6 44.30414 35.62997 231.7237
7 52.95467 51.07877 443.1555