def am3_tuple(function, x0, x1, y0, n): csv_file_prey = open('results_prey.csv', 'w') csv_file_predator = open('results_predator.csv', 'w') prey_writer = csv.writer(csv_file_prey) predator_writer = csv.writer(csv_file_predator) h = (x1 - x0) / float(n) # Inicializacao x_k_minus_one = x0 y_k_minus_one = y0 # Primeiro ponto x_k = x0 + h y_k = rk2(function, x0, x_k, y0, 1) # Segundo ponto x_k_plus_one = x_k + h y_k_plus_one = rk2(function, x_k, x_k_plus_one, y_k, 1) for i in range(1, n + 1): # Calcula temporarias para usar na conta temp_k_minus_one = multi_tuple(- 1 / float(12), function(x_k_minus_one, y_k_minus_one)) temp_k = multi_tuple(8 / float(12), function(x_k, y_k)) predicted_y_k_plus_one = rk2(function, x_k, x_k_plus_one, y_k, 1) temp_k_plus_one = multi_tuple(5 / float(12), function(x_k_plus_one, predicted_y_k_plus_one)) # Calcula novo valor de Y new_y = add_two_tuples(y_k, multi_tuple(h, (add_many_tuples( temp_k_minus_one, temp_k, temp_k_plus_one)))) # Atualiza os valores de X x_k_minus_one = x_k x_k = x_k_plus_one x_k_plus_one = x_k_plus_one + h # Atualiza os valores de Y y_k_minus_one = y_k y_k = y_k_plus_one y_k_plus_one = new_y # Salva nos arquivos csv prey_writer.writerow([x_k_plus_one, y_k_plus_one[0]]) predator_writer.writerow([x_k_plus_one, y_k_plus_one[1]]) csv_file_prey.close() csv_file_predator.close() return y_k_plus_one
def BC_caseI(omega): """ Finds the value of w_1(x) at the end point. x = L = 1 (m) """ L = 1. w0 = [0.0, 1.0, omega] w = rk2(w0, 0.0, 1, 0.001, caseI) return w[-1, 0]
def long_ball(v0, theta0): """ Finds the distance traveled by a baseball with the initial velocity magnitude v0 and launched at angle theta0 Inputs ---------- v0: Initial velocity magnitude (m/s) that0: Initial launch angle (degrees) Output: ---------- d: Distance the ball traveled (m) """ theta_r = np.radians(theta0) #Convert the input angle into radians dt = 0.01 #time step size (s) #Initial conditions w0 = [x0, y0, vx0, vy0] w0 = [0, 0, v0 * np.cos(theta_r), v0 * np.sin(theta_r)] t0 = 0.0 #This is my 'flag' condition. The program will keep checking what 'done' is. #When it gets set to True then the program will end. done = False while not done: w, t = rk2(w0, t0, t0 + 2 * dt, dt, baseball) #'tf' is set to t0+2*dt becasue I want #to take one step and then check to see #if we're done. The 2 is needed because #arange doesn't end at tf but at tf-dt w0 = w[1] t0 = t[1] + dt if w[1, 1] < 0: #Checks if the 'y' position is in the ground or not done = True if t[-1] > 30: return print( 'Something went wrong, baseball is in the air to long') #Baseball has it the ground in the last step if done: x0 = w[0, 0] y0 = w[0, 1] x1 = w[1, 0] y1 = w[1, 1] frac = y0 / (y0 - y1) d = x0 * frac + x1 * (1 - frac) return d
def am3_int(function, x0, x1, y0, n): h = (x1 - x0) / float(n) # Inicializacao x_k_minus_one = x0 y_k_minus_one = y0 # Primeiro ponto x_k = x0 + h y_k = rk2(function, x0, x_k, y0, n) # Segundo ponto x_k_plus_one = x_k + h y_k_plus_one = rk2(function, x_k, x_k_plus_one, y_k, n) for i in range(1, n + 1): # Calcula temporarias para usar na conta temp_k_minus_one = -1 / 12 * function(x_k_minus_one, y_k_minus_one) temp_k = 8 / 12 * function(x_k, y_k) predicted_y_k_plus_one = rk2(function, x_k, x_k_plus_one, y_k, n) temp_k_plus_one = 5 / 12 * function(x_k_plus_one, predicted_y_k_plus_one) # Calcula novo valor de Y new_y = y_k + h * (temp_k_minus_one + temp_k + temp_k_plus_one) # Atualiza os valores de X x_k_minus_one = x_k x_k = x_k_plus_one x_k_plus_one = x_k_plus_one + h # Atualiza os valores de Y y_k_minus_one = y_k y_k = y_k_plus_one y_k_plus_one = new_y return y_k_plus_one
## Import all of my methods import euler import rk2 import rk4 import se1 import se2 import sho1 import numpy as np import matplotlib.pyplot as plt ## Execute the calculations of each one euler.euler(euler.x, euler.v) rk2.rk2(rk2.x, rk2.v) rk4.rk4(rk4.x, rk4.v) se1.se1(se1.x, se1.p) se2.se2(se2.x, se2.p) methods = [euler, rk2, rk4, se1, se2] colors = ["r-", "y-", "g-", "b-", "c-"] ## Graphing area for i, method in enumerate(methods): plt.plot(method.t_points, method.e_points, colors[i], label=method.name) plt.legend() plt.xlabel("t") plt.ylabel("total energy") plt.title("Energy Conservation") plt.plot(sho1.t_points, sho1.e_points, "r--", label=sho1.name) plt.show()
from euler import eulerMethod from rk2 import rk2 from rk4 import rk4 from rrz2 import rrz2 if __name__ == "__main__": eulerMethod() rk2() rk4() rrz2()
import picard import newton import rk2 if __name__ == "__main__": picard.picard() newton.newton() rk2.rk2()