Quantcast

Solving equations in R

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Solving equations in R

Diviya Smith
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?

Thanks,
Diviya

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Solving equations in R

Duncan Murdoch-2
On 23/07/2012 2:46 PM, 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?

See ?polyroot.

Duncan Murdoch

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Solving equations in R

Berend Hasselman
In reply to this post by Diviya Smith
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

Loading...