Python Forum
Matching two variables within a certain toleration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matching two variables within a certain toleration
#1
Hi all,

I am new to Python and currently working on a thesis project. I have come across an issue regarding some simple calculations. I have the following script:

#isentripic relationships
gamma=1.4
patm=23908.7784729
tatm=218.923952814
rho=0.379597
R=287
a=296.5355
Mf=.85
MFCR=0.7
P0=(1+((gamma-1)/2)*(Mf**2))**(gamma/(gamma-1))*patm
T0=(1+((gamma-1)/2)*(Mf**2))*tatm
#print(T0)
pg=P0-patm
#print (pg)

#determining Ah
import math
rhi=0.663
Ahi= math.pi*rhi**2
print(Ahi)
Ainf=MFCR*Ahi
minf=rho*Ainf*Mf*(gamma*R*tatm)**0.5
print(minf)
p_engine_outlet=P0-patm
np.isclose ([minf, m_fan_face]), (1e-09])
m_fan_face = rho*Ahi*M_fan_face*(gamma*R*tatm)**0.5
for M_fan_face in range(0.5,0.7,0.01):
    
    
What I want to achieve with my code is to match the variable minf and m_fan_face. I want to tell Python to match these two within a tolerance of 1e-09. In obtaining m_fan_face I need to iterate for the variable M_fan_face and substitute this in the equation :m_fan_face = rho*Ahi*M_fan_face*(gamma*R*tatm)**0.5. However, I don't know how to do this process. I have tried doing it in the last three lines of my code. I would be pleased if someone could help me with this. I am using spyder (Python 2.7).
Reply
#2
First off, you should put all your imports at the top of the script. Second... That's all I can help you with. IDK how simple this stuff seems to most people, but to 7th grader like me... I only recognize the formula for the area of a circle. Sorry
Reply
#3
https://docs.python.org/3/library/math.h...th.isclose Wrote:math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)¶
  • Return True if the values a and b are close to each other and False otherwise.

    Whether or not two values are considered close is determined according to given absolute and relative tolerances.

    rel_tol is the relative tolerance – it is the maximum allowed difference between a and b, relative to the larger absolute value of a or b. For example, to set a tolerance of 5%, pass rel_tol=0.05. The default tolerance is 1e-09, which assures that the two values are the same within about 9 decimal digits. rel_tol must be greater than zero.

    abs_tol is the minimum absolute tolerance – useful for comparisons near zero. abs_tol must be at least zero.

    If no errors occur, the result will be: abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol).

    The IEEE 754 special values of NaN, inf, and -inf will be handled according to IEEE rules. Specifically, NaN is not considered close to any other value, including NaN. inf and -inf are only considered close to themselves.

    New in version 3.5.
import math

if math.isclose(minf, m_fan_face):
    print('isclose')
Reply
#4
I don't understand what this means in your question:
Quote: match the variable minf and m_fan_face

What does Match mean in this context?
Reply
#5
(Apr-21-2019, 06:48 PM)Yoriz Wrote:
https://docs.python.org/3/library/math.h...th.isclose Wrote:math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)¶
  • Return True if the values a and b are close to each other and False otherwise.

    Whether or not two values are considered close is determined according to given absolute and relative tolerances.

    rel_tol is the relative tolerance – it is the maximum allowed difference between a and b, relative to the larger absolute value of a or b. For example, to set a tolerance of 5%, pass rel_tol=0.05. The default tolerance is 1e-09, which assures that the two values are the same within about 9 decimal digits. rel_tol must be greater than zero.

    abs_tol is the minimum absolute tolerance – useful for comparisons near zero. abs_tol must be at least zero.

    If no errors occur, the result will be: abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol).

    The IEEE 754 special values of NaN, inf, and -inf will be handled according to IEEE rules. Specifically, NaN is not considered close to any other value, including NaN. inf and -inf are only considered close to themselves.

    New in version 3.5.
import math

if math.isclose(minf, m_fan_face):
    print('isclose')

m_fan_face = rho*Ahi*M_fan_face*(gamma*R*tatm)**0.5
for M_fan_face in range(0.5,0.7,0.01):
I wanted to iterate for M_fan_face and substitute this into the first equation and check that it matches minf. The values of rho, Ahi, gamma, R and tatm are fixed. I only need to guess for M_fan_face from 0.5 to 0.7 in increments of 0.01. I know that m_fan_face won't be exactly identical to minf but I want the difference between them to be 1e-09.
Reply
#6
something like this maybe
for M_fan_face in range(0.5,0.7,0.01):
    m_fan_face = rho*Ahi*M_fan_face*(gamma*R*tatm)**0.5
 
    if math.isclose(minf, m_fan_face):
        print(f'm_fan_face isclose using M_fan_face as {M_fan_face}')
        break
else:
    print('M_fan_face is not close enough using range given')
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020