Finiding a value at which a function is zero


In [1]:
import numpy as np
from scipy.optimize import minimize, minimize_scalar, newton


In [2]:
# assuming currents 
I1 = 1000
I2 = 1000

# bars dimensions
b1 = 25.4e-3
a1 = 0.25*25.4e-3
b2 = 25e-3
a2 = 6e-3

# temperatures
T1 = 84.9
Ta = 50

# temperature resistance change coeff
alpha = 3.9e-3

def f(x):
    return np.sqrt((2*b1 + 2*a1)/(2*b2+2*a2)) * ((T1-Ta)/(x-Ta))**0.61 \
* np.sqrt((a1*b1*(1+alpha*(x-20)))/(a2*b2*(1+alpha*(T1-20)))) - I1/I2

Trying to use minimize_scalar function


In [3]:
result = minimize_scalar(f)

In [4]:
result.success


Out[4]:
True

In [5]:
T2 = result.x
T2


Out[5]:
49.999999988366476

In [6]:
f(T2)


Out[6]:
(-202715.36714150239+563060.81555927359j)

The result above is a really strange result. Most probably some constaints are missing.

Trying to use Newton method


In [7]:
# Newton method used to find zero; x0 is a starting point
res = newton(f, x0=90)

In [8]:
res


Out[8]:
88.077777089701527

In [9]:
f(res)


Out[9]:
0.0

In [10]:
# Excel goal seek result
f(88.035)


Out[10]:
0.00061994046218716292

Trying approach with the function


In [11]:
def findTemperature(I1, I2, a1, b1, a2, b2, T1, Ta, T20):
    """Function used to calculate the temperature of a bar in the new conditions.
    
    Inputs
    ---------------
    I1: float
        Tested current in Amps
    I2: float
        New current value in Amps
    a1: float
        Tested bar thickness in meters
    b1: float
        Tested bar width in meters
    a2: float
        New bar thickness in meters
    b2: float
        New bar width in meters
    T1: float
        Absolute temperature of a tested bar in deg C
    Ta: float
        Ambient temperature in deg C
    T20: float
        initial value of the calculated temperature in deg C
        starting point for the Newton method
    
    Returns
    -----------------
    T2: float
        Absolute temperature of a bar in new conditions in deg C"""
    
    # temperature resistance change coeff
    alpha = 3.9e-3

    def f(x):
        return np.sqrt((2*b1 + 2*a1)/(2*b2+2*a2)) * ((T1-Ta)/(x-Ta))**0.61 \
    * np.sqrt((a1*b1*(1+alpha*(x-20)))/(a2*b2*(1+alpha*(T1-20)))) - I1/I2
    
    T2 = newton(f, x0=T20)
    return T2

In [12]:
T2 = findTemperature(I1=1000, I2=1000, b1 = 25.4e-3, a1 = 0.25*25.4e-3, b2 = 25e-3, a2 = 6e-3, T1=84.9, Ta=50, T20=90)

In [13]:
T2


Out[13]:
88.077777089701527

In [14]:
T22 = findTemperature(I1=1000, I2=800, b1 = 25.4e-3, a1 = 0.25*25.4e-3, b2 = 25e-3, a2 = 6e-3, T1=84.9, Ta=50, T20=90)
T22


Out[14]:
75.574979923285781

In [15]:
T23 = findTemperature(I1=1000, I2=1500, b1 = 25.4e-3, a1 = 0.25*25.4e-3, b2 = 25e-3, a2 = 6e-3, T1=84.9, Ta=50, T20=90)
T23


Out[15]:
132.16715870829645

In [ ]: