I'm writing a Python implementation of Euler's method, using an example from Paul's math notes here.
I'm using a n x 3 numpy array to store the results. The goal is to have the t-value in the first column, y in the second, and the value of y' computed using the current row in the third column.
When I did the first problem listed on the page, using only ten iterations, everything behaved exactly as expected. The step size was 0.1, so the values in the first column incremented by 0.1 with each iteration of the for loop.
But now that I've copied the code over and attempted to apply it to problem 3, the first column behaves very strangely. I inputted the step size as 0.01, but for the first ten iterations it increments by 0.1, then after the tenth iteration it appears to reset to zero, then uses the expected 0.01, but later on it resets again in a similar fashion.
Here's my code:
import numpy as np
def ex3(t,y):
return y + (-0.5 * np.exp(t/2) * np.sin(5*t)) + (5 * np.exp(t/2) * np.cos(5*t))
ex3out = np.empty((0,3), float)
# Input the initial conditions and first y' computation
ex3out = np.append(ex1out, np.array([[0,0,ex3(0,0)]]), axis=0)
h = 0.01
n = 500
for i in range(1,n+1):
# Compute the new t and y values and put in 0 as a dummy y' for now
new = np.array([[ex3out[i - 1,0] + h, ex3out[i - 1,1] + h * ex3out[i - 1,2],0]])
# Append the new row
ex3out = np.append(ex3out,new,axis=0)
# Replace the dummy 0 with y' based on the new values
ex3out[i,2] = ex3(ex3out[i,0],ex3out[i,1])
And here are the first several rows of ex3out after running the above code:
array([[ 0. , 1. , -1. ],
[ 0.1 , 0.9 , 5.2608828 ],
[ 0.2 , 0.852968 , 3.37361534],
[ 0.3 , 0.8374415 , 0.6689041 ],
[ 0.4 , 0.83983378, -2.25688988],
[ 0.5 , 0.85167737, -4.67599317],
[ 0.6 , 0.86780837, -5.90918813],
[ 0.7 , 0.8851749 , -5.51040903],
[ 0.8 , 0.90205891, -3.40904125],
[ 0.9 , 0.91757091, 0.031139 ],
[ 1. , 0.93132436, 4.06022317],
[ 0. , 0. , 5. ],
[ 0.01 , 0.99 , 5.98366774],
[ 0.02 , 0.95260883, 5.92721107],
[ 0.03 , 0.88670415, 5.82942804],
[ 0.04 , 0.84413054, 5.74211536],
[ 0.05 , 0.81726488, 5.65763415],
[ 0.06 , 0.80491744, 5.57481145],
[ 0.07 , 0.80871649, 5.4953251 ],
[ 0.08 , 0.83007081, 5.42066644],
[ 0.09 , 0.8679685 , 5.34993924],
[ 0.1 , 0.9178823 , 5.2787651 ],
[ 0.11 , 0.97192659, 5.19944036],
[ 0.12 , 0.05 , 4.13207859],
[ 0.13 , 1.04983668, 4.97466166],
[ 0.14 , 1.01188094, 4.76791408],
[ 0.15 , 0.94499843, 4.5210138 ],
[ 0.16 , 0.90155169, 4.28666725],
[ 0.17 , 0.87384122, 4.0575499 ],
[ 0.18 , 0.86066555, 3.83286568],
[ 0.19 , 0.86366974, 3.61469476],
[ 0.2 , 0.88427747, 3.40492482],
[ 0.21 , 0.92146789, 3.20302701],
I wondered if this might be a floating point issue, so I tried enclosing various parts of the for loop in float() with the same results.
I must've made a typo somewhere, right?
Simpler loop:
ex3out = [[0, 0, ex3(0,0)]]
h = 0.01
n = 50
for i in range(1,n+1):
# Compute the new t and y values and put in 0 as a dummy y' for now
last = ex3out[-1]
new = [last[0] + h, last[1] + h * last[2], 0]
new[2] = ex3(new[0], new[1])
# Append the new row
ex3out.append(new)
print(np.array(ex3out)) # for pretty numpy display
Related
I am trying to calibrate a hand-to-eye robotic arm and camera system. I am using the cv2.CalibrateHandEye function to calculate the transform matrix between robot base and camera. But I only get wrong results.
The camera is calibrated, and I use the cv2.SolvePnp function to get the translation and rotation vector of the marker from images that took by camera, and use cv2.Rodrigues to transform the rotation vector into the rotation matrix.
The rotation matrix of end is generated using tfs.euler.euler2mat according to the rotation of end effector.
I checked both matrices many times, I think they are correct, but the cv2.CalibrateHandeye function just keep out put answers that not even close the true value.
here is some of the code.
I recorded about 20 sets of images and end pose, code below is how I extract matrix from each of them
(success, rvec, tvec) = cv2.solvePnP(np.array(point_3d), np.array(corners_2d), mtx, dist,flags=cv2.SOLVEPNP_ITERATIVE)
R_board_in_camera = cv2.Rodrigues(rvec)[0]
T_board_in_camera = tvec
H_board_in_camera = np.zeros((4, 4), np.float)
H_board_in_camera[:3, :3] = R_board_in_camera
H_board_in_camera[:3, 3] = np.array(T_board_in_camera).flatten()
H_board_in_camera[3, 3] = 1
R_hand_in_base = tfs.euler.euler2mat(math.radians(angle_x), math.radians(angle_y), math.radians(angle_z), axes='rxyz')
T_hand_in_base = np.array([x,y,z]) # calculated in advance
H_hand_in_base = np.zeros((4, 4), np.float)
H_hand_in_base[:3, :3] = R_hand_in_base
H_hand_in_base[:3, 3] = T_hand_in_base.flatten()
H_hand_in_base[3, 3] = 1
After I got all matrices, I use calibrate funcion
n = len(Ts_hand_to_base)
R_base_to_hand = []
T_base_to_hand = []
R_board_to_camera = []
T_board_to_camera = []
for i in range(n):
Ts_base_to_hand = np.linalg.inv(Ts_hand_to_base[i])
R_base_to_hand.append(np.array(Ts_base_to_hand[:3, :3]))
T_base_to_hand.append(np.array(Ts_base_to_hand[:3, 3]))
R_board_to_camera.append(np.array(Ts_board_to_camera[i][0, :3]))
T_board_to_camera.append(np.array(Ts_board_to_camera[i][:3, 3]))
R_camera_to_base, T_camera_to_base = cv2.calibrateHandEye(R_base_to_hand, T_base_to_hand, R_board_to_camera,T_board_to_camera, method=cv2.CALIB_HAND_EYE_DANIILIDIS)
here are some of the results i get.
# method = cv2.CALIB_HAND_EYE_HORAUD
H_camera_to_base:
[[ 1. -0.01 -0.04 13.54]
[ 0. -0.96 0.29 141.48]
[ -0.04 -0.29 -0.96 0. ]
[ 0. 0. 0. 1. ]]
method = cv2.CALIB_HAND_EYE_HORAUD
[[ -0.22 -0.98 -0.01 186.04]
[ -0.95 0.21 0.23 -187.49]
[ -0.22 0.05 -0.97 782.4 ]
[ 0. 0. 0. 1. ]]
method = cv2.CALIB_HAND_EYE_TSAI
[[ 0.51 -0.3 0.8 64.88]
[-0.54 0.62 0.57 69.03]
[-0.67 -0.72 0.15 0. ]
[ 0. 0. 0. 1. ]]
method = CALIB_HAND_EYE_ANDREFF
error (-7:Iterations do not converge) Rotation normalization issue: determinant(R) is null in function 'normalizeRotation'
method = CALIB_HAND_EYE_PARK
[[nan nan nan nan]
[nan nan nan nan]
[nan nan nan nan]
[ 0. 0. 0. 1.]]
I have a sample DataFrame as below:
First column consists of 2 years, for each year, 2 track exist and each track includes pairs of longitude and latitude coordinated. How can I extract every track for each year separately to obtain an array of tracks with lat and long?
df = pd.DataFrame(
{'year':[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],
'track_number':[0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1],
'lat': [11.7,11.8,11.9,11.9,12.0,12.1,12.2,12.2,12.3,12.3,12.4,12.5,12.6,12.6,12.7,12.8],
'long':[-83.68,-83.69,-83.70,-83.71,-83.71,-83.73,-83.74,-83.75,-83.76,-83.77,-83.78,-83.79,-83.80,-83.81,-83.82,-83.83]})
You can groupby year and then extract a numpy.array from the created dataframes with .to_numpy().
>>> years = []
>>> for _, df2 in df.groupby(["year"]):
years.append(df2.to_numpy()[:, 1:])
>>> years[0]
array([[ 0. , 11.7 , -83.68],
[ 0. , 11.8 , -83.69],
[ 0. , 11.9 , -83.7 ],
[ 0. , 11.9 , -83.71],
[ 1. , 12. , -83.71],
[ 1. , 12.1 , -83.73],
[ 1. , 12.2 , -83.74],
[ 1. , 12.2 , -83.75]])
>>> years[1]
array([[ 0. , 12.3 , -83.76],
[ 0. , 12.3 , -83.77],
[ 0. , 12.4 , -83.78],
[ 0. , 12.5 , -83.79],
[ 1. , 12.6 , -83.8 ],
[ 1. , 12.6 , -83.81],
[ 1. , 12.7 , -83.82],
[ 1. , 12.8 , -83.83]])
Where years[0] would have the desired information for the year 0. And so on. Inside the array, the positions of the original dataframe are preserved. That is, the first element is the track; the second, the latitude, and the third, the longitude.
If you wish to do the same for the track, i.e, have an array of only latitude and longitude, you can groupby(["year", "track_number"]) as well.
This question already has an answer here:
Prevent scientific notation
(1 answer)
Closed 2 years ago.
I am plotting a simple plot in Matplotlib, Python using the following code:
temp=np.array([1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 0.99999999,
0.99999999, 0.99999998, 0.99999996, 0.99999993, 0.99999989,
0.99999982, 0.99999972, 0.99999958, 0.99999933, 0.99999906,
0.99999857, 0.99999791, 0.9999971 , 0.99999611, 0.99999459,
0.99999276, 0.99999014, 0.99998735, 0.99998418, 0.99997975,
0.99997557, 0.99997059, 0.9999657 , 0.99996077])
temp2=np.array([0.025, 0.05 , 0.075, 0.1 , 0.125, 0.15 , 0.175, 0.2 , 0.225,
0.25 , 0.275, 0.3 , 0.325, 0.35 , 0.375, 0.4 , 0.425, 0.45 ,
0.475, 0.5 , 0.525, 0.55 , 0.575, 0.6 , 0.625, 0.65 , 0.675,
0.7 , 0.725, 0.75 , 0.775, 0.8 , 0.825, 0.85 , 0.875, 0.9 ,
0.925, 0.95 , 0.975])
plt.plot(temp2,temp)
plt.xlabel(r'$\frac{\tau}{\tau_c}$')
plt.ylabel(r'$\frac{\alpha ^{ss}}{\alpha {_0} ^{ss}}$')
plt.ticklabel_format(style='plain')
plt.rcParams.update({'font.size': 16})
I am getting the following figure in a scientific notation despite specifying the style to be plain.
What is the issue here and how do I resolve this ?
Setting useOffset=False, will do it, like this:
plt.ticklabel_format(style='plain', useOffset=False)
when running this code I have an error
IndexError: only integers, slices (:), ellipsis (...),
numpy.newaxis (None) and integer or boolean arrays are valid indices
python code
import random
def getsys():
row = ''
for i in range(0 , 8):
randintt = str(random.randint(0 , 4))
row += randintt
return row
def getx():
x = []
for i in range(0,14):
mysys = getsys()
x.append(mysys)
return x
y = getx()
print (y)
import initialsys
import numpy as np
R = np.array([[0.90 , 0.93,0.91 , 0.95],
[0.95 , 0.94, 0.93, 0],
[0.85 , 0.90 , 0.87 , 0.92],
[0.83 , 0.87 , 0.85 , 0 ],
[0.94 , 0.93 , 0.95 , 0],
[0.99 , 0.98 , 0.97 , 0.96],
[0.91 , 0.92 , 0.94 , 0],
[0.81 , 0.90 , 0.91 , 0],
[0.97 , 0.99 , 0.96 , 0.91],
[0.83 , 0.85 , 0.90 , 0],
[0.94 , 0.95 , 0.96 , 0],
[0.79 , 0.82 , 0.85 , 0.90],
[0.98 , 0.99 , 0.97 , 0],
[0.85 , 0.92 , 0.95 , 0.99]
])
def expression(r ,possition , char ):
exp = 1-r[possition , char]
x = initialsys.getx()
possition = 1
Total = 1
char = ""
for row in x :
for char in row :
if char!= 0 :
exp = expression(R , possition , char)
Total = Total*exp
Total = 1-Total
possition = possition + 1
For people who are getting this error while doing machine learning coding using numpy. When you are trying to print out the prediction classes[d["Y_prediction_test"][0,index]] you will be getting the same error. Please note that d["Y_prediction_test"][0,index] return a float like 1.0. so you should convert it to int
plt.show()
val = d["Y_prediction_test"][0,index]
val = int(val)
print(classes[val])
I have looked for this answer. But was not able to find it. It took some time to solve this, I hope this will help you. I'm self-learning ML/AI. So if we are in the same team, let's connect and help each. Thanks.
You have a number of problems in your code but to fix your current error you need to index the array with integers as said in the error and not a char, you could do int(char) and then return the result.
def expression(r, possition , char):
return 1-r[possition, int(char)]
I have a text file with this format:
1 10.0e+08 1.0e+04 1.0
2 9.0e+07 9.0e+03 0.9
2 8.0e+07 8.0e+03 0.8
3 7.0e+07 7.0e+03 0.7
I would like to preserve the first variable of every line and to then normalize the data for all lines by the data on the first line. The end result would look something like;
1 1.0 1.0 1.0
2 0.9 0.9 0.9
2 0.8 0.8 0.8
3 0.7 0.7 0.7
so essentially, we are doing the following:
1 10.0e+08/10.0e+08 1.0e+04/1.0e+04 1.0/1.0
2 9.0e+07/10.0e+08 9.0e+03/1.0e+04 0.9/1.0
2 8.0e+07/10.0e+08 8.0e+03/1.0e+04 0.8/1.0
3 7.0e+07/10.0e+08 7.0e+03/1.0e+04 0.7/1.0
I'm still researching and reading on how to do this. I'll upload my attempt shortly. Also can anyone point me to a place where I can learn more about manipulating data files?
Read your file into a numpy array and use numpy broadcast feature:
import numpy as np
data = np.loadtxt('foo.txt')
data = data / data[0]
#array([[ 1. , 1. , 1. , 1. ],
# [ 2. , 0.09, 0.9 , 0.9 ],
# [ 2. , 0.08, 0.8 , 0.8 ],
# [ 3. , 0.07, 0.7 , 0.7 ]])
np.savetxt('new.txt', data)