[R] Solving equations in R
Berend Hasselman
bhh at xs4all.nl
Mon Jul 23 21:40:37 CEST 2012
Diviya Smith wrote
>
> Hi there,
>
> I would like to solve the following equation in R to estimate 'a'. I have
> the amp, d, x and y.
>
> amp*y^2 = 2*a*(1-a)*(-a*d+(1-a)*x)^2
>
> test data:
> amp = 0.2370 y=
> 0.0233 d=
> 0.002 x=
> 0.091
> Can anyone suggest how I can set this up?
>
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.
More information about the R-help
mailing list