How can I find the different points of a system of

2019-08-09 05:01发布

问题:

This question is an exact duplicate of:

  • How to solve a set of Linear equations? 2 answers

I've got to solve this system of equations:

aQ + bP = c
dQ + eP = f

I have to find 100 values Q and P that solve the systems with 100 randomly drawn coefficients from the following distributions:

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)

Which I did by doing the following:

a<-rnorm(100,mean=100,sd=10)
b<-rnorm(100,mean=-1,sd=.1)
c<-rnorm(100,mean=10,sd=1)
d<-rnorm(100,mean=10,0.1)
e<-rnorm(100,mean=100,sd=10)
f<-rnorm(100,mean=10,0.1)

Then I made matrix:

O <- matrix(data=c(a,b,c),1,1)
P<- matrix(c(d,e,f),1,1)

Finally solved it using:

solve(O,P)

My problem is that I'm trying to get 100 solutions but this code returns just one solution. After obtaining the 100 values for Q and P I need to make a plot with all of the values.

回答1:

You can use replicate to repeatedly solve your system, each time using a different random draw for the coefficients:

QP <- t(replicate(100, solve(rbind(c(rnorm(1, 100, 10), rnorm(1, -1, 0.1)),
                                   c(rnorm(1, 10, .1), rnorm(1, 100, 10))),
                             c(rnorm(1, 10, 1), rnorm(1, 10, 0.1)))))
colnames(QP) <- c("Q", "P")
plot(QP)



标签: r regression