Find function value in rstudio

I have a function

fun1 <- function(x) -(2 * (sin(x) * cos(x)))

Now I want to find for what values ​​x, fun1 = 0

In addition, I have a function

fun2 <- function(x) 2 * (cos(x) * sin(x))

Now I also want to find for what values ​​BOTH fun1 = 0 AND fun2 = 0

Hope someone can help

Hi,

s

+4
source share
2 answers

You can use unirootto find the root of the function. In this case, there are several roots for both functions (actually an infinite sum), so you will need to specify an interval. You can do this with visual inspection. Let us first build the functions on the range (0,6):

fun1 <- function(x) -(2 * (sin(x) * cos(x)))
fun2 <- function(x) 2 * (cos(x) * sin(x))

x = seq(0,6,0.01)
plot(x,fun1(x),col='blue')
points(x,fun2(x),col='red')

enter image description here

, , fun1 fun2 y=0 , , c(1,2). x -, y = 0 :

uniroot(fun1,c(1,2))$root
uniroot(fun2,c(1,2))$root

1.570795, x-value, fun1(x) = fun2(x) = 0. , !

+2

:

x ,

x<-seq(0,100,by=0.01)

fun1

f1<-x[round(unlist(lapply(x,FUN = fun1)),2)==0] 

fun2

f2<-x[round(unlist(lapply(x,FUN = fun2)),2)==0]

Intersaction

f1[f1 %in% f2]
 [1]  0.00  1.57  3.14  4.71 15.71 17.28 18.85 20.42 21.99 23.56 34.56 36.13 37.70 39.27 40.84 42.41 43.98 54.98 56.55 58.12
[21] 59.69 61.26 62.83 75.40 76.97 78.54 80.11 81.68 83.25 94.25 95.82 97.39 98.96
0

Source: https://habr.com/ru/post/1693021/


All Articles