Skip to content
Prev 300969 / 398506 Next

Solving equations in R

Diviya Smith wrote
This should help you  to get started.

library(nleqslv)

f <- function(a, amp=.2370,y=0.0233, d=0.002, x=0.091) {
    amp*y^2 - ( 2*a*(1-a)*(-a*d+(1-a)*x)^2 )
}

# draw the function
curve(f, from=-0.2, to=1.5, col="blue")
abline(h=0, col="red")

# use nleqslv
# use two different starting values to see if we can solve for the two roots
# you can leave out the control=... argument
astart <- 1
nleqslv(astart,f, control=list(trace=1))

astart <- 0
nleqslv(astart,f, control=list(trace=1))

# use uniroot
# limit the lower/upper interval to the two roots
# ranges chosen from the plot so that function values at the endpoints have
different signs
uniroot(f,c(-0.5,.1))
uniroot(f,c(0.5,1.5))

Berend





--
View this message in context: http://r.789695.n4.nabble.com/Solving-equations-in-R-tp4637498p4637503.html
Sent from the R help mailing list archive at Nabble.com.