@@ -74,26 +74,25 @@ Encontre uma solução numérica para a equação diferencial com condição ini
74
74
``` {code-cell} ipython3
75
75
%matplotlib widget
76
76
import numpy as np
77
- from scipy.integrate import odeint
77
+ from scipy.integrate import solve_ivp
78
78
import matplotlib.pyplot as plt
79
79
80
- # function that returns dy/dt
81
- def model(y,t):
82
- dydt = -y + 1.0
83
- return dydt
80
+ def exponential_decay(t, y): return -0.5 * y
84
81
85
- # initial condition
86
- y0 = 0
82
+ # solve_ivp(fun, t_span, y0)
83
+ sol = solve_ivp(exponential_decay, [0, 10], [2, 4, 8])
87
84
88
- # time points
89
- t = np.linspace(0,5)
85
+ print(sol.t)
86
+ ```
90
87
91
- # solve ODE
92
- y = odeint(model,y0,t)
88
+ ``` {code-cell} ipython3
89
+ print(sol.y)
90
+ ```
93
91
92
+ ``` {code-cell} ipython3
94
93
# plot results
95
94
fig = plt.figure()
96
- plt.plot(t,y )
95
+ plt.plot(sol.t, sol.y.T )
97
96
plt.xlabel('time')
98
97
plt.ylabel('y(t)')
99
98
plt.show()
@@ -118,15 +117,14 @@ y = x + 5*np.random.rand(10) - 6*np.random.rand(10)
118
117
f = interp1d(x, y)
119
118
120
119
# Gráfico
121
- fig = plt.figure ()
120
+ fig, ax = plt.subplots ()
122
121
t = np.linspace(0, 9, 50)
123
- plt.title('Exemplo: ajuste de curvas')
124
- plt.plot(x, y, 'r*')
125
- plt.plot(t, a_linear*t+b_linear,'g')
126
- plt.plot(t, a_quad*t**2+b_quad*t+c_quad, 'm')
127
- plt.plot(t, f(t), 'b')
128
- plt.legend(['linear', 'quadrático', 'interpolação'])
129
- plt.show();
122
+ ax.set_title('Exemplo: ajuste de curvas')
123
+ ax.plot(x, y, 'r*')
124
+ ax.plot(t, a_linear*t+b_linear,'g')
125
+ ax.plot(t, a_quad*t**2+b_quad*t+c_quad, 'm')
126
+ ax.plot(t, f(t), 'b')
127
+ ax.legend(['linear', 'quadrático', 'interpolação'])
130
128
```
131
129
132
130
### Exemplo: Teste de hipótese estatística
0 commit comments