Calling/selecting variables (float valued) with user input in Python - python

I've been working on a computational physics project (plotting related rates of chemical reactants with respect to eachother to show oscillatory behavior) with a fair amount of success. However, one of my simulations involves more than two active oscillating agents (five, in fact) which would obviously be unsuitable for any single visual plot...
My scheme was hence to have the user select which two reactants they wanted plotted on the x-axis and y-axis respectively. I tried (foolishly) to convert string input values into the respective variable names, but I guess I need a radically different approach if any exist?
If it helps clarify any, here is part of my code:
def coupledBrusselator(A, B, t_trial,display_x,display_y):
t = 0
t_step = .01
X = 0
Y = 0
E = 0
U = 0
V = 0
dX = (A) - (B+1)*(X) + (X**2)*(Y)
dY = (B)*(X) - (X**2)*(Y)
dE = -(E)*(U) - (X)
dU = (U**2)*(V) -(E+1)*(U) - (B)*(X)
dV = (E)*(U) - (U**2)*(V)
array_t = [0]
array_X = [0]
array_Y = [0]
array_U = [0]
array_V = [0]
while t <= t_trial:
X_1 = X + (dX)*(t_step/2)
Y_1 = Y + (dY)*(t_step/2)
E_1 = E + (dE)*(t_step/2)
U_1 = U + (dU)*(t_step/2)
V_1 = V + (dV)*(t_step/2)
dX_1 = (A) - (B+1)*(X_1) + (X_1**2)*(Y_1)
dY_1 = (B)*(X_1) - (X_1**2)*(Y_1)
dE_1 = -(E_1)*(U_1) - (X_1)
dU_1 = (U_1**2)*(V_1) -(E_1+1)*(U_1) - (B)*(X_1)
dV_1 = (E_1)*(U_1) - (U_1**2)*(V_1)
X_2 = X + (dX_1)*(t_step/2)
Y_2 = Y + (dY_1)*(t_step/2)
E_2 = E + (dE_1)*(t_step/2)
U_2 = U + (dU_1)*(t_step/2)
V_2 = V + (dV_1)*(t_step/2)
dX_2 = (A) - (B+1)*(X_2) + (X_2**2)*(Y_2)
dY_2 = (B)*(X_2) - (X_2**2)*(Y_2)
dE_2 = -(E_2)*(U_2) - (X_2)
dU_2 = (U_2**2)*(V_2) -(E_2+1)*(U_2) - (B)*(X_2)
dV_2 = (E_2)*(U_2) - (U_2**2)*(V_2)
X_3 = X + (dX_2)*(t_step)
Y_3 = Y + (dY_2)*(t_step)
E_3 = E + (dE_2)*(t_step)
U_3 = U + (dU_2)*(t_step)
V_3 = V + (dV_2)*(t_step)
dX_3 = (A) - (B+1)*(X_3) + (X_3**2)*(Y_3)
dY_3 = (B)*(X_3) - (X_3**2)*(Y_3)
dE_3 = -(E_3)*(U_3) - (X_3)
dU_3 = (U_3**2)*(V_3) -(E_3+1)*(U_3) - (B)*(X_3)
dV_3 = (E_3)*(U_3) - (U_3**2)*(V_3)
X = X + ((dX + 2*dX_1 + 2*dX_2 + dX_3)/6) * t_step
Y = Y + ((dX + 2*dY_1 + 2*dY_2 + dY_3)/6) * t_step
E = E + ((dE + 2*dE_1 + 2*dE_2 + dE_3)/6) * t_step
U = U + ((dU + 2*dU_1 + 2*dY_2 + dE_3)/6) * t_step
V = V + ((dV + 2*dV_1 + 2*dV_2 + dE_3)/6) * t_step
dX = (A) - (B+1)*(X) + (X**2)*(Y)
dY = (B)*(X) - (X**2)*(Y)
t_step = .01 / (1 + dX**2 + dY**2) ** .5
t = t + t_step
array_X.append(X)
array_Y.append(Y)
array_E.append(E)
array_U.append(U)
array_V.append(V)
array_t.append(t)
where previously
display_x = raw_input("Choose catalyst you wish to analyze in the phase/field diagrams (X, Y, E, U, or V) ")
display_y = raw_input("Choose one other catalyst from list you wish to include in phase/field diagrams ")
coupledBrusselator(A, B, t_trial, display_x, display_y)
Thanks!

Once you have calculated the different arrays, you could add them to a dict that maps names to arrays. This can then be used to look up the correct arrays for display_x and display_y:
named_arrays = {
"X": array_X,
"Y": array_Y,
"E": array_E,
...
}
return (named_arrays[display_x], named_arrays[display_y])

Related

Is there a way to create a sign function in python?

import re
import math
from random
import randint
import hashlib
P = (-0.15, 2.645)
Q = (0.7, 2.71)
max_mod = 1.158 * 10 ** 77
def SHA256_INT(text):
return int(f'0x{hashlib.sha256(text.encode("ascii")).hexdigest()}', 0)
def ecc_double_slope(P):
slope = (3 * P[0] ** 2) / (2 * P[1])
return slope
def ecc_add(P, Q, slope):
xr = slope ** 2 - P[0] - Q[0]
yr = slope * (P[0] - xr) - P[1]
return (xr, yr)
def ecc_double(P):
slope = ecc_double_slope(P)
sum0 = ecc_add(P, P, slope)
return sum0
def ecc_double_for(P, limit):
lis = []
for i in range(limit):
b = ecc_double(P)
P = b
lis.append((2 ** i, b))
return lis
def greedy2(number):
x = 0
dub_lis = []
add_lis = []
while 2 ** x < number:
dub_lis.append(2 ** x)
x += 1
x -= 1
n = 2 ** x
while n != number:
y = x
while n + (2 ** y) > number:
y -= 1
n += (2 ** y)
x += 1
add_lis.append(2 ** y)
return len(dub_lis), add_lis
def ecc_main_add(P, Q):
x1 = P[0]
y1 = P[1]
x2 = Q[0]
y2 = Q[1]
slope = (y2 - y1) / (x2 - y1)
xr = slope ** 2 - x1 - x2
yr = slope * (x1 - xr) - y1
return xr, yr
def gen_points(gen, points):
if gen == 1:
return points
elif gen > 1:
if type(points) == int:
point_curve = (points, math.sqrt(points ** 3 + 7))
elif type(points) == tuple:
point_curve = points
point_steps = greedy2(gen)
point_dub = point_steps[0]
point_add = point_steps[1]
dub_list = ecc_double_for(point_curve, point_dub)
for i in point_add:
b = ecc_main_add(dub_list[int(math.log(i, 2))][1], dub_list[-1][1])
return b, gen
elif gen == 0:
return 0
def sign(messsage, private_key, public_key, G):
global max_mod
msg_hash = SHA256_INT(messsage)
randnum = randint(2, max_mod)
r = gen_points(randnum, G)[0]
r_x = r[0]
msg_hash = SHA256_INT(messsage)
s = (r_x * private_key + msg_hash) / randnum
p1 = gen_points(msg_hash/s, G)
p2 = gen_points(r_x/s, public_key)
p3 = ecc_main_add(p1, p2)
return p3
pub, priv = gen_points(9756, P)
print(sign('hello', priv, pub, P))
Hello, I'm trying to implement a way to sign messages using this tutorial:
https://learnmeabitcoin.com/beginners/digital_signatures_signing_verifying
I've tried to follow the tutorial before, but it's either my key generation is wrong, I didn't follow the tutorial correctly, or both.
My current code generates an error, I've already debugged it and it generates the error: TypeError: 'NoneType' object is not subscriptable.
Does anyone know how to solve this?

numpy.ndarray lambda is not callable within loop when using scipy.minimize_scalar, works fine single-run

I'm working on a piece of code that calculates surface wave velocities. When I run this as a single-shot in my Jupyter notebook it works fine, however when I try to start looping over the phi1 angle, I end up with the 'numpy.ndarray is not callable error' after the zero iteration of the loop.
It seems to be focused on the lambda (g) I am using, but I am not sure the best way to go about debugging it. Any help/review would be greatly appreciated as Google searches aren't helping too much.
I do apologize for the novel of code, but this is trimmed down to where it should be easily reproducible.
## This code is an implementation of the framework described by M. Cherry et al. in
## Cherry, Matthew R., Shamachary Sathish, and Ramana Grandhi. "A numerical method for predicting Rayleigh surface wave
## velocity in anisotropic crystals." Journal of Computational Physics 351 (2017): 108-120.
## The logic of this code is as follows.
## (1) It then calculates R, T, and Qv (which is the velocity independent Q, needed to determine the velocity range
## over which to search.
## (2) It then iterates to determine the velocity range by changing a variable called 'angle'.
## (3) It then finds a minimum vL, and solves for Q (with velocity as a term), N, A, B, and Z matrices.
## (4) It stores the three Euler angles (p1, P, p2) and RSW_vel
## This does not deal with pseudowaves yet.
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp
import scipy.optimize as spo
import scipy.linalg as la
from scipy.linalg import eig, eigvals
import math
from decimal import *
import time
ts1 = time.time()
# Setup material properties
# Setup the Cij matrix of Ni (units in Pa)
C11 = C22 = C33 = 2.41e11
C12 = C13 = C23 = C21 = C31 = C32 = 1.47e11
C14 = C15 = C16 = C24 = C25 = C26 = C34 = C35 = C36 = C41 = C42 = C43 = C51 = C52 = C53 = C61 = C62 = C63 = 0
C44 = C55 = C66 = 1.26e11
C45 = C46 = C56 = C54 = C64 = C65 = 0
Cij_list = np.array([C11,C12,C13,C14,C15,C16,C21,C22,C23,C24,C25,C26,C31,C32,C33,C34,C35,C36,C41,C42,C43,C44,C45,C46,C51,C52,C53,C54,C55,C56,C61,C62,C63,C64,C65,C66])
Cij=Cij_list.reshape(6,6)
print('Cij =')
print(Cij)
# Setup density of Ni (units are in kg/m3)
rho = 8910
RSW_matrix = np.array([0,0,0,0])
## This is where the setting up of the loop for p1, P, and p2 should be.
# Setup Euler Angles in Bunge's active notation (one time only - demo, will need to be iterated over), angles in degrees. p1 = phi1, P = Phi, p2 = phi2
#p1 = 15
P = 45
p2 = 8
for rotation in range(0, 180):
## Below this point, everything must be done for every Euler calculation.
p1_rad = np.radians(rotation)
P_rad = np.radians(P)
p2_rad = np.radians(p2)
Eul_Ang_deg = np.array([p1,P,p2])
Eul_Ang_rad = np.array([p1_rad,P_rad,p2_rad])
c1 = np.cos(p1_rad)
c2 = np.cos(P_rad)
c3 = np.cos(p2_rad)
s1 = np.sin(p1_rad)
s2 = np.sin(P_rad)
s3 = np.sin(p2_rad)
# Solve Rotation Matrix from Bunge's active notation, and m and n (m is wave propagation vector, n is plane normal)
Rot_mat = np.array([[c1*c3-c2*s1*s3,-c1*s3-c2*c3*s1,s1*s2],
[c3*s1+c1*c2*s3,c1*c2*c3-s1*s3,-c1*s2],
[s2*s3,c3*s2,c2]]) #- could fill in the rest here and just extract col 1 and col 3
print('Rotation Matrix =')
print(Rot_mat)
m_vec = Rot_mat[[0],:]
m1 = Rot_mat[[0],[0]]
m2 = Rot_mat[[0],[1]]
m3 = Rot_mat[[0],[2]]
n_vec = Rot_mat[[2],:]
n1 = Rot_mat[[2],[0]]
n2 = Rot_mat[[2],[1]]
n3 = Rot_mat[[2],[2]]
# Below, we calculate R, T and Q matrices
# CORRECT R matrix calculations
Rij = np.ndarray(shape=(3,3))
Rij[0,0] = C11*m1*n1 + C61*m2*n1 + C51*m3*n1 + C16*m1*n2 + C66*m2*n2 + C56*m3*n2 + C15*m1*n3 + C65*m2*n3 + C55*m3*n3
Rij[0,1] = C16*m1*n1 + C66*m2*n1 + C56*m3*n1 + C12*m1*n2 + C62*m2*n2 + C52*m3*n2 + C14*m1*n3 + C64*m2*n3 + C54*m3*n3
Rij[0,2] = C15*m1*n1 + C65*m2*n1 + C55*m3*n1 + C14*m1*n2 + C64*m2*n2 + C54*m3*n2 + C13*m1*n3 + C63*m2*n3 + C53*m3*n3
Rij[1,0] = C61*m1*n1 + C21*m2*n1 + C41*m3*n1 + C66*m1*n2 + C26*m2*n2 + C46*m3*n2 + C65*m1*n3 + C25*m2*n3 + C45*m3*n3
Rij[1,1] = C66*m1*n1 + C26*m2*n1 + C46*m3*n1 + C62*m1*n2 + C22*m2*n2 + C42*m3*n2 + C64*m1*n3 + C24*m2*n3 + C44*m3*n3
Rij[1,2] = C65*m1*n1 + C25*m2*n1 + C45*m3*n1 + C64*m1*n2 + C24*m2*n2 + C44*m3*n2 + C63*m1*n3 + C23*m2*n3 + C43*m3*n3
Rij[2,0] = C51*m1*n1 + C41*m2*n1 + C31*m3*n1 + C56*m1*n2 + C46*m2*n2 + C36*m3*n2 + C55*m1*n3 + C45*m2*n3 + C35*m3*n3
Rij[2,1] = C56*m1*n1 + C46*m2*n1 + C36*m3*n1 + C52*m1*n2 + C42*m2*n2 + C32*m3*n2 + C54*m1*n3 + C44*m2*n3 + C34*m3*n3
Rij[2,2] = C55*m1*n1 + C45*m2*n1 + C35*m3*n1 + C54*m1*n2 + C44*m2*n2 + C34*m3*n2 + C53*m1*n3 + C43*m2*n3 + C33*m3*n3
Rij_trans = np.transpose(Rij)
Rij_inv = np.linalg.inv(Rij)
# CORRECT T matrix calculations
Tij = np.ndarray(shape=(3,3))
Tij[0,0] = C11*n1*n1 + C16*n2*n1 + C51*n3*n1 + C16*n1*n2 + C66*n2*n2 + C56*n3*n2 + C15*n1*n3 + C65*n2*n3 + C55*n3*n3
Tij[0,1] = C16*n1*n1 + C66*n2*n1 + C56*n3*n1 + C12*n1*n2 + C62*n2*n2 + C52*n3*n2 + C14*n1*n3 + C64*n2*n3 + C54*n3*n3
Tij[0,2] = C15*n1*n1 + C65*n2*n1 + C55*n3*n1 + C14*n1*n2 + C64*n2*n2 + C54*n3*n2 + C13*n1*n3 + C63*n2*n3 + C53*n3*n3
Tij[1,0] = C61*n1*n1 + C21*n2*n1 + C41*n3*n1 + C66*n1*n2 + C26*n2*n2 + C46*n3*n2 + C65*n1*n3 + C25*n2*n3 + C45*n3*n3
Tij[1,1] = C66*n1*n1 + C26*n2*n1 + C46*n3*n1 + C62*n1*n2 + C22*n2*n2 + C42*n3*n2 + C64*n1*n3 + C24*n2*n3 + C44*n3*n3
Tij[1,2] = C65*n1*n1 + C25*n2*n1 + C45*n3*n1 + C64*n1*n2 + C24*n2*n2 + C44*n3*n2 + C63*n1*n3 + C23*n2*n3 + C43*n3*n3
Tij[2,0] = C51*n1*n1 + C41*n2*n1 + C31*n3*n1 + C56*n1*n2 + C46*n2*n2 + C36*n3*n2 + C55*n1*n3 + C45*n2*n3 + C35*n3*n3
Tij[2,1] = C56*n1*n1 + C46*n2*n1 + C36*n3*n1 + C52*n1*n2 + C42*n2*n2 + C32*n3*n2 + C54*n1*n3 + C44*n2*n3 + C34*n3*n3
Tij[2,2] = C55*n1*n1 + C45*n2*n1 + C35*n3*n1 + C54*n1*n2 + C44*n2*n2 + C34*n3*n2 + C53*n1*n3 + C43*n2*n3 + C33*n3*n3
Tij_trans = np.transpose(Tij)
Tij_inv = np.linalg.inv(Tij)
## R and T defined as above. Q needs to be new - and not remove the rv2. Thus, we introduce Qv for "Q to determine
## critical velocity"
Qv = np.ndarray(shape=(3,3))
Qv[0,0] = C11*m1*m1 + C61*m2*m1 + C51*m3*m1 + C16*m1*m2 + C66*m2*m2 + C56*m3*m2 + C15*m1*m3 + C65*m2*m3 + C55*m3*m3
Qv[0,1] = C16*m1*m1 + C66*m2*m1 + C56*m3*m1 + C12*m1*m2 + C62*m2*m2 + C52*m3*m2 + C14*m1*m3 + C64*m2*m3 + C54*m3*m3
Qv[0,2] = C15*m1*m1 + C65*m2*m1 + C55*m3*m1 + C14*m1*m2 + C64*m2*m2 + C54*m3*m2 + C13*m1*m3 + C63*m2*m3 + C53*m3*m3
Qv[1,0] = C61*m1*m1 + C21*m2*m1 + C41*m3*m1 + C66*m1*m2 + C26*m2*m2 + C46*m3*m2 + C65*m1*m3 + C25*m2*m3 + C45*m3*m3
Qv[1,1] = C66*m1*m1 + C26*m2*m1 + C46*m3*m1 + C62*m1*m2 + C22*m2*m2 + C42*m3*m2 + C64*m1*m3 + C24*m2*m3 + C44*m3*m3
Qv[1,2] = C65*m1*m1 + C25*m2*m1 + C45*m3*m1 + C64*m1*m2 + C24*m2*m2 + C44*m3*m2 + C63*m1*m3 + C23*m2*m3 + C43*m3*m3
Qv[2,0] = C51*m1*m1 + C41*m2*m1 + C31*m3*m1 + C56*m1*m2 + C46*m2*m2 + C36*m3*m2 + C55*m1*m3 + C45*m2*m3 + C35*m3*m3
Qv[2,1] = C56*m1*m1 + C46*m2*m1 + C36*m3*m1 + C52*m1*m2 + C42*m2*m2 + C32*m3*m2 + C54*m1*m3 + C44*m2*m3 + C34*m3*m3
Qv[2,2] = C55*m1*m1 + C45*m2*m1 + C35*m3*m1 + C54*m1*m2 + C44*m2*m2 + C34*m3*m2 + C53*m1*m3 + C43*m2*m3 + C33*m3*m3
res = []
res = spo.minimize_scalar(lambda x: ((min(eigvals(Qv * np.cos(np.radians(x)) * np.cos(np.radians(x)) + (Rij + Rij_trans) * np.cos(np.radians(x)) * np.sin(np.radians(x)) + Tij * np.sin(np.radians(x)) * np.sin(np.radians(x)))) / rho)**(0.5))*(1/(np.abs(np.cos(np.radians(x))))), bracket=(-90,0,90), method='Golden')
vel_L = res.fun.real
vel_L_int = int(vel_L)
print('velocity limit')
print(vel_L)
print(vel_L_int)
Det_B_try = 1
Qij = np.zeros(shape=(3,3))
Qij[0,1] = C16*m1*m1 + C66*m2*m1 + C56*m3*m1 + C12*m1*m2 + C62*m2*m2 + C52*m3*m2 + C14*m1*m3 + C64*m2*m3 + C54*m3*m3
Qij[0,2] = C15*m1*m1 + C65*m2*m1 + C55*m3*m1 + C14*m1*m2 + C64*m2*m2 + C54*m3*m2 + C13*m1*m3 + C63*m2*m3 + C53*m3*m3
Qij[1,0] = C61*m1*m1 + C21*m2*m1 + C41*m3*m1 + C66*m1*m2 + C26*m2*m2 + C46*m3*m2 + C65*m1*m3 + C25*m2*m3 + C45*m3*m3
Qij[1,2] = C65*m1*m1 + C25*m2*m1 + C45*m3*m1 + C64*m1*m2 + C24*m2*m2 + C44*m3*m2 + C63*m1*m3 + C23*m2*m3 + C43*m3*m3
Qij[2,0] = C51*m1*m1 + C41*m2*m1 + C31*m3*m1 + C56*m1*m2 + C46*m2*m2 + C36*m3*m2 + C55*m1*m3 + C45*m2*m3 + C35*m3*m3
Qij[2,1] = C56*m1*m1 + C46*m2*m1 + C36*m3*m1 + C52*m1*m2 + C42*m2*m2 + C32*m3*m2 + C54*m1*m3 + C44*m2*m3 + C34*m3*m3
for v in range(1, vel_L_int):
# Calculate rho vel-squared
rv2 = rho * v ** 2
# CORRECT Q matrix calculations
Qij[0,0] = C11*m1*m1 + C61*m2*m1 + C51*m3*m1 + C16*m1*m2 + C66*m2*m2 + C56*m3*m2 + C15*m1*m3 + C65*m2*m3 + C55*m3*m3 - rv2
Qij[1,1] = C66*m1*m1 + C26*m2*m1 + C46*m3*m1 + C62*m1*m2 + C22*m2*m2 + C42*m3*m2 + C64*m1*m3 + C24*m2*m3 + C44*m3*m3 - rv2
Qij[2,2] = C55*m1*m1 + C45*m2*m1 + C35*m3*m1 + C54*m1*m2 + C44*m2*m2 + C34*m3*m2 + C53*m1*m3 + C43*m2*m3 + C33*m3*m3 - rv2
Qij_trans = np.transpose(Qij)
Qij_inv = np.linalg.inv(Qij)
# Create a new Nik matrix
TR_ik = -1*np.matmul(Tij_inv, Rij_trans)
TI_ik = Tij_inv
QRTR_ik = -Qij + np.matmul(np.matmul(Rij, Tij_inv), Rij_trans)
RT_ik = -1*np.matmul(Rij, Tij_inv)
# Nik Creation
Nik_r1to3 = np.concatenate((TR_ik,TI_ik),axis=1)
Nik_r4to6 = np.concatenate((QRTR_ik,RT_ik),axis=1)
Nik_whole = np.concatenate((Nik_r1to3,Nik_r4to6))
# Eigenvalues and Eigenvectors of Nik
eigvals, eigvecs = eig(Nik_whole)
## The following code sorts the eigenvalues for those with negative imaginary numbers, and conducts the matrix
## operations to generate the B, A, and Z matrices from the COLUMNS in Pythons representation of eigenvectors
eigvecsT = np.transpose(eigvecs)
output_ab = output_ab_neg_imag = eigvecsT[eigvals.imag < 0]
output_abT = np.transpose(output_ab)
### This is the B matrix (bottom 3 vectors assumed as b)
B_matrix = output_b = np.delete(output_abT, [0,1,2], axis=0)
B_matrixT = np.transpose(B_matrix)
### This is the A matrix (these are the displacement vectors, and when A is combined with B, the surface impedance (Z) matrix can be found)
A_matrix = output_a = np.delete(output_abT, [3,4,5], axis=0)
det_B = np.linalg.det(B_matrix)
det_cc_B = det_B.conjugate()
det_B_x_det_cc_B = det_B*det_cc_B
v_real = v.real
det_B_x_det_cc_B_real = det_B_x_det_cc_B.real
if det_B_x_det_cc_B_real < Det_B_try:
RSW_vel = v_real
Det_B_try = det_B_x_det_cc_B_real
print('RSW =')
print(RSW_vel)
print('Min |B|x|B|* =')
print(Det_B_try)
col_to_be_added2 = np.array([p1, P, p2, RSW_vel])
RSW_matrix = np.column_stack((RSW_matrix,col_to_be_added2))
print('RSW vel matrix is')
print(RSW_matrix)

I cannot get my 3 DOF bicycle model to work

I'm trying to simulate a vehicle using a dynamic bicycle model but I cannot seem to get it working. If I set a constant steering angle the lateral velocity grows exponentially and creates impossible results.
a = 0.34284
b = 0.40716
m = 155
I = 37.29
def f_DynBkMdl(x, y, delta, theta, dt, states):
dtheta = states[0]
vlat = states[1]
vlon = states[2]
if delta > math.radians(180):
delta = delta - math.radians(360)
if delta<0:
j = 1
else:
j = 0
if dtheta<0:
q = 1
else:
q = 0
dtheta = abs(dtheta)
delta = abs(delta)
sf = delta - (a*dtheta)/vlon
ff = 30.77*math.degrees(sf)
pf = 0
sr = (b*dtheta)/vlon
fr = 30.77*math.degrees(sr)
pr = 0
if j == 1:
fr = -fr
ff = -ff
if q == 1:
dtheta = -dtheta
theta = theta%math.radians(360)
ddtheta = (a*pf*delta + a*ff - b*fr)/I
dvlat = (pf*delta + ff + fr)/m - vlon*dtheta
dvlon = (pf + pr - ff*delta)/m - vlat*dtheta
dx = -vlat*np.sin(theta) + vlon*np.cos(theta)
dy = vlat*np.cos(theta) + vlon*np.sin(theta)
theta = theta + dtheta*dt + (1/2)*ddtheta*dt**2
dtheta = dtheta + ddtheta*dt
vlat = vlat + dvlat*dt
vlon = vlon + dvlon*dt
vabs = np.sqrt(vlat**2 + vlon**2)
x = x + dx*dt
y = y + dy*dt
states = [dtheta, vlat, vlon]
array = np.array([x, y, theta, vabs, states])
return array
With a and b being the distance between the front and rear axle to the vehicle's centre of gravity, m being the mass and I the inertia. x and y are the global position and theta is the heading with delta being the steering angle. I got these equations from this document. Please note I used a simplified tyre model to calculate the lateral forces and I assumed infinite friction so the friction circle was not needed.
Is there something I am missing to make this work?

IndexError: list assignment index out of range. How do I solve this?

I am trying the Newmark's constant average acceleration method. I am getting this error. How do I recover from this error?
IndexError Traceback (most recent call last)
41 for i in range(len(t)):
42 pn[i+1] = p[i+1]+ a1*u[i] + a2*v[i] + a3*a[i]
43 u[i+1] = pn[i+1]/kn
44 v[i+1] = y*(u[i+1]-u[i])/(b*dt) + (1-y/b)v[i] + dt (1-y/(2*b))*a[i]
IndexError: list assignment index out of range
y = 1/2
b = 1/4
u = []
v = []
t = []
p = [0,25,43.3013,50,43.3013,25,0,0,0,0,0,0]
a = []
pn = []
pn.append(0)
x = 0.0
for i in range(11):
z = 0.0 + x
t.append(z)
x = x + 0.1
m = 0.45594
k = 18
c = 0.2865
u.append(0)
v.append(0)
a.append((p[0]-c*v[0]-k*u[0])/m)
dt = 0.1
a1 =(m/(b*dt*dt)+y*c/(b*dt))
a2 = (m/(b*dt)+(y/b-1)*c)
a3 = (((1/(2*b))-1)*m + dt*((y/(2*b))-1)*c)
kn = k + a1
for i in range(len(t)-1):
pn[i+1] = p[i+1]+ a1*u[i] + a2*v[i] + a3*a[i]
u[i+1] = pn[i+1]/kn
v[i+1] = y*(u[i+1]-u[i])/(b*dt) + (1-y/b)*v[i] + dt* (1-y/(2*b))*a[i]
a[i+1] = (u[i+1]-u[i])/(b*dt*dt) - v[i]/(b*dt)-(1/(2*b)-1)*a[i]
Your pn, a, u, v are defined as a list with length 1, so there is no index such as pn[1]. You can use append or define the list with needed length.
for i in range(len(t)):
pn.append(p[i+1] + a1*u[i] + a2*v[i] + a3*a[i])
u.append(pn[i+1]/kn)
v.append(y*(u[i+1]-u[i])/(b*dt) + (1-y/b)*v[i] + dt* (1-y/(2*b))*a[i])
a.append((u[i+1]-u[i])/(b*dt*dt) - v[i]/(b*dt)-(1/(2*b)-1)*a[i])
or
pn, a, u, v = [0]*11, [0]*11, [0]*11 [0]*11
pn[0], u[0], v[0] = 0, 0, 0
a[0] = (p[0]-c*v[0]-k*u[0])/m
...
for i in range(len(t)-1):
pn[i+1] = p[i+1] + a1*u[i] + a2*v[i] + a3*a[i]
u[i+1] = pn[i+1]/kn
v[i+1] = y*(u[i+1]-u[i])/(b*dt) + (1-y/b)*v[i] + dt* (1-y/(2*b))*a[i]
a[i+1] = (u[i+1]-u[i])/(b*dt*dt) - v[i]/(b*dt)-(1/(2*b)-1)*a[i]

Solving 2 Non-Linear Equations

I've been working with this equations of Potential Flow
pSI: 0 = 10 * y + 23.8732414638 * (pi + atan(abs((x - 12) / x))) + 23.8732414638 * (pi - atan(abs((y + 12) / x)))-234.882642242
V: 0 = ((10 + 23.8732414638 * x / (x*2 + (y - 12)*2) + 23.8732414638 * x / (x*2 + (y + 12)*2))**2 + (23.8732414638 * (y - 12) / (x*2 + (y - 12)*2) + 23.8732414638 * (y + 12) / (x*2 + (y + 12)*2))*2)*0.5-8
using the next code to solve them:
# Prototype of N-R for a system of two non-linear equations
# evaluating functions of two variables
from math import *
eq1 = raw_input('Enter the equation 1: ')
eq2 = raw_input('Enter the equation 2: ')
x0 = float(input('Enter x: '))
y0 = float(input('Enter y: '))
def f(x,y):
return eval(eq1)
def g(x,y):
return eval(eq2)
x = x0
y = y0
for n in range(1, 8):
a = (f(x + 1e-06, y) - f(x,y)) / 1e-06
b = (f(x, y + 1e-06) - f(x,y)) / 1e-06
c = - f(x,y)
d = (g(x + 1e-06, y) - g(x,y)) / 1e-06
eE = (g(x, y + 1e-06) - g(x,y)) / 1e-06
fF = - g(x,y)
print "f(x, y)= ", eq1
print "g(x, y)= ", eq2
print """x y """
print x, y
print """a b c d e f """
print a, b, c, d, e, fF
print """
a * x + b * y = c
d * x + e * y = f
"""
print a," * x + ",b," * y = ",c
print d," * x + ",eE," * y = ",fF
_Sy = (c - a * fF / d) / (b - a * eE / d)
_Sx = (fF / d) - (eE / d) * _Sy
Ea_X = (_Sx ** 2 + _Sy ** 2)**0.5
x = x + _Sx
y = y + _Sy
print "Sx = ", _Sx
print "Sy = ", _Sy
print "x = ", x
print "y = ", y
print "|X_1 - X_0| = ", Ea_X
And gives me the Zero division Error
Traceback (most recent call last):
File "C:\Documents and Settings\Principal\Mis documentos\Finished_Non_lin_N_R.py", line 38, in <module>
d = (g(x + 1e-06, y) - g(x,y)) / 1e-06
File "C:\Documents and Settings\Principal\Mis documentos\Finished_Non_lin_N_R.py", line 27, in g
return eval(eq2)
File "<string>", line 1, in <module>
ZeroDivisionError: float division by zero
Then I tried with this one that I've found in one solved question:
from scipy.optimize import fsolve
from math import *
def equations(p):
x, y = p
return ( ((10 + 23.8732414638 * x / (x**2 + (y - 12)**2) + 23.8732414638 * x / (x**2 + (y + 12)**2))**2 + (23.8732414638 * (y -12) / (x**2 + (y - 12)**2) + 23.8732414638 * (y + 12) / (x**2 + (y + 12)**2))**2)**0.5-8, 10 * y + 23.8732414638 * (pi + atan(abs((x - 12) / x))) + 23.8732414638 * (pi - atan(abs((y + 12) / x)))-234.882642242)
x, y = fsolve(equations, (0, 18))
print equations((x, y))
And with the next problem:
<module2>:19: RuntimeWarning: divide by zero encountered in long_scalars
<module2>:19: RuntimeWarning: divide by zero encountered in double_scalars
(-1.3374190643844486e-11, -2.8308022592682391e-11)
And the last part of this story is that I have some code that actually works
but is too clumsy and a little bit inaccurate, and I would like some suggestions in order to make it simple:
from math import *
U = 10
b = 15
h = 12
cF = 0.5 * U * b / pi
pSI0 = 234.882642242
VP = 12.0
X0 = 0
Y0 = 40
sX = 0.01
sY = 0.01
print cF
def FirstFor(A,B,C,D):
for n in range(1,808):
for m in range(1,4002):
X = A - B*n
Y = C - D*m
pSI = U * Y + cF * (pi + atan(abs((Y - h) / X))) + cF * (pi - atan(abs((Y + h) / X)))
Vaprox = ((10 + 23.8732414638 * X / (X**2 + (Y - 12)**2) + 23.8732414638 * X / (X**2 + (Y + 12)**2))**2 +(23.8732414638 * (Y - 12) / (X**2 + (Y - 12)**2) + 23.8732414638 * (Y + 12) / (X**2 + (Y + 12)**2))**2)**0.5
EA1 = abs(pSI0 - pSI)
EA2 = abs(VP - Vaprox)
if EA1 < 0.05 and EA2 < 0.05:
X1f = X
Y1f = Y
H3 = [X1f,Y1f]
print n,m,X,Y,"GG son",EA1,EA2
print H3
for n in range(1,808):
for m in range(1,4002):
X = H3[0] - B*n*0.001
Y = H3[1] - D*m*0.001
pSI = U * Y + cF * (pi + atan(abs((Y - h) / X))) + cF * (pi - atan(abs((Y + h) / X)))
Vaprox = ((10 + 23.8732414638 * X / (X**2 + (Y - 12)**2) + 23.8732414638 * X / (X**2 + (Y + 12)**2))**2 +(23.8732414638 * (Y - 12) / (X**2 + (Y - 12)**2) + 23.8732414638 * (Y + 12) / (X**2 + (Y + 12)**2))**2)**0.5
EA1 = abs(pSI0 - pSI)
EA2 = abs(VP - Vaprox)
if EA1 < 0.0001 and EA2 < 0.0001:
X2f = X
Y2f = Y
I3 = [X2f,Y2f]
print n,m,X,Y,"GG son",EA1,EA2
print I3
for n in range(1,808):
for m in range(1,4002):
X = H3[0] + B*n*0.001
Y = H3[1] + D*m*0.001
pSI = U * Y + cF * (pi + atan(abs((Y - h) / X))) + cF * (pi - atan(abs((Y + h) / X)))
Vaprox = ((10 + 23.8732414638 * X / (X**2 + (Y - 12)**2) + 23.8732414638 * X / (X**2 + (Y + 12)**2))**2 +(23.8732414638 * (Y - 12) / (X**2 + (Y - 12)**2) + 23.8732414638 * (Y + 12) / (X**2 + (Y + 12)**2))**2)**0.5
EA1 = abs(pSI0 - pSI)
EA2 = abs(VP - Vaprox)
if EA1 < 0.0001 and EA2 < 0.0001:
X2f = X
Y2f = Y
I3 = [X2f,Y2f]
print n,m,X,Y,"GG son",EA1,EA2
print I3
# starting with the FirstFor
FirstFor(X0,sX,Y0,sY)
print "\n This should be good"
raw_input()

Categories

Resources