How to solve non-linear equations using python

Posted by stars83clouds on Stack Overflow See other posts from Stack Overflow or by stars83clouds
Published on 2014-08-21T06:44:10Z Indexed on 2014/08/21 10:20 UTC
Read the original article Hit count: 425

Filed under:
|
|

I have the following code:

#!/usr/bin/env python
from scipy.optimize import fsolve
import math

h = 6.634e-27
k = 1.38e-16

freq1 = 88633.9360e6
freq2 = 88631.8473e6
freq3 = 88630.4157e6

def J(freq,T):
     return (h*freq/k)/(math.exp(h*freq/(k*T))-1)

def equations(x,y,z,w,a,b,c,d):
     f1 = a*(J(freq1,y)-J(freq1,2.73))*(1-math.exp(-a*z))-(J(freq2,x)-J(freq2,2.73))*(1-math.exp(-z))
     f2 = b*(J(freq3,w)-J(freq3,2.73))*(1-math.exp(-b*z))-(J(freq2,x)-J(freq2,2.73))*(1-math.exp(-z))
     f3 = c*(J(freq3,w)-J(freq3,2.73))*(1-math.exp(-b*z))-(J(freq1,y)-J(freq1,2.73))*(1-math.exp(-a*z))
     f4 = d*(J((freq3+freq1)/2,(y+w)/2)-J((freq3+freq1)/2,2.73))-(J(freq2,x)-J(freq2,2.73))*(1-math.exp(-z))
     return (f1,f2,f3,f4)

So, I have defined the equations in the above code. However, I now wish to solve the above set of equations using fsolve or other alternative non-linear numerical routine. I tried the following syntax but with no avail:

  x,y,z,w = fsolve(equations, (1,1,1,1))

I keep getting the error that "x" is not defined. I am executing all commands at the command-line, since I have no idea how to run a batch of commands as above automatically in python. I welcome any advice on how to solve this.

© Stack Overflow or respective owner

Related posts about python

Related posts about python-2.7