简体   繁体   English

给定一个 x 和多项式方程,有没有办法使用 r 获得 y 值?

[英]Given an x and polynomial equation, is there way to get the y value using r?

If I have a equation like 10 + x^2 + x^3 + x^4 = y and an x value like 2. Is there way to plug this into r so it would solve for y?如果我有一个像10 + x^2 + x^3 + x^4 = y这样的等式和像 2 这样的 x 值。有没有办法将它插入 r 以便它可以解决 y? It sounds trivial but eventually I would like to solve for x using polynomials that higher degrees like 30. Anyone know of a possible way to do this in r but without plugging in the x value manually?这听起来微不足道,但最终我想使用诸如 30 的更高次数的多项式来求解 x。任何人都知道在 r 中执行此操作但无需手动插入 x 值的可能方法吗?

Please note: I'm trying to solve for y given a specific x value.请注意:我正在尝试解决给定特定 x 值的 y 问题。

You can easily write your own function:您可以轻松编写自己的 function:

p <- function(x, coefs) c(cbind(1, poly(x, degree = length(coefs) - 1, 
                            raw = TRUE, simple = TRUE)) %*% coefs)
p(2, c(10, 0, 1, 1, 1))
#[1] 38

Use rep if you need many coefficients of 1.如果您需要许多 1 的系数,请使用rep

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

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