Reputation: 13
I am coding two different graphs where they are supposed to be alike when a constant dt is a correct value. A part of the assignment is to make a widget slider that controls the constant dt, so when you run the code, the slider appears on the plot and lets you control the constant and changing the graph. However when I run this code:
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact, FloatSlider
#constants
T0 = 83 #[celcius]
Ts = 22.8 #[celcius]
r = 0.1 #[min-1]
#The analytical solution. Part i:
def T(t):
return Ts + (T0 - Ts)*np.exp(-r*t)
t50 = np.arange(0, 50.1, 0.1)
Tanalytisk = []
for i in range(len(t50)):
Tanalytisk.append(T(t50[i]))
#Numerical solution. Part ii:
tslutt = 50
h = 1
def f(T):
return -r*(T-Ts)
def euler(f):
Tn = [T0]
tn = [0]
while(tn[-1] < tslutt):
t, y = tn[-1], Tn[-1]
Tn.append(y + h*f(y))
tn.append(t + h)
return (np.array(tn), np.array(Tn))
tn, Tn = euler(f)
plt.plot(t50, Tanalytisk,label="Analytical") #Analytical solution
plt.plot(tn, Tn, label = "Numerical") #Numerical solution
plt.plot(time, temp, label = "Experimental")
plt.xlabel("Time [min]")
plt.ylabel("Temperature [C]")
plt.title("Analytical solution T(t)")
plt.legend()
plt.show()
håndtak = FloatSlider(value = 1, min = 0, max = 15, step = 0.1, description = "dt")
interact(euler(f), h = håndtak)
I end up with this error:
> TypeError Traceback (most recent call last)
~\anaconda3\lib\site-packages\ipywidgets\widgets\interaction.py in update(self, *args)
254 value = widget.get_interact_value()
255 self.kwargs[widget._kwarg] = value
--> 256 self.result = self.f(**self.kwargs)
257 show_inline_matplotlib_plots()
258 if self.auto_display and self.result is not None:
TypeError: 'tuple' object is not callable
<function ipywidgets.widgets.interaction._InteractFactory.__call__.<locals>.<lambda>(*args,
**kwargs)>
How do I understand the problem and how can I make the widget work? It appears on the graph but doesn't change the graph when changing the value.
Is there an easier way to code the slider?
Thanks in advance!
Upvotes: 1
Views: 622
Reputation: 128
You haven't define time and temp variables so I commented them. Anyway here's how you put the slider in your code :
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as wg
@wg.interact(h = wg.FloatSlider(min = 0, max = 15, step = 0.1, value=1, description = "dt"))
def run(h):
#constants
T0 = 83 #[celcius]
Ts = 22.8 #[celcius]
r = 0.1 #[min-1]
#The analytical solution. Part i:
def T(t):
return Ts + (T0 - Ts)*np.exp(-r*t)
t50 = np.arange(0, 50.1, 0.1)
Tanalytisk = []
for i in range(len(t50)):
Tanalytisk.append(T(t50[i]))
#Numerical solution. Part ii:
tslutt = 50
# h = 1
def f(T):
return -r*(T-Ts)
def euler(f,h):
Tn = [T0]
tn = [0]
while(tn[-1] < tslutt):
t, y = tn[-1], Tn[-1]
Tn.append(y + h*f(y))
tn.append(t + h)
return (np.array(tn), np.array(Tn))
tn, Tn = euler(f,h)
plt.plot(t50, Tanalytisk,label="Analytical") #Analytical solution
plt.plot(tn, Tn, label = "Numerical") #Numerical solution
# plt.plot(time, temp, label = "Experimental")
plt.xlabel("Time [min]")
plt.ylabel("Temperature [C]")
plt.title("Analytical solution T(t)")
plt.legend();
Upvotes: 1