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. For example, given a function like:

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

What am I supposed to do?

Upvotes: 0

Views: 241

Answers (3)

Finally I do a base R solution:

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

Upvotes: 0

Limey
Limey

Reputation: 12585

Here is a base R solution:

> 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). The second parameter gives the range over which to search for a solution.

Upvotes: 1

Otto Kässi
Otto Kässi

Reputation: 3093

You can do solve linear equations (and loads of more complicated symbolic calculation tasks) using the library 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/.

Upvotes: 1

Related Questions