Numerical Methods in Python : Bisection Method
This semester, I have Computational Lab as one of my practical subjects. While the subject itself is quite interesting, the programming environment being used in the lab is Turbo C, a DOS based IDE which has been abandoned a long time ago. It doesn't even run natively on modern operating systems and requires a DOS emulator called DOSBox to run. This makes it very irritating to use due to the poor mouse support.
While its true that the academic world can never catch up with the industry, falling behind by such amounts is nothing but a shame. When beginner-friendly yet powerful modern languages such as Python are available, why stick to the ages old Turbo C?
So I've decided to rewrite the programs I do in the lab in Python 2.7. This would serve me in two ways, it would help me understand the algorithm better while also improving my python coding skills. The first one would be the Bisection Method
Bisection Method to find the roots of a non linear equation.
This method is probably the most straight forward method which may not be very efficient but is has got a really simple logic. The idea is that given two limits between which the root is bracketed, we assume that the root lies exactly at the center of the two. Then we go on to check if its really true or not.
If yes, then hurray you've found the root!!!!
If not, then you have to do some logical push-ups. The first push-up involves in finding where the root is actually? Is it between the lower limit and the midpoint? Or is it between the upper limit and the midpoint?
If the former is true, set upper limit to the midpoint value else set the lower limit to midpoint value.
Then the biggest push-up! Repeat all the steps till you get the root up to the desired level of accuracy.
Here's my python code :
While its true that the academic world can never catch up with the industry, falling behind by such amounts is nothing but a shame. When beginner-friendly yet powerful modern languages such as Python are available, why stick to the ages old Turbo C?
So I've decided to rewrite the programs I do in the lab in Python 2.7. This would serve me in two ways, it would help me understand the algorithm better while also improving my python coding skills. The first one would be the Bisection Method
Bisection Method to find the roots of a non linear equation.
This method is probably the most straight forward method which may not be very efficient but is has got a really simple logic. The idea is that given two limits between which the root is bracketed, we assume that the root lies exactly at the center of the two. Then we go on to check if its really true or not.
If yes, then hurray you've found the root!!!!
If not, then you have to do some logical push-ups. The first push-up involves in finding where the root is actually? Is it between the lower limit and the midpoint? Or is it between the upper limit and the midpoint?
If the former is true, set upper limit to the midpoint value else set the lower limit to midpoint value.
Then the biggest push-up! Repeat all the steps till you get the root up to the desired level of accuracy.
Here's my python code :
There's a line of code missing. In your while loop you have:
ReplyDeleteif fm == 0.0: return mid
above this you need to define it:
fm = function(mid)