If I have matrics as follows:> a <- c(1,1,0,0) > b <- c(4,4,0,0) > c <- c(3,5,5,6)How can I use R code to solve the equation ax^2+bx+c=0. thanks! yuying shi [[alternative HTML version deleted]]
Hi Have a look at ?polyroot. This might be helpful. Regards, Christoph Buser -------------------------------------------------------------- Christoph Buser <buser at stat.math.ethz.ch> Seminar fuer Statistik, LEO C13 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-44-632-4673 fax: 632-1228 http://stat.ethz.ch/~buser/ -------------------------------------------------------------- Yuying Shi writes: > If I have matrics as follows: > > a <- c(1,1,0,0) > > b <- c(4,4,0,0) > > c <- c(3,5,5,6) > How can I use R code to solve the equation ax^2+bx+c=0. > thanks! > yuying shi > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On 05-Nov-05 Yuying Shi wrote:> If I have matrics as follows: >> a <- c(1,1,0,0) >> b <- c(4,4,0,0) >> c <- c(3,5,5,6) > How can I use R code to solve the equation ax^2+bx+c=0. > thanks! > yuying shiHere is a solution, using the more interesting example in an ealrier mail of yours: a b c 1 4 3 1 4 5 0 2 5 0 0 6 qs<-function(a,b,c){ a<-as.complex(a); b<-as.complex(b); c<-as.complex(c) i2<-(a!=0); i1<-((a==0)&(b!=0)); solns<-as.complex(rep(NA,length(a))) solns<-cbind(solns,solns); colnames(solns)<-c("soln 1","soln 2") a2<-a[i2]; b2<-b[i2]; c2<-c[i2] solns[i2,1]<-(-b2 + sqrt(b2^2 - 4*a2*c2))/(2*a2) solns[i2,2]<-(-b2 - sqrt(b2^2 - 4*a2*c2))/(2*a2) b1<-b[i1]; c1<-c[i1] solns[i1,1]<-(-c1)/b1 solns } a<-c(1,1,0,0); b<-c(4,4,2,0); c<-c(3,5,5,6) qs(a,b,c) soln 1 soln 2 [1,] -1.0+0i -3+0i [2,] -2.0+1i -2-1i [3,] -2.5+0i NA [4,] NA NA Check that a*s^2 + b*s + c = 0: s1<-solns[,1]; s2<-solns[,2] diag(cbind(a,b,c)%*%rbind(s1^2,s1,c(1,1,1,1))) [1] 0.000000e+00-2.449213e-16i -8.881784e-16-1.776357e-15i [3] 0.000000e+00+0.000000e+00i NA diag(cbind(a,b,c)%*%rbind(s2^2,s2,c(1,1,1,1))) [1] 1.776357e-15-2.204291e-15i -8.881784e-16+1.776357e-15i [3] NA NA which is as close to 0 as you can expect to get. Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 07-Nov-05 Time: 20:04:00 ------------------------------ XFMail ------------------------------
The inquiry below and similar ones seem a lot like homework problems. They are distinguished by showing no evidence of the OP's ever having read a manual and also by being problems of apparently pedagogical interest. The Fortran group (comp.lang.fortran) has a long tradition of not giving more than the broadest of hints in response to such inquiries, and I would encourage r-help readers to consider doing the same. MHP Yuying Shi wrote:>If I have matrics as follows: > > >>a <- c(1,1,0,0) >>b <- c(4,4,0,0) >>c <- c(3,5,5,6) >> >> >How can I use R code to solve the equation ax^2+bx+c=0. >thanks! > yuying shi > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >