## Main.PythonSolveEquations History

June 21, 2020, at 04:17 AM by 136.36.211.159 -
Deleted lines 108-126:

----

(:html:)
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname

/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js';
})();
</script>
(:htmlend:)
May 04, 2020, at 04:31 PM by 136.36.211.159 -
Changed line 76 from:
{$-2*x+y=-z$}
to:
{$-2x+y=-z$}
May 02, 2020, at 02:02 PM by 136.36.211.159 -
Changed line 15 from:
(:source lang=python:)
to:
(:source lang=python -inline:)
April 17, 2019, at 09:37 PM by 59.10.106.1 -
Changed lines 35-36 from:
!!!!Source Code for Nonlinear Solution
to:
!!!!Source Code for Nonlinear Solution (fsolve)
Changed lines 38-40 from:
from numpy import *
from scipy.optimize import *
to:
import numpy as np
from scipy.optimize import fsolve
Changed lines 45-48 from:

F = empty((3))
F[0] = pow(x,2)+pow(y,2)-20
F[1] = y - pow(x,2)
to:

F = np.empty((3))
F[0] = x**2+y**2-20
F[1] = y - x**2
Changed line 52 from:
zGuess = array([1,1,1])
to:
zGuess = np.array([1,1,1])
Changed lines 57-70 from:
!!!! Symbolic Solutions
to:
!!!!Source Code for Nonlinear Solution (gekko)

(:source lang=python:)
from gekko import GEKKO
m = GEKKO()
x,y,w = [m.Var(1) for i in range(3)]
m.Equations([x**2+y**2==20,\
y-x**2==0,\
w+5-x*y==0])
m.solve(disp=False)
print(x.value,y.value,w.value)
(:sourceend:)

!!!! Symbolic Solution with Sympy
Changed line 72 from:
h = sym.Eq(-2*x+y,-z)sourceend:)
to:
h = sym.Eq(-2*x+y,-z)
Changed lines 81-82 from:
{$z=c_1 - \frac{5}{2} +\frac{5 \sqrt{3}}{2}$}
to:
{$z=-c_1 - \frac{5}{2} +\frac{5 \sqrt{3}}{2}$}
Changed lines 87-89 from:
{$z=c_1 - \frac{5}{2} -\frac{5 \sqrt{3}}{2}$}
to:
{$z=-c_1 - \frac{5}{2} -\frac{5 \sqrt{3}}{2}$}

The same approach applies to linear or nonlinear equations.
Changed lines 79-87 from:
{$-\frac{1}{2}+\frac{\sqrt{3}}{2}$}
to:
{$x=-\frac{1}{2}+\frac{\sqrt{3}}{2}$}
{$y=c_1 - \frac{3 \sqrt{3}}{2} +\frac{3}{2}$}
{$z=c_1 - \frac{5}{2} +\frac{5 \sqrt{3}}{2}$}

and a second solution:

{$x=-\frac{1}{2}-\frac{\sqrt{3}}{2}$}
{$y=c_1 + \frac{3 \sqrt{3}}{2} +\frac{3}{2}$}
{$z=c_1 - \frac{5}{2} -\frac{5 \sqrt{3}}{2}$}
Changed line 79 from:
{$-\frac{1}{2}+\frac{\sqrt(3)}{2}$}
to:
{$-\frac{1}{2}+\frac{\sqrt{3}}{2}$}

!!!! Symbolic Solutions

Sympy is a package for symbolic solutions in Python that can be used to solve systems of equations.

{$2x^2+y+z=1$}
{$x+2y+z=c_1$}
{$-2*x+y=-z$}

(:source lang=python:)
import sympy as sym
sym.init_printing()
x,y,z = sym.symbols('x,y,z')
c1 = sym.Symbol('c1')
f = sym.Eq(2*x**2+y+z,1)
g = sym.Eq(x+2*y+z,c1)
h = sym.Eq(-2*x+y,-z)sourceend:)

sym.solve([f,g,h],(x,y,z))
(:sourceend:)

When solved in an IPython environment such as a Jupyter notebook, the result is displayed as:

{$-\frac{1}{2}+\frac{\sqrt(3)}{2}$}
Changed line 61 from:
The [[https://apmonitor.com|APMonitor Modeling Language]] 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 [[https://apmonitor.com/online/view_pass.php|web browser interface]].
to:
The [[https://apmonitor.com|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 [[https://apmonitor.com/online/view_pass.php|web browser interface]].

The [[https://apmonitor.com|APMonitor Modeling Language]] 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 [[https://apmonitor.com/online/view_pass.php|web browser interface]].
March 07, 2017, at 01:05 PM by 45.56.3.173 -
Changed lines 15-26 from:
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)
to:
(:source lang=python:)
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)
(:sourceend:
)
Changed lines 37-43 from:
from numpy import *
from scipy.optimize import *

def myFunction(z):
x = z[0]
y = z[1]
w = z[2]
to:
(:source lang=python:)
from numpy import *
from scipy.optimize import *

def myFunction(z):
x = z[0]
y = z[1]
w = z[2]
Changed lines 46-54 from:
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)
to:
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)
(:sourceend:
)
Changed line 55 from:
Linear and nonlinear equations can also be solved with [[Main/ExcelSolveEquations|Excel]] and [[Main/PythonSolveEquations|Python]]. Click on the appropriate link for additional information and source code.
to:
Linear and nonlinear equations can also be solved with [[Main/ExcelSolveEquations|Excel]] and [[Main/MatlabSolveEquations|MATLAB]]. Click on the appropriate link for additional information and source code.
Changed line 17 from:
A = np.array([[3,-9],[2,4]])
to:
A = np.array([ [3,-9], [2,4] ])
Changed line 22 from:
M = np.array([[1,-2,-1],[2,2,-1],[-1,-1,2]])
to:
M = np.array([ [1,-2,-1], [2,2,-1], [-1,-1,2] ])
!!!!Source Code for Linear Solutions

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)

Changed line 33 from:
!!!!Source Code
to:
!!!!Source Code for Nonlinear Solution
(:title Solve Equations in Python:)
(:keywords Python, solve equations, linear, nonlinear:)
(:description Python tutorial on solving linear and nonlinear equations with matrix operations (linear) or fsolve NumPy(nonlinear):)

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.

!!!!Solve Linear Equations with Python

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/44pAWI7v5Zk" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

!!!!Solve Nonlinear Equations with Python

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/S4Qg2CsiIj8" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

!!!!Source Code

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)

Linear and nonlinear equations can also be solved with [[Main/ExcelSolveEquations|Excel]] and [[Main/PythonSolveEquations|Python]]. Click on the appropriate link for additional information and source code.

----

(:html:)
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname

/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js';