Question: can you fix this code so that the graphs appear properly with all the lines, and so that the last graph is not a straight

can you fix this code so that the graphs appear properly with all the lines, and so that the last graph is not a straight line? :import numpy as np
import matplotlib.pyplot as plt
# Material properties
rho =138 # Density (kg/m^3)
c =608 # Specific heat capacity (J/kg-K)
k_room =0.0512 # Thermal conductivity at room temperature (W/m-K)
k_1100=0.1462 # Thermal conductivity at 1100\deg C (W/m-K)
emissivity =0.8 # Emissivity of the coating
# Heat flux data
time = np.array([0,100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500])
heat_flux = np.array([0,10,25,50,75,80,78,80,80,82,70,50,30,20,10,0])*1000 # Convert to W/m^2
# Function to calculate temperature-dependent thermal conductivity
def thermal_conductivity(T):
return k_room +(k_1100- k_room)*(T -273.15)/(1100-273.15) # Linear interpolation
# Function to calculate surface radiation
def surface_radiation(T):
return emissivity *5.67e-8* T**4 # Stefan-Boltzmann law
# Finite difference method implementation
def solve_heat_equation(L, dx, dt, initial_temp):
nx = int(L / dx)+1 # Number of spatial nodes
nt = len(time) # Number of time steps based on the provided heat_flux data length
T = np.zeros((nt, nx)) # Initialize temperature array
T[0, :]= initial_temp # Set initial temperature
alpha = k_room /(rho * c) # Thermal diffusivity at room temperature
r = alpha * dt / dx**2 # Stability condition
for j in range(nt -1): # Iterate over time steps
heat_flux_value = heat_flux[min(j, len(heat_flux)-1)]
# Update temperature profile using finite difference method
T[j +1,0]= T[j,0]+(2* r * thermal_conductivity(T[j,1])*(T[j,1]- T[j,0])+
dt * heat_flux_value /(rho * c)- dt * surface_radiation(T[j,0])/(rho * c * dx))
for i in range(1, nx -1):
T[j +1, i]= T[j, i]+ r *(thermal_conductivity(T[j, i +1])*(T[j, i +1]- T[j, i])-
thermal_conductivity(T[j, i])*(T[j, i]- T[j, i -1]))
T[j +1,-1]= T[j,-1] # Adiabatic boundary condition at the back surface
# Debugging print statement
print(f"Time step {j +1}: Heat flux value ={heat_flux_value}")
return T
# (a) Plot temperature vs. depth for different grid resolutions
# Validate lengths of time and heat_flux arrays
print("Length of time array:", len(time))
print("Length of heat_flux array:", len(heat_flux))
# Check the time step calculation
alpha = k_room /(rho * c)
print("Alpha (thermal diffusivity):", alpha)
# Define grid resolutions
dx_values =[0.01,0.005,0.002,0.001]
# Plot temperature vs. depth for different grid resolutions
L =0.1 # Tile thickness (m)
t =1000 # Time (s)
for dx in dx_values:
alpha = k_room /(rho * c) # Thermal diffusivity at room temperature
dt = dx**2/(2* alpha) # Calculate time step based on stability condition
T = solve_heat_equation(L, dx, dt,0)
# Determine the time step index corresponding to time t
time_step_index = np.where(time == t)[0] # Find the index in time array closest to t
if len(time_step_index)>0:
time_step_index = time_step_index[0] # Take the first index if multiple found
else:
time_step_index = len(time)-1 # Use the last time step index if t is beyond the range
# Plot temperature profile at the specified time t
x = np.linspace(0, L, len(T[0]))
plt.plot(x, T[time_step_index, :], label=f'dx ={dx:.3f} m')
# label plot
plt.xlabel('Depth (m)')
plt.ylabel('Temperature (\deg C)')
plt.title(f'Temperature vs. Depth at t ={t} s')
plt.legend()
plt.show()
# (b) Plot temperature vs. time at different depths
L =0.1 # Tile thickness (m)
dx =0.001 # Grid resolution based on part (a)
dt = dx**2/(2* k_room /(rho * c)) # Stability condition
T = solve_heat_equation(L, dx, dt,0)
x_values =[0,0.05,0.1] # Depths (m)
for x in x_values:
i = int(x / dx)
plt.plot(time, T[:, i], label=f'x ={x:.2f} m')
plt.xlabel('Time (s)')
plt.ylabel('Temperature (\deg C)')
plt.title('Temperature vs. Time')
plt.legend()
plt.show()
# (c) Plot temperature vs. depth at different times
# Plot temperature vs. depth at the specified times
L =0.1 # Tile thickness (m)
dx =0.001 # Grid resolution based on part (a)
dt = dx**2/(2* k_room /(rho * c)) # Stability condition
T = solve_heat_equation(L, dx, dt,0)
x = np.linspace(0, L, len(T[0]))
# Define the times of interest for plotting
t_values =[500,1000,1500]
for t in t_values:
# Determine the time step index corresponding to time t
time_step_index = np.where(time == t)[0]
if len(time_step_index)>0:
time_step_index = time_step_index[0] # Take the first index if multiple found
else:
continue # Skip if t is beyond

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!