简体   繁体   English

是否可以求解非线性方程 R?

[英]It is possible to solve equation R that are not linear?

I want to build a function that takes E[x] and Var[X] and give me the mean and standard error of a univariate lognormal variable.我想构建一个接受 E[x] 和 Var[X] 的函数,并给出单变量对数正态变量的均值和标准误差。

E[x] = exp(mu + theta) 
Var[x] =  exp(2*mu + theta)*(exp(theta) - 1)

The function would take E[x] and Var[x] as input and as output would give me theta and mu该函数将E[x]Var[x]作为输入,作为输出将给我thetamu

There are several packages that provide ways and means to solve a system of nonlinear equations.有几个软件包提供了求解非线性方程组的方法和方法。 One of these is nleqslv .其中之一是nleqslv

You nee to provide a function that function that returns the differences between the actual value of the equations and the desired value.您需要提供一个函数,该函数返回方程的实际值与所需值之间的差异。

Load package nleqslv and define the following function加载包nleqslv并定义如下函数

library(nleqslv)

f <- function(x,Ex,Varx) {
    y<- numeric(length(x))    
    mu <- x[1]
    theta <- x[2]
    y[1] <- exp(mu+theta) - Ex
    y[2] <- exp(2*mu+theta)*(exp(theta)-1) - Varx

    y
}

The vector x in the function contains the values of mu and theta .函数中的向量x包含mutheta的值。 An example with Ex=2 and Varx=3 and some random starting values Ex=2Varx=3以及一些随机起始值的示例

xstart <- c(1,1)
nleqslv(xstart,f,Ex=2,Varx=3)

gives the following给出以下

$x
[1] -0.6931472  1.3862944

$fvec
[1] -8.095125e-11 -8.111645e-11

$termcd
[1] 1

$message
[1] "Function criterion near zero"

$scalex
[1] 1 1

$nfcnt
[1] 31

$njcnt
[1] 2

$iter
[1] 22

See the manual of nleqslv for the meaning of the different elements of the return value of nleqslv .见手册nleqslv为返回值的不同元素的含义nleqslv

If you want to investigate the effect of the different solving methods try this如果你想研究不同求解方法的效果,试试这个

testnslv(xstart,f,Ex=2,Varx=3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM