简体   繁体   English

求解一个简单的 (?) 非线性方程组

[英]solving a simple (?) system of nonlinear equations

I'm trying to solve a simple system of non-linear equations described in this post .我正在尝试解决这篇文章中描述的一个简单的非线性方程组。

The system is two equations with two unknowns p and q and a free parameter lambda:该系统是两个方程,有两个未知数 p 和 q 以及一个自由参数 lambda:

在此处输入图像描述

When lambda = 1 the system looks like this:当 lambda = 1 时,系统如下所示:

在此处输入图像描述

There is a unique solution and it's in the vicinity of p = 0.3, q = 0.1.有一个唯一解,它在 p = 0.3, q = 0.1 附近。

I'm trying to solve it with nleqslv .我正在尝试用nleqslv解决它。 My objective function is:我的目标 function 是:

library(nleqslv)

fn = function(x, lambda = 1){ 
  # p = x[1]
  # q = x[2]
  pstar = exp(lambda * (1*x[2])) / (exp(lambda * (1*x[2])) + exp(lambda * (1 - x[2])))
  qstar = exp(lambda * (1 - x[1])) / (exp(lambda * ((1 - x[1]))) + exp(lambda * (9*x[1])))
  return(c(pstar,qstar))
}

but the results don't match what the plot:但结果与 plot 的结果不匹配:

> xstart = c(0.1, 0.3)
> nleqslv(xstart, fn)$x
[1]  1.994155 -8.921285

My first question is: am I using nleqslv correctly?我的第一个问题是:我是否正确使用nleqslv I thought so after looking at other examples.看了其他例子后,我是这么想的。 But now I'm not sure.但现在我不确定。

My second question: is this a good problem nleqslv?我的第二个问题:这是一个很好的问题 nleqslv 吗? Or am I barking up the wrong tree?还是我在叫错树?

Your function does not reflect properly what you want.您的 function 无法正确反映您想要的内容。

You can see this by evaluating fn(c(0.3,0.1)) as follows.您可以通过如下评估fn(c(0.3,0.1))来看到这一点。

fn(c(0.3,0.1))
[1] 0.3100255 0.1192029

So the output is very close to the input.所以 output 非常接近输入。 You wanted (almost) zero as output.你想要(几乎)零作为 output。

So you want to solve the system for p and q .所以你想解决pq的系统。 What you need to do is to make your function return the difference between the input p and the expression for pstar and the difference between the input q and the expression for qstar .您需要做的是让您的 function 返回输入ppstar表达式之间的差异以及输入qqstar表达式之间的差异。

So rewrite your function as follows所以重写你的 function 如下

fn <- function(x, lambda = 1){ 
  p <- x[1]
  q <- x[2]
  pstar <- exp(lambda * (1*x[2])) / (exp(lambda * (1*x[2])) + exp(lambda * (1 -    x[2])))
  qstar <- exp(lambda * (1 - x[1])) / (exp(lambda * ((1 - x[1]))) + exp(lambda * (9*x[1])))
  return(c(pstar-p,qstar-q))
}  

and then call nleqslv as follows (PLEASE always show all the code you are using. You left out the library(nleqslv) ).然后按如下方式调用nleqslv (请始终显示您正在使用的所有代码。您遗漏了library(nleqslv) )。

library(nleqslv)
xstart <- c(0.1, 0.3)
nleqslv(xstart, fn)

This will display the full output of the function.这将显示 function 的完整 output。 Always a good idea to check for succes.检查成功总是一个好主意。 Always check $termcd for succes.始终检查$termcd是否成功。

$x
[1] 0.3127804 0.1064237

$fvec
[1] 5.070055e-11 6.547240e-09

$termcd
[1] 1

$message
[1] "Function criterion near zero"

$scalex
[1] 1 1

$nfcnt
[1] 7

$njcnt
[1] 1

$iter
[1] 7

The result for $x is more what you expect. $x的结果更符合您的预期。

Finally please use <- for assignment.最后请使用<-进行分配。 If you don't there will come the day that you will be bitten by R and its magic.如果您不这样做,那么您将被R及其魔力咬住的那一天。

This is nothing wrong in using nleqslv for this problem.使用nleqslv解决这个问题并没有错。 You only made a small mistake.你只是犯了一个小错误。

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

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