Re: [R] Question with uniroot function

2015-04-16 Thread li li
Thank you.



2015-04-16 12:33 GMT-04:00 William Dunlap :

> Use optimize() to find the minimum and feed that value into uniroot().
>
>  Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>  On Thu, Apr 16, 2015 at 7:47 AM, li li  wrote:
>
>>  Hi Jeff,
>>   Thanks for the reply. I am aware that the sign needs to be different at
>> the ends of the starting interval.
>>
>>Another question:
>>
>> Is there a way to set the right end point ( (the "upper" argument in the
>> uniroot function below) as the point where the function takes on its
>> minimun, for example my function f1 below?
>>
>> Thanks very much!
>>
>>
>>
>> u1 <- -3
>> u2 <- 4
>> pi0 <- 0.8
>>
>> f1 <- function(lambda,z,p1){
>> lambda*(p1*exp(u1*z-u1^2/2)+(0.2-p1)*exp(u2*z-u2^2/2))-(1-lambda)*pi0}
>>
>>  x <- seq(-20,20, by=0.1)
>> y <- numeric(length(x))
>> for (i in 1:length(x)){y[i] <- f1(x[i],p1=0.15,lambda=0.998)}
>> plot(y ~ x, ylim=c(-1,1))
>> abline(h=0)
>>
>>
>> a <- uniroot(f1, lower =-10, upper = 0,
>>tol = 1e-20,p1=0.15,lambda=0.998)$root
>>
>>
>>
>>
>>
>> 2015-04-15 22:57 GMT-04:00 Jeff Newmiller :
>>
>> > You really need to read the help page for uniroot. The sign needs to be
>> > different at the ends of the starting interval. This is a typical
>> > limitation of numerical root finders.
>> >
>> ---
>> > Jeff NewmillerThe .   .  Go
>> Live...
>> > DCN:Basics: ##.#.   ##.#.  Live
>> > Go...
>> >   Live:   OO#.. Dead: OO#..  Playing
>> > Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
>> > /Software/Embedded Controllers)   .OO#.   .OO#.
>> rocks...1k
>> >
>> ---
>> > Sent from my phone. Please excuse my brevity.
>> >
>> > On April 15, 2015 7:20:04 PM PDT, li li  wrote:
>> > >Hi all,
>> > >In the following code, I am trying to use uniroot function to solve for
>> > >the root (a and b in code below) for function f1.
>> > >I am not sure why uniroot function does not give the answer since when
>> > >we
>> > >look the graph, the function does cross 0 twice.
>> > >Any suggestion?
>> > >   Thanks.
>> > >   Hanna
>> > >
>> > >u1 <- -3
>> > >u2 <- 4
>> > >pi0 <- 0.8
>> > >
>> > >f1 <- function(lambda,z,p1){
>> > >lambda*(p1*exp(u1*z-u1^2/2)+(0.2-p1)*exp(u2*z-u2^2/2))-(1-lambda)*pi0}
>> > >
>> > >a <- uniroot(f1, lower =-10, upper = 0,
>> > >   tol = 1e-20,p1=0.15,lambda=0.998)$root
>> > >
>> > >b <- uniroot(f1, lower =0, upper = 10,
>> > >   tol = 1e-20,p1=0.15,lambda=0.998)$root
>> > >
>> > >x <- seq(-20,20, by=0.1)
>> > >y <- numeric(length(x))
>> > >for (i in 1:length(x)){y[i] <- f1(x[i],p1=0.15,lambda=0.998)}
>> > >plot(y ~ x, ylim=c(-1,1))
>> > >abline(h=0)
>> > >
>> > >   [[alternative HTML version deleted]]
>> > >
>> > >__
>> > >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > >https://stat.ethz.ch/mailman/listinfo/r-help
>> > >PLEASE do read the posting guide
>> > >http://www.R-project.org/posting-guide.html
>> 
>> > 
>> > >and provide commented, minimal, self-contained, reproducible code.
>> >
>> >
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> 
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Question with uniroot function

2015-04-16 Thread William Dunlap
Use optimize() to find the minimum and feed that value into uniroot().

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Apr 16, 2015 at 7:47 AM, li li  wrote:

> Hi Jeff,
>   Thanks for the reply. I am aware that the sign needs to be different at
> the ends of the starting interval.
>
>Another question:
>
> Is there a way to set the right end point ( (the "upper" argument in the
> uniroot function below) as the point where the function takes on its
> minimun, for example my function f1 below?
>
> Thanks very much!
>
>
>
> u1 <- -3
> u2 <- 4
> pi0 <- 0.8
>
> f1 <- function(lambda,z,p1){
> lambda*(p1*exp(u1*z-u1^2/2)+(0.2-p1)*exp(u2*z-u2^2/2))-(1-lambda)*pi0}
>
>  x <- seq(-20,20, by=0.1)
> y <- numeric(length(x))
> for (i in 1:length(x)){y[i] <- f1(x[i],p1=0.15,lambda=0.998)}
> plot(y ~ x, ylim=c(-1,1))
> abline(h=0)
>
>
> a <- uniroot(f1, lower =-10, upper = 0,
>tol = 1e-20,p1=0.15,lambda=0.998)$root
>
>
>
>
>
> 2015-04-15 22:57 GMT-04:00 Jeff Newmiller :
>
> > You really need to read the help page for uniroot. The sign needs to be
> > different at the ends of the starting interval. This is a typical
> > limitation of numerical root finders.
> >
> ---
> > Jeff NewmillerThe .   .  Go
> Live...
> > DCN:Basics: ##.#.   ##.#.  Live
> > Go...
> >   Live:   OO#.. Dead: OO#..  Playing
> > Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
> > /Software/Embedded Controllers)   .OO#.   .OO#.
> rocks...1k
> >
> ---
> > Sent from my phone. Please excuse my brevity.
> >
> > On April 15, 2015 7:20:04 PM PDT, li li  wrote:
> > >Hi all,
> > >In the following code, I am trying to use uniroot function to solve for
> > >the root (a and b in code below) for function f1.
> > >I am not sure why uniroot function does not give the answer since when
> > >we
> > >look the graph, the function does cross 0 twice.
> > >Any suggestion?
> > >   Thanks.
> > >   Hanna
> > >
> > >u1 <- -3
> > >u2 <- 4
> > >pi0 <- 0.8
> > >
> > >f1 <- function(lambda,z,p1){
> > >lambda*(p1*exp(u1*z-u1^2/2)+(0.2-p1)*exp(u2*z-u2^2/2))-(1-lambda)*pi0}
> > >
> > >a <- uniroot(f1, lower =-10, upper = 0,
> > >   tol = 1e-20,p1=0.15,lambda=0.998)$root
> > >
> > >b <- uniroot(f1, lower =0, upper = 10,
> > >   tol = 1e-20,p1=0.15,lambda=0.998)$root
> > >
> > >x <- seq(-20,20, by=0.1)
> > >y <- numeric(length(x))
> > >for (i in 1:length(x)){y[i] <- f1(x[i],p1=0.15,lambda=0.998)}
> > >plot(y ~ x, ylim=c(-1,1))
> > >abline(h=0)
> > >
> > >   [[alternative HTML version deleted]]
> > >
> > >__
> > >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > >https://stat.ethz.ch/mailman/listinfo/r-help
> > >PLEASE do read the posting guide
> > >http://www.R-project.org/posting-guide.html
> > 
> > >and provide commented, minimal, self-contained, reproducible code.
> >
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Question with uniroot function

2015-04-16 Thread li li
Hi Jeff,
  Thanks for the reply. I am aware that the sign needs to be different at
the ends of the starting interval.

   Another question:

Is there a way to set the right end point ( (the "upper" argument in the
uniroot function below) as the point where the function takes on its
minimun, for example my function f1 below?

Thanks very much!



u1 <- -3
u2 <- 4
pi0 <- 0.8

f1 <- function(lambda,z,p1){
lambda*(p1*exp(u1*z-u1^2/2)+(0.2-p1)*exp(u2*z-u2^2/2))-(1-lambda)*pi0}

 x <- seq(-20,20, by=0.1)
y <- numeric(length(x))
for (i in 1:length(x)){y[i] <- f1(x[i],p1=0.15,lambda=0.998)}
plot(y ~ x, ylim=c(-1,1))
abline(h=0)


a <- uniroot(f1, lower =-10, upper = 0,
   tol = 1e-20,p1=0.15,lambda=0.998)$root





2015-04-15 22:57 GMT-04:00 Jeff Newmiller :

> You really need to read the help page for uniroot. The sign needs to be
> different at the ends of the starting interval. This is a typical
> limitation of numerical root finders.
> ---
> Jeff NewmillerThe .   .  Go Live...
> DCN:Basics: ##.#.   ##.#.  Live
> Go...
>   Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
> /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
> ---
> Sent from my phone. Please excuse my brevity.
>
> On April 15, 2015 7:20:04 PM PDT, li li  wrote:
> >Hi all,
> >In the following code, I am trying to use uniroot function to solve for
> >the root (a and b in code below) for function f1.
> >I am not sure why uniroot function does not give the answer since when
> >we
> >look the graph, the function does cross 0 twice.
> >Any suggestion?
> >   Thanks.
> >   Hanna
> >
> >u1 <- -3
> >u2 <- 4
> >pi0 <- 0.8
> >
> >f1 <- function(lambda,z,p1){
> >lambda*(p1*exp(u1*z-u1^2/2)+(0.2-p1)*exp(u2*z-u2^2/2))-(1-lambda)*pi0}
> >
> >a <- uniroot(f1, lower =-10, upper = 0,
> >   tol = 1e-20,p1=0.15,lambda=0.998)$root
> >
> >b <- uniroot(f1, lower =0, upper = 10,
> >   tol = 1e-20,p1=0.15,lambda=0.998)$root
> >
> >x <- seq(-20,20, by=0.1)
> >y <- numeric(length(x))
> >for (i in 1:length(x)){y[i] <- f1(x[i],p1=0.15,lambda=0.998)}
> >plot(y ~ x, ylim=c(-1,1))
> >abline(h=0)
> >
> >   [[alternative HTML version deleted]]
> >
> >__
> >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide
> >http://www.R-project.org/posting-guide.html
> 
> >and provide commented, minimal, self-contained, reproducible code.
>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Question with uniroot function

2015-04-15 Thread Jeff Newmiller
You really need to read the help page for uniroot. The sign needs to be 
different at the ends of the starting interval. This is a typical limitation of 
numerical root finders.
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On April 15, 2015 7:20:04 PM PDT, li li  wrote:
>Hi all,
>In the following code, I am trying to use uniroot function to solve for
>the root (a and b in code below) for function f1.
>I am not sure why uniroot function does not give the answer since when
>we
>look the graph, the function does cross 0 twice.
>Any suggestion?
>   Thanks.
>   Hanna
>
>u1 <- -3
>u2 <- 4
>pi0 <- 0.8
>
>f1 <- function(lambda,z,p1){
>lambda*(p1*exp(u1*z-u1^2/2)+(0.2-p1)*exp(u2*z-u2^2/2))-(1-lambda)*pi0}
>
>a <- uniroot(f1, lower =-10, upper = 0,
>   tol = 1e-20,p1=0.15,lambda=0.998)$root
>
>b <- uniroot(f1, lower =0, upper = 10,
>   tol = 1e-20,p1=0.15,lambda=0.998)$root
>
>x <- seq(-20,20, by=0.1)
>y <- numeric(length(x))
>for (i in 1:length(x)){y[i] <- f1(x[i],p1=0.15,lambda=0.998)}
>plot(y ~ x, ylim=c(-1,1))
>abline(h=0)
>
>   [[alternative HTML version deleted]]
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Question with uniroot function

2015-04-15 Thread li li
Hi all,
   In the following code, I am trying to use uniroot function to solve for
the root (a and b in code below) for function f1.
I am not sure why uniroot function does not give the answer since when we
look the graph, the function does cross 0 twice.
Any suggestion?
   Thanks.
   Hanna

u1 <- -3
u2 <- 4
pi0 <- 0.8

f1 <- function(lambda,z,p1){
lambda*(p1*exp(u1*z-u1^2/2)+(0.2-p1)*exp(u2*z-u2^2/2))-(1-lambda)*pi0}

a <- uniroot(f1, lower =-10, upper = 0,
   tol = 1e-20,p1=0.15,lambda=0.998)$root

b <- uniroot(f1, lower =0, upper = 10,
   tol = 1e-20,p1=0.15,lambda=0.998)$root

x <- seq(-20,20, by=0.1)
y <- numeric(length(x))
for (i in 1:length(x)){y[i] <- f1(x[i],p1=0.15,lambda=0.998)}
plot(y ~ x, ylim=c(-1,1))
abline(h=0)

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] question with uniroot function

2011-09-04 Thread Carl Witthoft



As others will tell you: you need to provide a reproducible example. 
What are p1, u1, u2 ?



Dear all,

   I have the following problem with the uniroot function. I want to 
find roots for the fucntion "Fp2" which is defined as below.


Fz <- function(z){0.8*pnorm(z)+p1*pnorm(z-u1)+(0.2-p1)*pnorm(z-u2)}

Fp <- function(t){(1-Fz(abs(qnorm(1-(t/2)+(Fz(-abs(qnorm(1-(t/2)}

Fp2 <- function(t) {Fp(t)-0.8*t/alpha}

th <- uniroot(Fp2, lower =0, upper =1,

   tol = 0.0001)$root

The result is 0 as shown below.

> th

[1] 0

However, there should be a root between 0.00952 and 0.00955, since the 
function values are of opposite signs as below.


> Fp2(0.00952)

[1] 2.264272e-05
> Fp2(0.00955)

[1] -0.0003657404

Can any one give me a hand here? Thanks a lot.

   Hannah
--
-
Sent from my Cray XK6

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] question with uniroot function

2011-09-03 Thread Uwe Ligges



On 03.09.2011 18:31, li li wrote:

Dear all,
I forgot to mention the values for the parameters in the first email.

u1<- -3

u2<- 4

alpha<- 0.05

p1<- 0.15



Thank you very much.

2011/9/3 li li


Dear all,
I have the following problem with the uniroot function. I want to find
roots for the fucntion "Fp2" which is defined as below.


Fz<- function(z){0.8*pnorm(z)+p1*pnorm(z-u1)+(0.2-p1)*pnorm(z-u2)}


Fp<- function(t){(1-Fz(abs(qnorm(1-(t/2)+(Fz(-abs(qnorm(1-(t/2)}


Fp2<- function(t) {Fp(t)-0.8*t/alpha}



th<- uniroot(Fp2, lower =0, upper =1,

tol = 0.0001)$root


The result is 0 as shown below.



th

[1] 0



No surprise, since

Fp2(0) is 0. And uniroot is finished then and does not need to evaluate 
at a lower starting at some positive value such as:


 uniroot(Fp2, lower=1e-7, upper=1, tol=1e-7)

# 0.009521749


Uwe Ligges







However, there should be a root between 0.00952 and 0.00955, since the
function values are of opposite signs as below.



Fp2(0.00952)

[1] 2.264272e-05

Fp2(0.00955)

[1] -0.0003657404

Can any one give me a hand here? Thanks a lot.
Hannah



[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] question with uniroot function

2011-09-03 Thread li li
Dear all,
   I forgot to mention the values for the parameters in the first email.

u1 <- -3

u2 <- 4

alpha <- 0.05

p1 <- 0.15



Thank you very much.

2011/9/3 li li 

> Dear all,
>I have the following problem with the uniroot function. I want to find
> roots for the fucntion "Fp2" which is defined as below.
>
>
> Fz <- function(z){0.8*pnorm(z)+p1*pnorm(z-u1)+(0.2-p1)*pnorm(z-u2)}
>
>
> Fp <- function(t){(1-Fz(abs(qnorm(1-(t/2)+(Fz(-abs(qnorm(1-(t/2)}
>
>
> Fp2 <- function(t) {Fp(t)-0.8*t/alpha}
>
>
>
> th <- uniroot(Fp2, lower =0, upper =1,
>
>tol = 0.0001)$root
>
>
> The result is 0 as shown below.
>
>
> > th
> [1] 0
>
>
> However, there should be a root between 0.00952 and 0.00955, since the
> function values are of opposite signs as below.
>
>
> > Fp2(0.00952)
> [1] 2.264272e-05
> > Fp2(0.00955)
> [1] -0.0003657404
>
> Can any one give me a hand here? Thanks a lot.
>Hannah
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] question with uniroot function

2011-09-03 Thread li li
Dear all,
   I have the following problem with the uniroot function. I want to find
roots for the fucntion "Fp2" which is defined as below.


Fz <- function(z){0.8*pnorm(z)+p1*pnorm(z-u1)+(0.2-p1)*pnorm(z-u2)}


Fp <- function(t){(1-Fz(abs(qnorm(1-(t/2)+(Fz(-abs(qnorm(1-(t/2)}


Fp2 <- function(t) {Fp(t)-0.8*t/alpha}



th <- uniroot(Fp2, lower =0, upper =1,

   tol = 0.0001)$root


The result is 0 as shown below.


> th
[1] 0


However, there should be a root between 0.00952 and 0.00955, since the
function values are of opposite signs as below.


> Fp2(0.00952)
[1] 2.264272e-05
> Fp2(0.00955)
[1] -0.0003657404

Can any one give me a hand here? Thanks a lot.
   Hannah

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.