Computational Math Project
The heat conduction equation in a rod
For our final project, we were given a topic to look for a change of temperature on a stick within the stipulated time. For example, if a 100cm stick has a temperature of 100 ° C it will be cooled for 10 seconds. The purpose of this project is to find the temperature change every thousandth of a second for 10 seconds. So first, we will use numpy and matplotlib to calculate and search for results from this research. This can be done by:
import numpy as np;
import matplotlib.pyplot as plt
Import the following numpy and matplotlib to function the code for showing the graph and the calculation for numpy.
Next, we created a function to calculate the temperature at every thousandth of a second, as follows:
def calculate (pointinmid, calcmatrix, time, coeff, currtime = 1):
for cal in range (1, pointinmid-1):
calcmatrix [currtime] [cal] = calcmatrix [currtime-1] [cal] + coeff * (calcmatrix [currtime-1] [cal-1] +
calcmatrix [currtime-1] [cal + 1] – (2 * calcmatrix [currtime-1] [cal]))
print (calcmatrix [currtime] [cal])
print (“———————————————-” )
currtime + = 1
if (currtime <= time):
return calculate (pointinmid, calcmatrix, time, coeff, currtime)
else:
return calcmatrix
By putting pointinmid, calcmatrix, time, coeff, currtime = 1, as parameters in function, we will implement the formula
Tl + 1i = Tli + λTli + 1-2Tli + Tli-1
Where
λ = kt / (x) 2.
Next we create a ‘user input interaction’ where the user can determine the conditions that will determine the results of this research, as follows:
coeff = float (input (“Thermal conductivity:”))
x = float (input (“Length of the rod:”))
fronttemp = float (input (“Front temperature:”))
backtemp = float (input (“Back Temperature:”))
time = int (input (“Time:”))
pointinmid = int (input (“Total of Points between front and back:”))
The above input will determine the factors in the temperature difference in a stick. When running code, after we type the following input, intmid point will increase, deltax value x divide pointintmid. For coordx, we use the array to calculate the delta times of the rod by using a loop of (pointinmid + 1). Fronttemp is the starting point in a stick and also the initial temperature on a stick. Backtemp is the end point in a stick and also the last temperature on a stick.
Code of the final result:
pointinmid + = 1
deltax = x / pointinmid
coordx = np.array ([deltax * cordx for cordx in range (pointinmid + 1)])
pointinmid + = 1
initial = np.array ([[0.0 for i in range (pointinmid)] for j in range (time + 1)])
for l in range (time + 1):
initial [l] [0] = fronttemp
initial [l] [- 1] = backtemp
xtest = calculate (pointinmid, initial, time, coeff)
Example input:
Coeff = 0.1
X = 100cm
Fronttemp = 100 ° C
Backtemp = 50 ° C
Time = 10
Point in mid = 4
Final:
plt.figure (1); plt.clf ()
pl = plt.subplot ()
for h in range (time + 1):
pl.plot (coordx, xtest [h], “- o”)
pl.set_xlabel (“rod”)
pl.set_ylabel (“Temperature”)
plt.show ()
P.S. the program result can only be seen by my partner Nicander or Felix
Members:
Alfi – 2101693574
Felix – 2101693851
Nicander 2101693113
Recent Comments