简体   繁体   English

在R中的线性和非线性方程的交点处求x值

[英]Finding x-value at intersection between a linear and nonlinear equation in R

I have two functions: one for a line (y) and another for a curve (hnc). 我有两个函数:一个用于行(y),另一个用于曲线(hnc)。 I would like to determine the one x-value at which the two functions intersect 我想确定两个函数相交的一个x值

sigma = 0.075
mu = 0 
r=0.226 
theta=0.908 
H=0.16 

hnc <- function(x) (1/(sigma*sqrt(2*pi)))*(exp(-(x^2)/(2*(sigma^2))))
y <- function(x) 2*pi*x+(pi*r^2/((360/theta)/H))

curve(hnc,0,r,n=100,col="blue")
plot(y,0,r,add=T,col="red")

I have tried using the nleqslv package, but this results in two separate x-values that do not agree (perhaps because I am using it incorrectly) 我尝试过使用nleqslv软件包,但是这会产生两个不同意的x值(也许是因为我使用的不正确)

int <- function(x){
z <- numeric(2) 
z[1] <- (1/(sigma*sqrt(2*pi)))*(exp(-(x[1]^2)/(2*(sigma^2))))
z[2] <- 2*pi*x[2]+(pi*r^2/((360/theta)/H))
z}

nleqslv(c(0.14,0.14),int,method="Broyden")

Any help would be much appreciated! 任何帮助将非常感激!

Thanks, Eric 谢谢,埃里克

Using optimize here to find the minimum of a function if a single variable seems to work well 如果单个变量似乎运行良好,则使用optimize here来查找函数的最小值

xx <- optimize(function(x) abs(hnc(x)-y(x)), c(.10,.20))$minimum
abline(v=xx, lty=2)

在此输入图像描述

You are not using nleqslv in the correct way. 您没有以正确的方式使用nleqslv It is meant for solving a system of non linear equations with as many variables as there are equations. 它用于求解具有与方程一样多的变量的非线性方程组。

You have two functions and you want to determine the intersection which in your case consists of a single value for x . 您有两个函数,并且您希望确定在您的情况下由x的单个值组成的交集。

You need to define a new function like this 你需要定义一个这样的新函数

g <- function(x) hnc(x) - y(x)

Then you can use uniroot to find a zero of g(x) like this: 然后你可以使用uniroot找到g(x)的零,如下所示:

uniroot(g,c(0,1))

The root found will be 0.1417802 which corresponds with the graph in the first answer. 找到的根将是0.1417802 ,这与第一个答案中的图表相对应。

Minimizing won't always work to find a point of intersection; 最小化并不总是能找到交叉点; if there is no point of intersection you will get misleading results. 如果没有交叉点,你会得到误导性的结果。

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

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