2

I have two simultaneous equations I'd like to solve using r. Except the one of the equations has a natural log in it:

Equation 1: U(x; y; z) = 5 ln x + 8 ln y + 12 ln z (Will get maximized, so set to 0)

Equation 2: 10x + 15y + 30z = 3000

Following this previous post: Solving simultaneous equations with R

I'm trying:

 a <- rbind(c(5*log, 2*log, 3*log), 
       c(10, 15, 30))
 b <- c(0, 3000)
 solve(a, b)

I get an non-numeric argument error, since I have nothing plugged in for x in log function.

steppermotor
  • 701
  • 6
  • 22
  • The previous post that you referred to had _three_ equations in three unknowns, so there was a single solution. You have two equations in three unknowns. This could have zero, one or infinitely many solutions. – G5W Nov 14 '17 at 18:39
  • The `non-numeric argument` error is because you are multiplying a number (e.g., 5) by the *function* `log`. Of what value(s) are you meaning to take the logarithm? – r2evans Nov 14 '17 at 18:45
  • I don't understand your statement: `Will get maximized, so set to 0`. Why would the maximum of `U` be 0 subject to the linear restriction. Is this perhaps a problem of maximizing utility subject to a budget constraint? – Bhas Nov 15 '17 at 07:40

1 Answers1

1

The solve function you are trying to use is only for linear coupled equations. If you want to solve non linear coupled equations you need to use nleqslv library.

Then G5W is right, your system is lacking one relation.

I give you an example of solving coupled equations with a logarithm and a system with solutions :

library(nleqslv)

dslnex <- function(x) {
  y <- c(0,30)
  y[1] <- 5*log(x[1]) - x[2]
  y[2] <- 10*x[1] + 15*x[2] 
  y
}
xstart <- c(0.5,1)
fstart <- dslnex(xstart)
nleqslv(xstart,dslnex)

gives

$x
[1]  0.8883046 -0.5922030

as a solution. If you want to try different resolution methods:

> z <- testnslv(xstart,dslnex)
> print(z)
Call:
testnslv(x = xstart, fn = dslnex)

Results:
    Method Global termcd Fcnt Jcnt Iter Message     Fnorm
1   Newton  cline      1    4    4    4   Fcrit 8.106e-20
2   Newton  qline      1    4    4    4   Fcrit 8.106e-20
3   Newton  gline      1    4    4    4   Fcrit 8.106e-20
4   Newton pwldog      1    4    4    4   Fcrit 8.106e-20
5   Newton dbldog      1    4    4    4   Fcrit 8.106e-20
6   Newton   hook      1    4    4    4   Fcrit 8.106e-20
7   Newton   none      1    4    4    4   Fcrit 8.106e-20
8  Broyden  cline      1    6    1    6   Fcrit 2.473e-24
9  Broyden  qline      1    6    1    6   Fcrit 2.473e-24
10 Broyden  gline      1    6    1    6   Fcrit 2.473e-24
11 Broyden pwldog      1    6    1    6   Fcrit 2.473e-24
12 Broyden dbldog      1    6    1    6   Fcrit 2.473e-24
13 Broyden   hook      1    6    1    6   Fcrit 2.473e-24
14 Broyden   none      1    6    1    6   Fcrit 2.473e-24

gives you the method, a message to tell if it has converged or not, and the distance to the solution Fnorm

denis
  • 5,580
  • 1
  • 13
  • 40