Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
root of a real number (bisection)
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
# Square root of a real number - Bisection root <- function(number,tol = 0.00001){ bis <- function(f,a=0,b=number,tol){ while( (b-a)/2 > tol){ fa = f(a) fb = f(b) pm = (a+b)/2 fpm = f(pm) # Some especial cases if(f(b) == 0){ return(b) break; } if(fpm == 0){ return(pm) break; } if(fpm*fb < 0){ a <- pm }else{ b <- pm pm <- (a+b)/2 } } cat("Root: ", pm) curve(f, pm - 10, pm + 10) abline(h=0,v=0) points(pm,0, pch = 19, col = 'red') } f <- function(x){ x^2 - number } return(bis(f,0,number,tol)) } root(3) sqrt(3)
[
+
]
Show input
Absolute running time: 0.62 sec, cpu time: 0.92 sec, memory peak: 44 Mb, absolute service time: 0,75 sec
edit mode
|
history
|
discussion
Root: 1.732046[1] 1.732051