Last active
September 18, 2017 19:24
-
-
Save RHDZMOTA/720031f6b29e6812f284909ad88da509 to your computer and use it in GitHub Desktop.
Use each file as a jupyter notebook cell. First copy and run the contents of auxiliar_functions.py.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%matplotlib inline | |
from scipy.integrate import odeint | |
import matplotlib.pyplot as plt | |
import numpy as np | |
# Print function | |
def print_results(soln, time): | |
show = 5 | |
def get_head_and_tail(vector): | |
return np.concatenate([vector[:show], vector[-show:]]) | |
print_count = 1 | |
print("Values: ") | |
for t, val in zip(get_head_and_tail(time), get_head_and_tail(soln)): | |
print("[ x1({t:0.2f}), x2({t:0.2f})] = [{x1:0.2f}, {x2:0.2f}]".format(t=t, x1=val[0], x2=val[-1])) | |
if print_count == show: | |
print("...") | |
print_count += 1 | |
# Plot function | |
def plot_results(soln, time): | |
# TODO: aesthetcics and formatting. | |
plt.figure(figsize=(15,6)) | |
plt.plot(time,soln[:,0]) | |
plt.plot(time,soln[:,-1]) | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("Ex 1) [add description] \n") | |
# Update function | |
def step(prev_xs, t, a, b): | |
x1, x2 = prev_xs[0], prev_xs[-1] | |
return [a * x2, -b * x1] | |
# Initial conditions | |
initial_values = [5, 5] | |
time = np.linspace(0,50, 500) | |
# Solution | |
soln = odeint(step, initial_values, time, args=(1, 1)) | |
# Print results | |
print_results(soln, time) | |
# Plot results | |
plot_results(soln, time) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("Ex 2) [add description] \n") | |
# Update function | |
def step(prev_xs, t, a, b): | |
x1, x2 = prev_xs[0], prev_xs[-1] | |
return [-a * x1 + b * x2, b * x1 - a * x2] | |
# ---------------------------------------------- | |
# a) | |
print("A) initial values = (2, 1) and (a,b) = (2, 1)\n") | |
# Initial conditions | |
initial_values = [2, 1] | |
time = np.linspace(0,50, 500) | |
# Solution | |
soln = odeint(step, initial_values, time, args=(2,1)) | |
# Print results | |
print_results(soln, time) | |
# Plot results | |
plot_results(soln, time) | |
# ---------------------------------------------- | |
# b) | |
print("B) initial values = (-2, 1) and (a,b) = (2, 1)\n") | |
# Initial conditions | |
initial_values = [-2, 1] | |
time = np.linspace(0,50, 500) | |
# Solution | |
soln = odeint(step, initial_values, time, args=(2,1)) | |
# Print results | |
print_results(soln, time) | |
# Plot results | |
plot_results(soln, time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment