Hey currently I'm trying to program a quadratic programming algorithm with Python. But I'm struggling with creating a vector which indicates the dimensions of the problem.
My problem: I am trying to create a vector x = [x_1, x_2, x_3, ..., x_N] which containts N elements. N varies.The elements 'x_N' should be variables / the solution of my optimization problem.
My code so far: ('NoE'... is equal to N 1, thats why Im substracting 1 in my while statement)
#filling the x-vector according to the dimension of the given problem
temp_2 = 0
while temp_2 <= (NoE-1):
x[temp_2]= 'x_' temp_2
print(x)
Thanks for helping! :)
CodePudding user response:
You have to create an empty vector x
and append to it 'x_'
and convert temp_2
into a string in each iteration and concatenate it to 'x_'
, also the incrementation is missing in the code.
The following is the correct code:
temp_2 = 0
NoE=10
x=[] # Create an empty vector
while temp_2 <= (NoE-1):
x.append('x_' str(temp_2))
temp_2 = 1
print(x)
Output
['x_0', 'x_1', 'x_2', 'x_3', 'x_4', 'x_5', 'x_6', 'x_7', 'x_8', 'x_9']
CodePudding user response:
Your aim is to create a vector that has n elemnts formmated x_N
?..If so..Simply take a list and append to it inside the loop.
le =[]
num = 7
for x in range(num):
a = f'x_{x}'
le.append(a)
print(le)
Gives #
['x_0', 'x_1', 'x_2', 'x_3', 'x_4', 'x_5', 'x_6']