0
import numpy as np
a = 3.9 #m
L = 7.8 #m

x = np.arange(0,L+0.1,0.1)

def fun_v():
    for value in x:
        if 0 < x < a :
            V= P*(1-(a/L))
        if a < x < L :
            V= -P*(a/L)
    return (V) 
V = fun_v()

print('V =',V)

#ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

5
  • Does this answer your question? ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() Commented Dec 27, 2020 at 12:13
  • 1
    You are not returning anything Commented Dec 27, 2020 at 12:14
  • this whole function doesn't make any sense... can you please explain your question? Commented Dec 27, 2020 at 12:16
  • You are comparing x in the for loop. Maybe you meant 0 < value < a and a < value < L Commented Dec 27, 2020 at 12:16
  • you can't use an array in a 0<x<a expression or in an if. Those only work for single values, not multiple. Commented Dec 27, 2020 at 16:12

3 Answers 3

0

The issues are the following:

  • You are comparing an array instead of a value in a condition, which is ambiguous.
    Try 0 < value < a instead 0 < x < a

    To give an example of what you are doing:

    1. You are first assigning the x like [0, 0.1, 0.2, 0.3 ... ]
    2. Then you are comparing 0 < [0, 0.1, 0.2, 0.3 ... ] < 3.9 at line 0 < x < a
  • Also, the variable P is not defined. You will need to provide a value of P like the ones you provide for a and L.

  • To get an array of V values, you will need to push the calculated V values in an empty array or you can use the map function.

A working example:

import numpy as np
a = 3.9 
L = 7.8 
P = 10 # P is defined

x = np.arange(0,L+0.1,0.1)

def fun_v():
    arr = [] # empty array is initialized
    for value in x:
        if 0 < value < a : # value is compared instead of x
            V = P*(1-(a/L))
            arr.append(V) # V is pushed after calculation
        if a < value < L :
            V = -P*(a/L)
            arr.append(V)
    return arr # return the array of v values
V = fun_v()

print(V)
Sign up to request clarification or add additional context in comments.

Comments

0

I think the comparison is wrong and you didn't declare what is p. I am actually not sure what you trying to do at the end as it wont return anything. but from your code, I believe it suppose to be like this:

import numpy as np
a = 3.9 #m
L = 7.8 #m
x = np.arange(0,L+0.1,0.1)

def fun_v(x_):
    for value in x_:
        P = 1 # declare your P value
        if 0 < value < a:
            V = P*(1-(a/L))
        elif a < value < L:
            V = -P*(a/L)
    return V

V = fun_v(x)
print(V)

But in this code, the V value will not be an array or list. it will just return a value. if you would like to calculate all the x value and separate it in different list or array, you might need an append.

Comments

0

You can do it with a fancy indexing technique of NumPy, which will be both quicker and easier to comprehend:

import numpy as np
a = 3.9 #m
L = 7.8 #m
P=1
x = np.arange(0,L+0.1,0.1)
x[(0<x)&(x>a)]=P*(1-(a/L))
x[(a<x)&(x>L)]=-P*(a/L)
x

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.