You are on page 1of 6

Amartya Mondal ME547 Assignment Anway Bhattacharyya

1801ME07 1801ME14

The objective is to programme the equations for heating and cooling parts with all custom functions.

The error function is custom made, by using Horner’s Method. The non-integrable function has been
expressed as a series of terms inside an exponential function, as shown below:

def erf(z):
t = 1.0 / (1.0 + 0.5 * abs(z))
# using Horner's method
ans = 1 - t * exp(-z*z - 1.26551223 +
t * (1.00002368 +
t * (0.37409196 +
t * (0.09678418 +
t * (-0.18628806 +
t * (0.27886807 +
t * (-1.13520398 +
t * (1.48851587 +
t * (-0.82215223 +
t * (0.17087277))))))))))
if z >= 0.0:
return ans
else:
return -ans

The custom ierfc function is as follows:

# Custom ierfc
def ierfc(x):
t = (exp(-x*x) - x*(1-erf(x)))/sqrt(pi)
return t

The two heat transfer regimes, namely: heating and cooling have their equations as follows:

# For 0 < t < tp

def heating(h, k, alpha, t, z):


inside = (z/sqrt(4*alpha*t))
res = (h/k)*sqrt(4*alpha*t)*ierfc(inside)
return res

# During cooling (t > tp):


Amartya Mondal ME547 Assignment Anway Bhattacharyya
1801ME07 1801ME14

def cooling(h, k, alpha, t, z, tp):


inside1 = (z/sqrt(4*alpha*t))
inside2 = (z/sqrt(4*alpha*(t-tp)))

res = (2*h/k)*sqrt(alpha) * \
(sqrt(t)*ierfc(inside1) - sqrt(t-tp)*ierfc(inside2))
return res

def equation(h, k, t, z, tp, alpha):

if 0 < t < tp:


return heating(h, k, alpha, t, z)
elif t >= tp:
return cooling(h, k, alpha, t, z, tp)
else:
return 0

The equation function is the function which takes in the parameters at each iteration, and first of all
decides the heat transfer regime: heating or cooling based on the value of tp. Then it calls the
heating or the cooling equation depending on the same.

The main function is the plot function which is as follows:

def plot(h, k, tp, alpha):


x = []
y0 = []
y1 = []
y2 = []
for t in np.arange(0.001, 5, 0.01):
t = t*(10**(-6))
x.append(t)
# z=0
y0.append(equation(h, k, t, 0, tp, alpha))
y1.append(equation(h, k, t, 0.009, tp, alpha))
y2.append(equation(h, k, t, 0.02, tp, alpha))

plt.plot(x, y0, label="z=0")


plt.plot(x, y1, label="z=0.009")
plt.plot(x, y2, label="z=0.02")
plt.xlabel('Time. t')
plt.ylabel('Temperature, T')
plt.title("Temperature rise vs time")
plt.show()a
# z=0
plot(h, k, tp, alpha)
Amartya Mondal ME547 Assignment Anway Bhattacharyya
1801ME07 1801ME14

The plot function takes h, k tp and alpha [(𝛼) 𝑖𝑠 𝑤𝑟𝑖𝑡𝑡𝑒𝑛 𝑎𝑠 𝑎𝑙𝑝ℎ𝑎 𝑖𝑛 𝑡ℎ𝑒 𝑐𝑜𝑑𝑒] as arguments, and
using a loop based on t, readies the array x which actually hosts all the used values of t. Now, by
calling equation function, gets the value of 𝛥𝑇(𝑧, 𝑡), which are again, appended to some array(s). For
z = 0, the values of 𝛥𝑇(0, 𝑡) get appended to the array y0, the ones corresponding to z = 0.009 get
appended to the array y1 and the ones for z = 0.02 get appended to the array y2.

The plt.plot lines actually plot the graphs by taking the values of the array x as x coordinate, and the
values of y0, y1, and y2 as the y coordinates, and plots 3 different curves for the three different
values of z.

The graph is as follows:


Amartya Mondal ME547 Assignment Anway Bhattacharyya
1801ME07 1801ME14

The full code is hereby given below:

# Import math Library


# import math
from math import erf, exp, sqrt, pi, exp
import matplotlib.pyplot as plt
import numpy as np

# ------------------------------------------------------
# Constant Values
# alpha = 1.11 * (10**(-4))
# k = 385
# h = 10**12
tp = 10**(-6)
h = 9708777
k = 398
alpha = 111

# ------------------------------------------------------

def getErf(x):
return erf(x)

# Custom erf function

def erf(z):
t = 1.0 / (1.0 + 0.5 * abs(z))
# using Horner's method
ans = 1 - t * exp(-z*z - 1.26551223 +
t * (1.00002368 +
t * (0.37409196 +
t * (0.09678418 +
t * (-0.18628806 +
t * (0.27886807 +
t * (-1.13520398 +
t * (1.48851587 +
t * (-0.82215223 +
t *
(0.17087277))))))))))
if z >= 0.0:
return ans
else:
return -ans

# Custom ierfc
def ierfc(x):
Amartya Mondal ME547 Assignment Anway Bhattacharyya
1801ME07 1801ME14

t = (exp(-x*x) - x*(1-erf(x)))/sqrt(pi)
return t

# For 0 < t < tp

def heating(h, k, alpha, t, z):


inside = (z/sqrt(4*alpha*t))
res = (h/k)*sqrt(4*alpha*t)*ierfc(inside)
return res

# During cooling (t > tp):

def cooling(h, k, alpha, t, z, tp):


inside1 = (z/sqrt(4*alpha*t))
inside2 = (z/sqrt(4*alpha*(t-tp)))

res = (2*h/k)*sqrt(alpha) * \
(sqrt(t)*ierfc(inside1) - sqrt(t-tp)*ierfc(inside2))
return res

def equation(h, k, t, z, tp, alpha):

if 0 < t < tp:


return heating(h, k, alpha, t, z)
elif t >= tp:
return cooling(h, k, alpha, t, z, tp)
else:
return 0

def plot(h, k, tp, alpha):


x = []
y0 = []
y1 = []
y2 = []
for t in np.arange(0.001, 5, 0.01):
t = t*(10**(-6))
x.append(t)
# z=0
y0.append(equation(h, k, t, 0, tp, alpha))
y1.append(equation(h, k, t, 0.009, tp, alpha))
y2.append(equation(h, k, t, 0.02, tp, alpha))

plt.plot(x, y0, label="z=0")


plt.plot(x, y1, label="z=0.009")
Amartya Mondal ME547 Assignment Anway Bhattacharyya
1801ME07 1801ME14

plt.plot(x, y2, label="z=0.02")


plt.xlabel('Time. t')
plt.ylabel('Temperature, T')
plt.title("Temperature rise vs time")
plt.show()

# z=0
plot(h, k, tp, alpha)

You might also like