如何求解线性方程组?

编程入门 行业动态 更新时间:2024-10-11 05:25:08
本文介绍了如何求解线性方程组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要求解线性方程组:

I need to solve a system of linear equations:

aQ + bP = c dQ + eP = f

位置:

a ~ N(100;10) b ~ N(-1;0.1) c ~ N(10;1) d ~ N(10;0.1) e ~ N(100;10) f ~ N(10;0.1)

到目前为止,我已经写过:

So far I have written:

a <- rnorm(100, mean=100, sd=10) b <- rnorm(100, mean=-1, sd=0.1) c <- rnorm(100, mean=10, sd=1) d <- rnorm(100, mean=10, sd=0.1) e <- rnorm(100, mean=100, sd=10) f <- rnorm(100, mean=10, sd=0.1) P <- vector() Q <- vector() for (i in 1:100) { coefs <- matrix(c(a[i],d[i],b[i],e[i]),2,2) ys <- array(c(c,f),2) solve(coefs[i], ys[i]) }

问题是for循环只为我提供了P和Q的一种解决方案,我希望for循环计算100个值集来进行a,b,c,d,e和f并存储向量Q和P上的数据.

The problem is that the for loop is only giving me one solution for P and Q and I would like that the for loop to calculate the 100 set of values do a, b, c, d, e and f and store the data on the vectors Q and P.

推荐答案

您可以尝试使用apply()

# data df = data.frame(a,b,c,d,e,f) # apply function out = t(apply(df, 1, function(x){ coefs = matrix( c(x['a'], x['d'], x['b'], x['e']), 2, 2); ys = array(c(x['c'],x['f']),2); out = solve(coefs, ys); names(out) = c('P','Q'); out}))

或者使用sapply()

out = t(sapply(seq(100), function(i){ coefs = matrix(c(a[i], d[i], b[i] ,e[i]),2,2); ys = array(c(c[i],f[i]),2); out = solve(coefs, ys); names(out) = c('P','Q'); out}))

如果您想使用for循环,这是您可以做的

and if you want to use for loop, here is what you could do

# declare matrix to store the output out = matrix(ncol=2, nrow=100) # populate declared matrix using for loop for(i in 1:100){ coefs = matrix(c(a[i],d[i],b[i],e[i]),2,2) ys = array(c(c[i],f[i]),2) out[i,] = as.vector(solve(coefs, ys)) colnames(out) = c('P','Q') out}

更多推荐

如何求解线性方程组?

本文发布于:2023-11-30 06:10:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线性方程组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!