applying uniroot function to each element in vector

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

applying uniroot function to each element in vector

Dale-2
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: applying uniroot function to each element in vector

David Winsemius

On Apr 27, 2011, at 11:54 AM, Dale wrote:

> If I have a vector of n elements, e.g. a vector of length 4 with  
> elements 10,
> 20, 30, 40 and want to find the different values of x such that  
> x^2=10,
> x^2=20, x^30 and x^2=40, how could I do this in R? I'm thinking of  
> using the
> uniroot function, but am finding difficult applying it to a vector.  
> Thanks

I find myself scratching my head wondering why the answer is not:

y=c(10,20,30,40)
x=sqrt(y)
x

>
> --
> View this message in context: http://r.789695.n4.nabble.com/applying-uniroot-function-to-each-element-in-vector-tp3478573p3478573.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[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
|

Re: applying uniroot function to each element in vector

Rolf Turner-3
On 28/04/11 06:12, David Winsemius wrote:

>
> On Apr 27, 2011, at 11:54 AM, Dale wrote:
>
>> If I have a vector of n elements, e.g. a vector of length 4 with
>> elements 10,
>> 20, 30, 40 and want to find the different values of x such that x^2=10,
>> x^2=20, x^30 and x^2=40, how could I do this in R? I'm thinking of
>> using the
>> uniroot function, but am finding difficult applying it to a vector.
>> Thanks
>
> I find myself scratching my head wondering why the answer is not:
>
> y=c(10,20,30,40)
> x=sqrt(y)
> x

My guess is that he really wants to solve foo(x) = y_i for each entry y_i of
some vector y, and that ``x^2'' was just an over-simplified version of foo()
(and c(10,20,30,40) was an over-simplified version of y).

I think that something like

     lapply(y,function(a){uniroot(function(t){foo(t) - a},lower=L,upper=U)})

(untested) might be what is wanted (for appropriately chosen values of L
and U).

Things get more complicated if L and U need to be chosen differently for
each entry of y.

     cheers,

         Rolf Turner

______________________________________________
[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
|

Re: applying uniroot function to each element in vector

Dale-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: applying uniroot function to each element in vector

David Winsemius

On Apr 27, 2011, at 9:29 PM, Dale wrote:

> Yes my example was not the best, it is an oversimplified example. I  
> tried
> using your method and didn't seem to work.

And to whom might you be addressing this complaint?

>
> --
> View this message in context: http://r.789695.n4.nabble.com/applying-uniroot-function-to-each-element-in-vector-tp3478573p3479895.html
> Sent from the R help mailing list archive at Nabble.com.

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[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
|

Re: applying uniroot function to each element in vector

main
This post has NOT been accepted by the mailing list yet.
In reply to this post by Rolf Turner-3
CONTENTS DELETED
The author has deleted this message.