Quick & dirty python scripts for finding roots of equations:

The functions are hard-coded in these examples, it’s more for the algorithm implementation…

Bisection Method  code

This piece of code answers 5.12 in Chapra’s Numerical Methods for Engineers.  Useful for finding the max between the integrals [0,1] for the following function: f(x) = -2x^6 – 1.6x^4 +12x +1

Results: 13 iterations to get below error of <5%  ;  final xr: 0.90478

f (x) = −2x^6 − 1.6x^4 + 12x + 1
f (x) = −2x^6 − 1.6x^4 + 12x + 1

Newton-Raphson method code

This answers Chapra’s 6.4.  You have to run this as many times as you think there are roots, which is why it’s a good idea to graph the function first.  I circled my initial guesses on the graph below for the hard-coded example: f(x) = -1 + 5.5x -4x^2 + 0.5x^3

The actual results of the prediction are:

x0:  0   after 4 iterations xroot:  0.21433

x0:  1   after 5 iterations xroot: 1.47976

x0:  6   after 4 iterations xroot: 6.30589


f(x) = -1 + 5.5x -4x^2 + 0.5x^3
f(x) = -1 + 5.5x -4x^2 + 0.5x^3

Comment Here:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s