Given a function f(x) on floating number x and two numbers ‘a’ and ‘b’ such that f(a)*f(b) < 0 and f(x) is continuous in [a, b]. Here f(x) represents algebraic or transcendental equation. Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0).
Input: A function of x, for example x3 – x2 + 2. And two values: a = -200 and b = 300 such that f(a)*f(b) < 0, i.e., f(a) and f(b) have opposite signs. Output: The value of root is : -1.00 OR any other value close to root.
We strongly recommend to refer below post as a prerequisite of this post.
Solution of Algebraic and Transcendental Equations | Set 1 (The Bisection Method)
In this post The Method Of False Position is discussed. This method is also known as Regula Falsi or The Method of Chords.
Similarities with Bisection Method:
- Same Assumptions: This method also assumes that function is continuous in [a, b] and given two numbers 'a' and 'b' are such that f(a) * f(b) < 0.
- Always Converges: like Bisection, it always converges, usually considerably faster than Bisection--but sometimes very much more slowly than Bisection.
Differences with Bisection Method:
It differs in the fact that we make a chord joining the two points [a, f(a)] and [b, f(b)]. We consider the point at which the chord touches the x axis and named it as c.
- Write equation of the line connecting the two points.
y – f(a) = ( (f(b)-f(a))/(b-a) )*(x-a) Now we have to find the point which touches x axis. For that we put y = 0. so x = a - (f(a)/(f(b)-f(a))) * (b-a) x = (a*f(b) - b*f(a)) / (f(b)-f(a)) This will be our c that is c = x.
- If f(c) == 0, then c is the root of the solution.
- Else f(c) != 0
- If value f(a)*f(c) < 0 then root lies between a and c. So we recur for a and c
- Else If f(b)*f(c) < 0 then root lies between b and c. So we recur b and c.
- Else given function doesn't follow one of assumptions.
Since root may be a floating point number and may converge very slow in worst case, we iterate for a very large number of times such that the answer becomes closer to the root.
Following is the implementation.
The value of root is : -1
This method always converges, usually considerably faster than Bisection. But worst case can be very slow.
We will soon be discussing other methods to solve algebraic and transcendental equations.
This article is contributed by Abhiraj Smit. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Samsung Interview Experience | Set 22 (On-Campus)
- Samsung Interview Experience | Set 8 (On-Campus for Samsung Research Institute, Noida)
- TCS Ninja Interview Experience (CSE) 2018 (In-Details)
- GE Appliances Interview Experience (On-campus)
- Amazon Interview Experience SDE 2
- Oracle Finance Interview Experience (On-Campus)
- UHG(United Health Group) Interview Experience (Internship)
- Ola Interview Experience
- Aasaanjobs interview experience (On-Campus)
- Probotiq Solution Interview (On Campus) - Internship
- Deloitte Interview Experience (On-Campus)
- Accolite internship interview experience off campus
- Amazon Interview Experience for Spring Internship ( On-Campus)
- Raja Software Labs Interview Experience | Set 2
- OLA Interview Experience (Off-Campus)
Improved By : Sam007