[R] uniroot function question
Berend Hasselman
bhh at xs4all.nl
Wed Dec 14 18:57:00 CET 2011
kchkchkch wrote
>
> I have one equation, two unknowns, so I am trying to build the solution
> set by running through possible values for one unknown, and then using
> uniroot to solve for the accompanying second solution, then graphing the
> two vectors.
>
> p0 = .36
> f = function(x) 0.29 * exp(5.66*(x - p0))
> f.integral = integrate(f, p0, 1)
> p1 = p0 + .01
> i = 1
> n = (1 - p0)/.01
> p1.vector = rep(0,n)
> p2.vector = rep(0,n)
> for (i in 1:n) {
> p1.vector[i] = p1
> fcn = function(p2) p1*f(p1) + (.20/5.66)*(exp(5.66*(p2 - p0)) -
> exp(5.66*(p1 - p0))) + (1 - p2)*f(p2) - as.numeric(f.integral$value)
> sol = uniroot(try, lower = p1, upper = 1)
> p2.vector[i] = p2
> i = i+1
> p1 = p1 + .01
> }
> plot(p1.vector,p2.vector)
>
> p1, p2 both have to be between p0 and 1, p1 < p2. Is there a better way
> to do this? I keep getting the error that my lower and upper bounds are
> not of opposite sign, but I don't know how to find the correct interval
> values in that case. This may not even be a uniroot question (although I
> don't know how to find those values in a general sense), if there is a
> better way to do the same thing.
>
Shouldn't the line with uniroot be (why try as function?)
sol = uniroot(fcn, lower = p1, upper = 1)
Before the line with for(i in 1:n) insert the following
fcn = function(p2) p1*f(p1) + (.20/5.66)*(exp(5.66*(p2 - p0)) - exp(5.66*(p1
- p0))) + (1 - p2)*f(p2) - as.numeric(f.integral$value)
curve(fcn, from=0, to=1)
And it should be obvious what the problem is.
Berend
--
View this message in context: http://r.789695.n4.nabble.com/uniroot-function-question-tp4195737p4196220.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list