0

i am getting an error File

"C:/Users/user/tensorEnv/project.py", line 28, in <module>
    if N <= 100 :
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

can someone tell me how to use a.any() or a.all() or help me fix this issue

import matplotlib.pyplot as plt
import numpy as np
r3=np.arange(1.11,10,0.1)
r1=1
r2=1.1
k=5
R1cond=3.36*10**(-3)
R2cond=(1/k)*(0.0727-(0.08/r3))
Rconv=(0.004)/(r3**2)
Rtotal=Rconv+R1cond+R2cond
qloss=975/Rtotal
t=176
Q=qloss*t
L0=[]
L0.append(Q)
L0=[x*10**(-3) for x in L0]
#E is the price of EDL in LBP/NW.h
#Eel is the electric price to pay
#t is the time of working hours of the furnace 22 business days
#E=Eel*qloss*t
L=[]
L1=[]
L2=[]
L3=[]
L4=[]
for N in L0:
        if N <= 100 :
                L.append(35*N)
        if N <= 300 and N > 100:
                L1.append( 55*N)
        if N <=400 and N > 300:
                L2.append(80*N)
        if N <= 500 and N > 400:
                L3.append(120*N)
        if N > 500 :
                L4.append(200*N)
plt.plot(L)
plt.plot(L1)
plt.plot(L2)
plt.plot(L3)
plt.plot(L4)
plt.show()
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • 1
    What do you expect `N` to be? As written `L0` is a list of numpy arrays. – hpaulj Feb 06 '20 at 18:21
  • https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous – AMC Feb 08 '20 at 21:59
  • Variable and function names should follow the `lower_case_with_underscores` style. – AMC Feb 08 '20 at 21:59

1 Answers1

1

As the code is written, the variable N stores each item of L0 for every iteration. It looks like L0 is a list containing Q based on the code preceding the loop. This is due to the r3 = np.arange() line.

One of the first things I would do is confirm that things are working as expected. For example, adding a simple print(N) inside the loop to confirm that N truly is an array of floating values (i.e. Q):

for N in L0:
    print( N )
    ...

Assuming all this is expected, it is not syntactically correct to compare an array to a single value, which is why the error suggested using either the .all() or .any() functions. This would have a syntax like the following if we are checking if any of the elements in N (and therefore Q) is greater than or equal to 100:

if ( N <= 100 ).any():
    ...

And the following if we want to check if all the elements in N is greater than or equal to 100:

if ( N <= 100 ).all():
    ...
Jason K Lai
  • 1,500
  • 5
  • 15
  • it worked thank you but my plot of L,L1,L2,L3,L4 is giving an empty figure why – joseph tannoury Feb 07 '20 at 12:45
  • If you examine the `L`, `L1`, etc variables after the `for` loop (e.g. `print( L )`, you will see that your variable is a list containing an array (i.e. `[array([...])]`). To plot the data, you just want to feed in the array, so you would plot it via `plot( L[0] )`, which calls the first array of the `L` list of arrays. Also, when you examine your variables, you will notice that all your numbers are in the 1e5 range, so most of your if/else statements are useless... – Jason K Lai Feb 07 '20 at 16:30