0

Why it has syntax error?? In teta = th2[i]

from math import cos, sin, pi import numpy as np from scipy.optimize import fsolve r1 = 0.245 r2 = 0.35 r3 = 0.31292 r4 = 0.286356 th1 = 0 th2 = np.arange(0, pi/2, 0.1) length = np.copy(th2) for i in length: teta = th2[i] def posture(z): th3 = z[0] th4 = z[1] p = np.zeros((2, 1)) p[0] = r1*cos(th1)+r2*cos(teta)+r3*cos(th3)+r4*cos(th4) p[1] = r1*sin(th1)+r2*sin(teta)+r3*sin(th3)+r4*sin(th4) return p zGuess = np.array([3, 0.5]) z = fsolve(posture, zGuess) print(z)

24th Mar 2020, 5:43 PM
Mehrta Farsad
Mehrta Farsad - avatar
9 Answers
0
After a bit of research, I've found that numpy no longer allows floating point indexing. i is a float, which is why it gives the IndexError. As I don't really know what this code is doing, I can't offer any advice on how to fix. I have no idea why you get a Syntax Error. Lothar 's point is that you create a new function definition for every loop. Much better and more efficient is to define the function once and take 'teta' as an argument.
24th Mar 2020, 6:46 PM
Russ
Russ - avatar
+ 2
There are several issues in the code. First of all you should unindent the complete function "posture" as it is to left side. An other thing is this part of the code: for i in length: teta = th2[i] Here you get values from array "length". in a for loop. As array contains only float numbers, you get an error message, because only integers are valid index values.
24th Mar 2020, 6:16 PM
Lothar
Lothar - avatar
0
What should i do instead?
24th Mar 2020, 5:55 PM
Mehrta Farsad
Mehrta Farsad - avatar
0
Sorry, I was wrong before. I ran the code and didn't get the error you described. Here, it is giving an IndexError, but not when I run it on Pythonista đŸ€·â€â™‚ïž https://code.sololearn.com/cQjznsS25UCJ/?ref=app
24th Mar 2020, 6:06 PM
Russ
Russ - avatar
0
Thanks, do you know what can i do?
24th Mar 2020, 6:09 PM
Mehrta Farsad
Mehrta Farsad - avatar
0
For this indexError
24th Mar 2020, 6:09 PM
Mehrta Farsad
Mehrta Farsad - avatar
0
Alas no, sorry!
24th Mar 2020, 6:13 PM
Russ
Russ - avatar
0
Thanks . But i didn't understand the "posture" issue?
24th Mar 2020, 6:18 PM
Mehrta Farsad
Mehrta Farsad - avatar
0
👍 thanks.
24th Mar 2020, 6:47 PM
Mehrta Farsad
Mehrta Farsad - avatar