简体   繁体   English

求解 R 中的一阶线性方程组

[英]Solving first degree linear equations in R

I am new at R and I am trying to do a function to resolve first degree linear equations but I don't know how to start.我是 R 的新手,我正在尝试做一个 function 来解决一阶线性方程,但我不知道如何开始。 For example, given a function like:例如,给定一个 function,如:

7x+4 = 18 or 5x+3=0 7x+4 = 18 或 5x+3=0

What am I supposed to do?我应该做些什么?

You can do solve linear equations (and loads of more complicated symbolic calculation tasks) using the library Ryacas .您可以使用库Ryacas解决线性方程(以及更复杂的符号计算任务的负载)。

Example:例子:

require(Ryacas)

yacas('Solve(7*x + 4 == 18, x)')
Yacas vector:
[1] x == 2

For a more thorough discussion, see https://cran.r-project.org/web/packages/Ryacas0/vignettes/elaborate-reference.html , and https://www.brodrigues.co/blog/2013-12-31-r-cas/ . For a more thorough discussion, see https://cran.r-project.org/web/packages/Ryacas0/vignettes/elaborate-reference.html , and https://www.brodrigues.co/blog/2013-12- 31-r-cas/

Here is a base R solution:这是一个基本的 R 解决方案:

> uniroot(function(x) 7*x + 4 - 18, c(0, 5))$root
[1] 2
> uniroot(function(x) 5*x + 3, c(-3, 0))$root
[1] -0.6
> 

Rewrite your equations, if necessary, in the form f(x) = 0 (as in the first example).如有必要,以f(x) = 0的形式重写您的方程(如第一个示例所示)。 The second parameter gives the range over which to search for a solution.第二个参数给出了搜索解决方案的范围。

Finally I do a base R solution:最后我做了一个基本的 R 解决方案:

sol = function(A,B,C){
    C-B
    -B/A
}

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

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