0

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.

eeek82
  • 1
  • 1
  • Where's `fsolve` from? Your tags and inputs don't give a clue. – hpaulj Dec 06 '22 at 16:41
  • Sorry this was my first command: import math import sympy as sym import numpy as np import pandas as pd from sympy import symbols, Eq, solve from math import pi import scipy from scipy.optimize import fsolve – eeek82 Dec 06 '22 at 16:45
  • Put that in the question, not a comment. Why all these other imports like pandas, numpy, and scipy. Focus your question for maximum clarity. – hpaulj Dec 06 '22 at 16:47
  • Why are you trying to mix `sympy` and `scipy`? One is symbolic, the other numeric. They don't play well together. At the very least you should be showing your errors. What is `fun(em)`? Show it! Does it meet the requirements of `fsolve`? Read, and reread the function documentation. Test simpler cases if necessary. – hpaulj Dec 06 '22 at 17:09

1 Answers1

2

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.

user9794
  • 186
  • 4
  • thank you, I now received the error: Result from function call is not a proper array of floats. Am I using the wrong formatting to call the values from my Linsulation and theta1 array? – eeek82 Dec 06 '22 at 17:28
  • As hpaulj was saying it's a bit hard to see what you're trying to do by mixing numeric and symbolic computation. I'm not going to go through your code, but your approach seems to be trying to solve all the problems at once, which is not really how `fsolve` works. Break it into smaller problems. Try to solve it for just one value of all the other variables instead of 56 at once. Then iterate over the different combinations to get the value for each configuration. – user9794 Dec 06 '22 at 17:36
  • Also the return value of fnc should be an array of the same shape as the initial guess, so return [LHS,] instead of just LHS. – user9794 Dec 06 '22 at 17:39