Hi r-users,
I have the following code to solve 4 simultaneous eqns with 4 unknowns using
newton iteration method. But I got the error message:
pars <- c(1.15, 40, 50, 0.78)
newton.input2 <- function(pars)
{ ## parameters to estimate
alp <- pars[1]
b1 <- pars[2]
b2 <- pars[3]
rho <- pars[4]
f1 <- pars[1]*pars[2]
f2 <- pars[1]*(pars[1]+1)*pars[2]^2
f3 <- pars[1]*(pars[1]+1)*pars[3]^2
f4 <- pars[1]*pars[2]*pars[3](pars[1]+pars[4])
fval <- c(f1,f2,f3,f4)
## jacobian matrix
j11 <- pars[2]
j12 <- pars[1]
j13 <- 0
j14 <- 0
j21 <- (2*pars[1]+1)*pars[2]^2
j22 <- 2*pars[2]*pars[1]*(pars[1]+1)
j23 <- 0
j24 <- 0
j31 <- (2*pars[1]+1)*pars[3]^2
j32 <- 2*pars[3]*pars[1]*(pars[1]+1)
j33 <- 0
j34 <- 0
j41 <- pars[2]*pars[3]*(2*pars[1]+pars[4])
j42 <- pars[3]*(pars[3]^2+pars[3]*pars[4])
j43 <- pars[2]*(pars[3]^2+pars[3]*pars[4])
j44 <- pars[3]*pars[2]*pars[3]
jacobian <-
matrix(c(j11,j12,j13,j14,j21,j22,j23,j24,j31,j32,j33,j34,j41,j42,j43,j44),byrow=TRUE,ncol=4)
## output
list(fval=fval,jacobian=jacobian)
}
newton.input2(pars)
newt(newton.input2, c(1,20,30,0.5))
Note: I use 'newt' code is develope by Rolf Turner.
Thank you.
Error message:
> newton.input2(pars)
Error in newton.input2(pars) : attempt to apply
non-function> newt(newton.input2, c(1,20,30,0.5))
Error in fn(p.o, ...) : attempt to apply non-function
[[alternative HTML version deleted]]
When I look at this line:
f4 <- pars[1]*pars[2]*pars[3](pars[1]+pars[4])
It appears to my wet-brain interpreter that you are trying to apply a
function par[3] to the argument (pars[1]+pars[4])
When I insert a "*", I then get output but I don't know if it is
correct.
> newton.input2(pars)
$fval
[1] 46.00 3956.00 6181.25 4439.00
$jacobian
[,1] [,2] [,3] [,4]
[1,] 40 1.15 0 0e+00
[2,] 5280 197.80 0 0e+00
[3,] 8250 247.25 0 0e+00
[4,] 6160 126950.00 101560 1e+05
--
David
On Jan 20, 2010, at 4:58 AM, Roslina Zakaria wrote:
> Hi r-users,
>
> I have the following code to solve 4 simultaneous eqns with 4
> unknowns using newton iteration method. But I got the error message:
>
> pars <- c(1.15, 40, 50, 0.78)
> newton.input2 <- function(pars)
> { ## parameters to estimate
>
> alp <- pars[1]
> b1 <- pars[2]
> b2 <- pars[3]
> rho <- pars[4]
>
> f1 <- pars[1]*pars[2]
> f2 <- pars[1]*(pars[1]+1)*pars[2]^2
> f3 <- pars[1]*(pars[1]+1)*pars[3]^2
> f4 <- pars[1]*pars[2]*pars[3](pars[1]+pars[4])
> fval <- c(f1,f2,f3,f4)
> ## jacobian matrix
> j11 <- pars[2]
> j12 <- pars[1]
> j13 <- 0
> j14 <- 0
> j21 <- (2*pars[1]+1)*pars[2]^2
> j22 <- 2*pars[2]*pars[1]*(pars[1]+1)
> j23 <- 0
> j24 <- 0
> j31 <- (2*pars[1]+1)*pars[3]^2
> j32 <- 2*pars[3]*pars[1]*(pars[1]+1)
> j33 <- 0
> j34 <- 0
> j41 <- pars[2]*pars[3]*(2*pars[1]+pars[4])
> j42 <- pars[3]*(pars[3]^2+pars[3]*pars[4])
> j43 <- pars[2]*(pars[3]^2+pars[3]*pars[4])
> j44 <- pars[3]*pars[2]*pars[3]
> jacobian <-
> matrix
> (c
> (j11
> ,j12
> ,j13
> ,j14
> ,j21,j22,j23,j24,j31,j32,j33,j34,j41,j42,j43,j44),byrow=TRUE,ncol=4)
> ## output
> list(fval=fval,jacobian=jacobian)
> }
> newton.input2(pars)
> newt(newton.input2, c(1,20,30,0.5))
>
> Note: I use 'newt' code is develope by Rolf Turner.
>
> Thank you.
>
>
> Error message:
>
>> newton.input2(pars)
> Error in newton.input2(pars) : attempt to apply non-function
>> newt(newton.input2, c(1,20,30,0.5))
> Error in fn(p.o, ...) : attempt to apply non-function
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
Roslina Zakaria wrote:> > I have the following code to solve 4 simultaneous eqns with 4 unknowns > using newton iteration method.? But I got the error message: > ? > pars <- c(1.15, 40, 50, 0.78) > newton.input2 <- function(pars) > {? ## parameters to estimate > ....... > ? > f1 <- pars[1]*pars[2] > f2 <- pars[1]*(pars[1]+1)*pars[2]^2 > f3 <- pars[1]*(pars[1]+1)*pars[3]^2 > f4 <- pars[1]*pars[2]*pars[3](pars[1]+pars[4]) > fval? <- c(f1,f2,f3,f4) > ## jacobian matrix > .... > list(fval=fval,jacobian=jacobian) > } > newton.input2(pars) > newt(newton.input2, c(1,20,30,0.5)) > ? > Note: I use 'newt' code is develope by Rolf Turner. > Error message: > ? >> newton.input2(pars) > Error in newton.input2(pars) : attempt to apply non-function >> newt(newton.input2, c(1,20,30,0.5)) > Error in fn(p.o, ...) : attempt to apply non-function >I agree with David. The formula for f4 is surely incorrect. Furthermore: 1. what is the newt package you mention. I can't find it on CRAN. 2. A solution of your system is pars = [ 0, anything, anything, anything ] (if pars[1] == 0 then all f-values will also be 0 regardless of values for pars[2:4]) There are more solutions to your system (try pars = c(-1 , 0, 0, 0)). 3. The jacobian you generate is incorrect 4. I tried to solve your system with the correction mentioned above with my package nleqlsv. I have to change the return value of your function newton.input2 to only return fval. Using Broyden and a numerical jacobian. As far as I can see, this confirms the point I made in item 2. pars[1] = 0 with arbitrary values in pars[2], pars[3], pars[4] is a solution. Berend -- View this message in context: http://n4.nabble.com/Error-meaning-tp1018287p1018468.html Sent from the R help mailing list archive at Nabble.com.