mathematics purrr package map_dbl unroot

hello, I am struggling to find a code which has to combine map_dbl along with uniroot to compute the root between the -3pi/4and3pi/4of each of the three functions inlist_fun which are :

linear = function(x) x
quadratic = function(x) (x - pi/2)^2 - pi/2
sinusoidal = function(x) sin(x)

finally, I should assign this result to a vector root_values having the same names
as list_fun.

What I have tried so far :

interval <- c(-3pi/4, 3pi/4)

linear <- uniroot(function(x) x, - x, interval)
quadratic <- uniroot(function(x) (x - pi/2)^2 - pi/2- x, interval)
sinusoidal <- uniroot(function(x) sin(x)- x, interval)
a <- map_dbl(1, list_fun)
root_values <- c(linear=linear$value, quadratic=quadratic$value, sinusoidal=sinusoidal$value)

Hi, welcome!

Homework inspired questions are welcome here, but you have to tell us what have you tried so far? what is your specific problem? We are more inclined towards helping you with specific coding problems rather than doing your work for you.

For guidelines about how to properly ask homework related questions here, please read our homework policy.

If you have no basis to start, perhaps you would be best served by learning basics of r , swirl package is useful for that.

The free book R for datascience is free online and a great intro to tidyverse way of working in R.

However, perhaps you do have some foundations. Therefore , perhaps you know( or can find reading material) on how to use the uniroot function you have your eye on. At least to where you can use it manually on one function. Then we can discuss such things as generalising and iterating from a specific case

What I tried so far with this question :

interval <- c(-3*pi/4, 3*pi/4)

linear <- uniroot(function(x) x, - x, interval)
quadratic <- uniroot(function(x) (x - pi/2)^2 - pi/2- x, interval)
sinusoidal <- uniroot(function(x) sin(x)- x, interval)
a <- map_dbl(1, list_fun)
root_values <- c(linear=linear$value, quadratic=quadratic$value, sinusoidal=sinusoidal$value)

hello, I am struggling to find a code which has to combine map_dbl along with uniroot to compute the root between the -3 * pi/4 and 3 * pi/4 of each of the three functions in list_fun which are :

linear = function(x) x
quadratic = function(x) (x - pi/2)^2 - pi/2
sinusoidal = function(x) sin(x)

finally, I should assign this result to a vector root_values having the same names
as list_fun .

What I have tried so far :

interval <- c(-3 *pi/4, 3* pi/4)

linear <- uniroot(function(x) x, - x, interval)
quadratic <- uniroot(function(x) (x - pi/2)^2 - pi/2- x, interval)
sinusoidal <- uniroot(function(x) sin(x)- x, interval)
a <- map_dbl(1, list_fun)
root_values <- c(linear=linear$value, quadratic=quadratic$value, sinusoidal=sinusoidal$value)
library(tidyverse)
linear <- function(x) x
quadratic <- function(x) (x - pi/2)^2 - pi/2
sinusoidal <- function(x) sin(x)
(list_fun <- c(linear,quadratic,sinusoidal) %>% 
    purrr::set_names(c("linear","quadratic","sinusoidal")) %>%
    purrr::map(~.))


interval <- c(-3 *pi/4, 3* pi/4)

uniroot(linear,interval)$root
uniroot(quadratic,interval)$root
uniroot(sinusoidal,interval)$root

map_dbl(list_fun,
        ~uniroot(.,interval)$root)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.