JavaScript
x
17
17
1
import numpy as np
2
a = 3.9 #m
3
L = 7.8 #m
4
5
x = np.arange(0,L+0.1,0.1)
6
7
def fun_v():
8
for value in x:
9
if 0 < x < a :
10
V= P*(1-(a/L))
11
if a < x < L :
12
V= -P*(a/L)
13
return (V)
14
V = fun_v()
15
16
print('V =',V)
17
#ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Advertisement
Answer
The issues are the following:
You are comparing an array instead of a value in a condition, which is ambiguous.
Try0 < value < a
instead0 < x < a
To give an example of what you are doing:
- You are first assigning the
x
like[0, 0.1, 0.2, 0.3 ... ]
- Then you are comparing
0 < [0, 0.1, 0.2, 0.3 ... ] < 3.9
at line0 < x < a
- You are first assigning the
Also, the variable
P
is not defined. You will need to provide a value ofP
like the ones you provide fora
andL
.To get an array of
V
values, you will need to push the calculatedV
values in an empty array or you can use themap
function.
A working example:
JavaScript
1
21
21
1
import numpy as np
2
a = 3.9
3
L = 7.8
4
P = 10 # P is defined
5
6
x = np.arange(0,L+0.1,0.1)
7
8
def fun_v():
9
arr = [] # empty array is initialized
10
for value in x:
11
if 0 < value < a : # value is compared instead of x
12
V = P*(1-(a/L))
13
arr.append(V) # V is pushed after calculation
14
if a < value < L :
15
V = -P*(a/L)
16
arr.append(V)
17
return arr # return the array of v values
18
V = fun_v()
19
20
print(V)
21