Search code examples
rformulaequationnon-linear

Is there a way to solve an equation in R?


I'm trying to write an equation in R, and then solve it. I'm fairly new to R, so it's probably a basic question, but I haven't been able to make much sense of the CRAN notes on several packages that come up with a google.

My equation:

F- b ln(|1+ (F/b)|) - 0.05t = 0

I'm trying to solve for F, and have other equations/variables in R that define b and t already.

I guess what I'm asking is, how do I translate this formula into something in R, and go about solving it for F?


Solution

  • Assuming b and t are scalars with known values (here we assume 1 for both) we can minimize the the square of the left hand side assuming the answer lies in the indicated interval and if it achieves zero (which it does below) we have solved it. Note that F means FALSE in R so we used FF for clarity.

    fun <- function(FF, b, t) (FF - b * log(abs(1+ (FF/b))) - 0.05*t)^2
    optimize(fun, c(-10, 10), b = 1, t = 1)
    

    giving:

    $minimum
    [1] 0.3503927
    
    $objective
    [1] 7.525844e-12