How do I use fsolve to find theta2?

49 Views Asked by At

I need to solve the attached equation for "theta2" given the attached values. There should be 56 results because there are 7 different insulation thicknesses(Linsluation/d2/hc), 4 different temperatures (theta1), and 2 e values (em, ec) which must be tested.

Given data:

k = .5  
d1 = 20/12  
Linsulation = [2/12,3/12,4/12,5/12,6/12,7/12,8/12]  
em = .09  
ec = .9  
sigma = .171*10**(-8)  
theta1 = [800, 900, 1000, 1100]  
theta3 = 70  
d2 = []  
for i in range(len(Linsulation)):  
    di = (d1+(2*Linsulation[i]))  
    d2.append(di)  
pi=math.pi  
theta2 = sym.symbols("theta2")  
hc=[]  
for i in range(len(d2)):  
    hi = .270*((theta2-theta3)**(.25))*(d2[i]**(-.25))  
    hc.append(hi)  

[enter image description here](https://i.stack.imgur.com/MgBrp.png)

I tried:

import math  
import sympy as sym  
from sympy import symbols, Eq, solve  
import scipy  
from scipy.optimize import fsolve  
from math import pi  

k = .5  
d1 = 20/12  
Linsulation = [2/12,3/12,4/12,5/12,6/12,7/12,8/12]  
em = .09  
ec = .9  
sigma = .171*10**(-8)  
theta1 = [800, 900, 1000, 1100]  
theta3 = 70  
d2 = []  
for i in range(len(Linsulation)):  
    di = (d1+(2*Linsulation[i]))  
    d2.append(di)  
pi=math.pi  
theta2 = sym.symbols("theta2")  
hc=[]  
for i in range(len(d2)):  
    hi = .270*((theta2-theta3)**(.25))*(d2[i]**(-.25))  
    hc.append(hi)  

def fun(e):  
    for i in range(len(Linsulation)):  
        for j in range(len(theta1)):  
            return (pi)*d2[i]*hc[i]*(theta2-theta3)+(pi)*d2[i]*e*sigma*(((theta2+460)**4)-((theta3+460)**4))-(2*(pi)*k*(theta1[j]-theta2))/ln(d2[i]/d1)  
theta2 = fsolve(fun(em))  
print(theta2)  

I don't understand how fsolve should work in this context. What is the best way I can solve the equation for multiple values and when the variables cannot be separated?

Attempt 2:

import math  
import sympy as sym  
from sympy import symbols, Eq, solve  
import scipy  
from scipy.optimize import fsolve  
from math import pi  

k = .5  
d1 = 20/12  
Linsulation = [2/12,3/12,4/12,5/12,6/12,7/12,8/12]  
em = .09  
ec = .9  
sigma = .171*10**(-8)  
theta1 = [800, 900, 1000, 1100]  
theta3 = 70  
d2 = []  
for i in range(len(Linsulation)):  
    di = (d1+(2*Linsulation[i]))  
    d2.append(di)  
pi=math.pi  
theta2 = sym.symbols("theta2")  
hc=[]  
for i in range(len(d2)):  
    hi = .270*((theta2-theta3)**(.25))*(d2[i]**(-.25))  
    hc.append(hi)  

def fcn(theta2):  
    for i in range(len(Linsulation)):  
        for j in range(len(theta1)):  
            LHS = (pi)*d2[i]*hc[i]*(theta2-theta3)+(pi)*d2[i]*em*sigma*(((theta2+460)**4)-((theta3+460)**4))-(2*(pi)*k*(theta1[j]-theta2))/ln(d2[i]/d1)  
            return LHS
theta2_initial = 300 # Your inital guess  
result = fsolve(fcn, [theta2_initial,])  

Resulted in: error: Result from function call is not a proper array of floats.

1

There are 1 best solutions below

3
user9794 On

If you look at the documentation https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html you see that fsolve has two required arguments. The first is a callable, i.e. a function, or a function handle. The second is an initial "guess" for the variable you want to solve for.

So to use fsolve you would first define a function that will return 0 for the correct value of its input:

def fcn(theta2):
# rewrite your equation as LHS(theta2) = 0
    LHS = # Some expression depending on theta2
    return [LHS,] # fsolve requires input and output to be the same shape.

# Now call fsolve
theta2_initial = # Your inital guess
result = fsolve(fcn, [theta2_initial,]) # Note fsolve expects an array in general as it can solve multivariable equations.

See the documentation page for a complete example.