I have this function:
a <- 1
b <- 2
get_y <- function (x,a,b) {
a * b * x
}
And I want to create a function that takes in get_y and returns the x that makes y = 4 for example. How would I do that?
On
You can try Ryacas like below
library(Ryacas)
y <- 4
f <- sprintf("%s-%s", gsub("\\{|\\}", "", deparse1(body(get_y))), y)
x <- solve(ysym(f), "x")
and you will obtain
> x
{x==4/(a*b)}
If you want numeric solution of x, you can try the code below with a few more lines
get_x <- function(get_y, y, a, b) {
f <- sprintf("%s-%s", gsub("\\{|\\}", "", deparse1(body(get_y))), y)
s <- gsub("x==", "", solve(ysym(f), "x"))
y_eval(yac_expr(s), a = a, b = b, as.r = TRUE)
}
such that
> get_x(get_y, 4, a, b)
[1] 2
> get_x(get_y, 4, a, 5)
[1] 0.8
You can solve
with
uniroot. You don't have to create a new function, an anonymous one will do it.Created on 2023-08-15 with reprex v2.0.2
Edit
Following ThomasIsCoding's comment, here is a heuristic I many times use to find the search limits.
When the functions are (relatively) simple, use
curveto plot the function. If I get it wrong at the first try, expand the limits until I no longer do. In this case, it was at the 2nd try.Created on 2023-08-15 with reprex v2.0.2