The following tutorials are an introduction to solving linear and nonlinear equations with Python. The solution to linear equations is through matrix operations while sets of nonlinear equations require a solver to numerically find a solution.

import numpy as np

A = np.array([ [3,-9], [2,4] ])

b = np.array([-42,2])

z = np.linalg.solve(A,b)

print(z)

M = np.array([ [1,-2,-1], [2,2,-1], [-1,-1,2] ])

c = np.array([6,1,1])

y = np.linalg.solve(M,c)

print(y)

A = np.array([ [3,-9], [2,4] ])

b = np.array([-42,2])

z = np.linalg.solve(A,b)

print(z)

M = np.array([ [1,-2,-1], [2,2,-1], [-1,-1,2] ])

c = np.array([6,1,1])

y = np.linalg.solve(M,c)

print(y)

from numpy import *

from scipy.optimize import *

def myFunction(z):

x = z[0]

y = z[1]

w = z[2]

F = empty((3))

F[0] = pow(x,2)+pow(y,2)-20

F[1] = y - pow(x,2)

F[2] = w + 5 - x*y

return F

zGuess = array([1,1,1])

z = fsolve(myFunction,zGuess)

print(z)

from scipy.optimize import *

def myFunction(z):

x = z[0]

y = z[1]

w = z[2]

F = empty((3))

F[0] = pow(x,2)+pow(y,2)-20

F[1] = y - pow(x,2)

F[2] = w + 5 - x*y

return F

zGuess = array([1,1,1])

z = fsolve(myFunction,zGuess)

print(z)

Linear and nonlinear equations can also be solved with Excel and MATLAB. Click on the appropriate link for additional information and source code.

The APMonitor Modeling Language with a Python interface is optimization software for mixed-integer and differential algebraic equations. It is coupled with large-scale solvers for linear, quadratic, nonlinear, and mixed integer programming (LP, QP, NLP, MILP, MINLP). Modes of operation include data reconciliation, real-time optimization, dynamic simulation, and nonlinear predictive control. It is freely available through MATLAB, Python, Julia, or from a web browser interface.

comments powered by Disqus

Retrieved from http://apmonitor.com/che263/index.php/Main/PythonSolveEquations

Page last modified on September 30, 2017, at 12:40 PM