Minimization of Nonlinear equations

夙愿已清 提交于 2019-12-04 05:43:45

问题


I am relatively new to R, so I apologize if my questions isn't expressed well, or if there is excessive detail. What I'm doing here is taking a naturally occurring gas isotope of C12 and C13 that is produced at a linear rate (P) at respective fractions (F12 and F13) that sums to 1. The two isotopic gases are then consumed at rates k12 for C13 and k13 for C13. I then want to solve for P and k12 using a minimization function.

The equations are:

Eqn 1: conc.12 = ((F12*P)/k12)-(((F12*P)/k12)-c12zero)exp(-k12(t-t0))

Eqn 2: conc.13 = ((F13*P)/k13)-(((F13*P)/k13)-c13zero)exp(-k13(t-t0))

Eqn 3: Sum Square Error = sum(((conc.12-c12meas)/0.07)^2) +sum(((conc.13-c13meas)/0.07)^2)

conc.12 and conc.13 are the estimated concentrations of two isotopes at time t

c12meas and c13meas are the measured concentrations of two isotopes at time t

t0 is the initial time point

F12 and F13 are fractions that sum to 1

k12 and k13 are exponential decay coefficients for the two isotopes, with k13 = k12/1.06

P is a linear production rate of both 12CH4 and 13CH4

The data for a toy data set with known approximate parameters follow:

Time c12meas c13meas

1 109.7000 19.35660
2 118.9150 18.74356
3 127.6693 18.15943
4 135.9858 17.60285
5 143.8865 17.07253 6 151.3922 16.56722
7 158.5226 16.08575
8 165.2964 15.62698
9 171.7316 15.18986
10 177.8450 14.77336
11 183.6528 14.37650
12 189.1701 13.99837
13 194.4116 13.63807
14 199.3911 13.29476
15 204.1215 12.96765
16 208.6154 12.65597
17 212.8847 12.35899
18 216.9404 12.07602
19 220.7934 11.80639
20 224.4537 11.54949

Note that the rows in reality are of equal length and the problem above has to do with pasting them in to the web portal.

  1. I first tried to solve these equations with optim with the following code:

      error.func <- function (k12, P) {  
      t <- Time
      t0 <-Time[1]
      c12zero=c12meas[1]
      c13zero=c13meas[1]
      k13=k12/1.06
      F12=0.98
      F13=1-F12
      ratio.12<- (F12*P)/k12
      exp.12<- exp(-k12*(t-t0))
      conc.12<-ratio.12 - ((ratio.12-c12zero)*exp.12)
      ratio.13<- (F13*P)/k13
      exp.13<- exp(-k13*(t-t0))
      conc.13<- ratio.13 - ((ratio.13-c13zero)*exp.13)
      error <- sum(((conc.12-c12meas)/0.07)^2)
      +sum(((conc.13-c13meas)/0.07)^2)
      return (error)
        }
      fit.model <- optim(k12=.05, P = 15, error.func)
    

This is the error code in R: "Error in optim(k12 = 0.05, P = 15, error.func) : cannot coerce type 'closure' to vector of type 'double' In addition: Warning message: In optim(k12 = 0.05, P = 15, error.func) : one-dimensional optimization by Nelder-Mead is unreliable: use "Brent" or optimize() directly"

My intepretation of this is that the optim function can't solve multiple equations at the same time, so I then tried the solnp function.

  isotopes2<- function(x) {
  t=Time
  t0<-Time[1]
  c12zero=c12meas[1]
  c13zero=c13meas[1]
  k13=x[1]/1.06
  F12=0.98
  F13=1-F12
  ratio.12<- (F12*x[2])/x[1]
  exp.12<- exp(-x[1]*(t-t0))
  conc.12<-ratio.12 - ((ratio.12-c12zero)*exp.12)
  ratio.13<- (F13*x[2])/k13
  exp.13<- exp(-k13*(t-t0))
  conc.13<- ratio.13 - ((ratio.13-c13zero)*exp.13)
    }

  error.func <- function (x) {
  t <- Time
  t0<-Time[1]
  c12zero=c12meas[1]
  c13zero=c13meas[1]
  k13=x[1]/1.06
  F12=0.98
  F13=1-F12
  ratio.12<- (F12*x[2])/x[1]
  exp.12<- exp(-x[1]*(t-t0))
  conc.12<-ratio.12 - ((ratio.12-c12zero)*exp.12)
  ratio.13<- (F13*x[2])/k13
  exp.13<- exp(-k13*(t-t0))
  conc.13<- ratio.13 - ((ratio.13-c13zero)*exp.13)
  error <- sum(((conc.12-c12meas)/0.07)^2)
  +sum(((conc.13-c13meas)/0.07)^2)
  return (error)
    }
  x0 <- c(0.05,15)
  constraint = c(0)
  fit <- solnp (x0, fun = isotopes2, eqfun = error.func, eqB=0)

I received the following error message: "Error: solnp-->error: objective function returns value of length greater than 1!

来源:https://stackoverflow.com/questions/39054806/minimization-of-nonlinear-equations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!