Wikipedia

Search results

17 October 2019

OSX broken pip and virtualenv after brew update

Working on a new project and needed to download some new brew packages that ended up breaking my virtualenv.

This is how I got it back to speed:


xcode-select --install
rm '/usr/local/bin/pip'
# or 
# mv /usr/local/bin/pip /usr/local/bin/pip.old

sudo /usr/bin/easy_install pip
sudo pip install virtualenv --upgrade

06 October 2019

Newton Raphson method to solve roots of higher-degree equations

Roots of High-Degree Equations

Equations can be polynomials, radicals, trigonometric, logarithmic, and other.

Simplest example is the quadratic function. More complex equations will often require numerical methods to solve.


Newton Raphson Method algorithm:

- similar to Simple Iteration except it handles the equation as a function, not a rearrangement
- first step is to put the equation as a function
- second step is to calculate the first derivative of the function
- third step is to iterate as the simple iteration method

This method is an optimization of the simple iteration method.


for example:

'''
for equation:
x^3 - x^2 + 7 = 10
x^3 - x^2 - 3 = 0

derivative
3x^2 - 2x = 0

Newton Raphson equation:

y = x - (x^3 - x^2 - 3) / (3x^2 - 2x)

'''

x = 2 # initial guess, derive multiple roots with different guesses
for i in range(100):
y = x - (x^3 - x^2 - 3) / (3x^2 - 2x)
if x == y: # or within tolerable range
break
x = y

# y now approximates the value of the root





Simple iteration method to solve roots of higher-degree equations

Equations of higher-degree equations can be polynomials, radicals, trigonometric, logarithmic, and other.

Simplest example is the quadratic function. More complex equations will often require numerical methods to solve.


Simple Iterations Method Algorithm:

- based on trial and error, values are substituted until root is obtained
- first step is to REARRANGE the equation to isolate the variable to the left side of the equation
- second step is to ASSUME an initial trial value for the first iteration
- third step is to SUBSTITUTE the value in the equation and solve
- fourth step is to REPLACE the value if the value does not solve the equation else BREAK
- fifth step is to REPEAT the third and fourth steps with the NEW VALUE of the equation


'''
for equation:
x^3 - x^2 + 7 = 10
x(x^2 - x) = 3
x = 3 / (x^2 - x)
'''

def tolerable_range(x, y, tolerance=0.001):
  pass
  # can be difference: abs(x, y) < tolerance
  # can be analysis of convergence 
  #(i.e. difference from previous value is nominal)

def check_divergence(x, y):
  pass
  # if divergence exists, solution cannot be solved
  # example implementation:
  # https://gist.github.com/zhiyzuo/f80e2b1cfb493a5711330d271a228a3d

x = 2 # initial guess
for i in range(100):
y = 3 / (x^2 - x) # new value
if x == y: # or within tolerable range
break
x = y

# y should now approximates the value of the root




Numerical Computing Categories and Methods

Roots of High Degree Equations
* Simple iteration method
* Newton-Raphson's method
* Bisection method

Interpolation and Curve Fitting
* Lagrange's method
* Newton's method
* Linear regression (fitting with straight line)
* Fitting with polynomial curve

Numerical Differentiation
* Finite differences method

Numerical Integration
* Trapezoidal rule
* Simpson's 1/3 rule
* Simpson's 3/8 rule
* Double integrations

Systems of Linear Equations
* Gauss elimination method
* Jacobi's method
* Gauss-Seidel's model

Ordinary Differential Equations
* Euler's method
* Second order Runge-Kutta's method
* Fourth order Runge-Kutta's method
* Higher-order ordinary differential equations