Calculate the Root of a Equation within an interval in R Programming – uniroot() Function

uniroot() function in R Language is used to calculate the root of an equation with the lower and upper range of the interval passed as arguments.

Syntax: uniroot(fun, interval, lower, upper)

Parameters:
fun: function with the equation
interval: with upper and lower range of root
lower: lower end point of the interval
upper: upper end point of the interval

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to calculate root of an equation
  
# Function with equation
fun <- function(x) {2 * x ^ 2 - 4 * x -10}
  
# Calling uniroot() function
uniroot(fun, lower = 0, upper = 4)

chevron_right


Output:

$root
[1] 3.449485

$f.root
[1] -4.310493e-05

$iter
[1] 5

$init.it
[1] NA

$estim.prec
[1] 6.103516e-05

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to calculate root of an equation
  
# Function with equation
fun <- function(x) {2 * x ^ 2 - 4 * x -10}
  
# Calling uniroot() function
uniroot(fun, c(0, 4))$root
uniroot(fun, lower = -4, upper = 0)$root

chevron_right


Output:

[1] 3.449485
[1] -1.44949
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.