-1

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?

fabla
  • 1,806
  • 1
  • 8
  • 20
  • Possible duplicate of [Solving simultaneous equations with R](https://stackoverflow.com/questions/8145694/solving-simultaneous-equations-with-r) – Chris Oct 03 '19 at 15:27
  • Function `nls()` can perform a non linear least squares. – Dave2e Oct 03 '19 at 15:27
  • More detail on non-linear here: https://stackoverflow.com/questions/48832731/solving-a-system-of-nonlinear-equations-in-r – Chris Oct 03 '19 at 15:29
  • If you don't get on with `nls` (it is fussy about starting parameters), we've found a more recent one in the package [minpack.lm](https://rdrr.io/cran/minpack.lm/man/nlsLM.html) which has proven to be more robust – Jonny Phelps Oct 03 '19 at 15:29

1 Answers1

4

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
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • That's been really helpful, thanks. However, what's the difference between $minimum and $objective? And can you explain why we're squaring something? If I wanted to then use the solution to this function in an equation, say the solution multiplied by something, how would I go about making the solution a value so that I can do this? Or is there another way of doing it? – rosabella_carter Oct 04 '19 at 12:43
  • That makes much more sense. I'm still not sure how to save/use this value from $objective to the Global Environment values so I can then use it in further equations, however. – rosabella_carter Oct 04 '19 at 14:33
  • `z <- optimize(...); z$minimum` – G. Grothendieck Oct 04 '19 at 15:21