0

I have this equation :

f(x) = i * ln(j * x + k)

With these two conditions : f(0) = 6 & f(1) = 12

After several hours of research, I can not find how to optimize the parameters i, j & k which respect the conditions with RStudio.

I know how to do it with Excel, but I want to succeed in doing it with R.

Anyone have any idea to fix this problem with R?

Paul
  • 8,734
  • 1
  • 26
  • 36

3 Answers3

0

i can help you with the monte carlo method so : after math calcul you find :

i=log(k)/6

k=exp(72*log(j+k))

so you apply the monte carlo method :

a=data.frame(k=round(runif(1000000,-2,2),4),j=round(runif(1000000,-2,2),4))
a$k2=round(exp(72*log(a$j+a$k)),4)
a=a[-which(is.na(a$k2)==TRUE),]   # you delete the NA coz of negatif number in log
library(tidyverse) # to use "near" function
a[which(near(a$k,a$k2,0.001)==TRUE),] 
0

optim

Define f as the function in the question except we explicitly list all arguments and ss as the residual sum of squares. Then minimize ss using an arbitrary value for i (since we have two equations and 3 unknowns). Below we show the solution for j and k (in the par component of the output) using i = 10 as an input.

f <- function(x, i, j, k) i * log(j * x + k)
ss <- function(p, i) (f(x = 0, i = i, j = p[1], k = p[2]) - 6)^2 + 
  (f(x = 1, i = i, j= p[1], k = p[2]) - 12)^2
optim(1:2, ss, i = 10)

giving:

$par
[1] 1.497972 1.822113

$value
[1] 9.894421e-09

$counts
function gradient 
      59       NA 

$convergence
[1] 0

$message
NULL

nlsLM

Alternately we can use nonlinear least squares. This is slightly easier to specify since we don't need to define ss but it does require a package. We use nlsLM instead of nls in the core of R since nls does not handle zero residual problems well.

library(minpack.lm)

nlsLM(y ~ f(x, i, j, k), data = list(y = c(6, 12), x = 0:1, i = 10), 
        start = c(j = 1, k = 2))

giving:

Nonlinear regression model
  model: y ~ f(x, i, j, k)
   data: list(y = c(6, 12), x = 0:1, i = 10)
   j    k 
1.50 1.82 
 residual sum-of-squares: 0

Number of iterations to convergence: 4 
Achieved convergence tolerance: 1.49e-08
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
0

Given the constraints you can solve for i and j in terms of k:

f(0) = 6 
=> i*ln( j*0 + k) = 6
=> i*ln(k) = 6 
=> i = 6/ln(k)
f(1) = 12
=> i*ln( j*1 + k) = 12
=> (6/ln(k))*ln(j+k) = 12
=> ln(j+k) = 2*ln(k)
=> j+k = k*k
=> j = k*k-k

So

f(x) = (6/ln(k))*ln( (k*(k-1)*x + k)

As a check

f(0) = (6/ln(k))*ln( (k*(k-1)*0 + k)
     = (6/ln(k))*ln(k) = 6
f(1) = (6/ln(k))*ln( (k*(k-1)*1 + k)
     = (6/ln(k))*ln( k*k)
     = (6/ln(k))*2*ln(k)
     = 12

However I do not understand what you want to optimize.

dmuir
  • 4,211
  • 2
  • 14
  • 12