Page MenuHomec4science

ExRefrigeratedTruck.py
No OneTemporary

File Metadata

Created
Sun, Jun 9, 12:27

ExRefrigeratedTruck.py

import numpy as np
import ipywidgets as widgets
from ipywidgets import HBox
import matplotlib.pyplot as plt
from IPython.display import display, clear_output, Latex
def EvaluateNumericalSolution(inputstring, solution, tolerance):
notaNumber = "Result is not a number"
correct = "Your answer is correct!"
wrong = "Wrong. Please try again."
clear_output();
try:
result = float(inputstring)
except ValueError:
print(notaNumber)
return
if np.abs(result-solution)<=tolerance:
print(correct)
else:
print(wrong)
def CreateAnsweringTools():
text = widgets.Text(placeholder='Type your answer')
button = widgets.Button(description="Check answer")
return text, button
"""Question 4"""
def R_wall():
text = widgets.Text(placeholder='Type your answer')
display(text)
checkbutton = widgets.Button(description="Check answer")
hintbutton = widgets.Button(description="Hint")
cheatbutton = widgets.Button(description="Show solution")
buttons = HBox([checkbutton, hintbutton, cheatbutton])
output = widgets.Output()
solution = 1.923
tolerance = 0.1
solstr = str(solution)
def evaluate_Rwall(b):
with output:
if text.value!="":
resultstring = text.value
EvaluateNumericalSolution(resultstring, solution, tolerance)
else:
clear_output();
def display_formula_Rwall(c):
with output:
clear_output()
display(Latex(r"""Recall the formula for the thermal resistance of a planar wall:
\begin{equation} R'' = \frac{L}{k}\end{equation}"""))
def display_solution_Rwall(s):
with output:
clear_output()
display(Latex(r"""Adding all resistances together leads to $R''_{wall} =""" + solstr + """ m^2 K / W$.
Note that the resistance of the aluminum is very small"""))
display(buttons, output)
checkbutton.on_click(evaluate_Rwall)
hintbutton.on_click(display_formula_Rwall)
cheatbutton.on_click(display_solution_Rwall)
"""Question 5"""
def R_conv():
text = widgets.Text(placeholder='Type your answer')
display(text)
checkbutton = widgets.Button(description="Check answer")
hintbutton = widgets.Button(description="Hint")
cheatbutton = widgets.Button(description="Show solution")
buttons = HBox([checkbutton, hintbutton, cheatbutton])
output = widgets.Output()
solution = 52.6
tolerance = 0.3
solstr = str(solution)
def evaluate_Rconv(b):
with output:
if text.value!="":
resultstring = text.value
EvaluateNumericalSolution(resultstring, solution, tolerance)
else:
clear_output();
def display_formula_Rconv(c):
with output:
clear_output()
display(Latex(r"""$Re > 5\cdot10^5$ means the flow is turbulent. Use the following correlation:
\begin{equation} h = 0.037 Re_L^{4/5}Pr^{1/3}\end{equation}"""))
def display_solution_Rconv(s):
with output:
clear_output()
display(Latex(r"""Using the relations $h = \frac{Nu_L k_f}{L}$ and $R''_{conv}=\frac{1}{h}$ leads to
$R''_{conv} =""" + solstr + """ m^2 K / W$."""))
display(buttons, output)
checkbutton.on_click(evaluate_Rconv)
hintbutton.on_click(display_formula_Rconv)
cheatbutton.on_click(display_solution_Rconv)

Event Timeline