Merlin - 1 year ago 142

Python Question

This is a simplified version of code. When run with no flag, I get results.

But, when I use flags it throws errors. I need to run this code through fsolve. The code has been simplified.

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

`import numpy as np`

from scipy.optimize import fsolve

price = np.array([39, 34, 29, 25, 21])

S = np.repeat(300, len(price))

flag = np.array([1, 1, 0, 1, 1])

def Val(S, flag = 0):

p = 4

if flag == 0: p = S * flag

elif flag == 1: p = S * flag

return p

val = lambda x: Val(S, flag) - price

print fsolve(val, np.repeat(35, len(S)))

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

This solves the problem:

```
import numpy as np
from scipy.optimize import fsolve
price = np.array([39, 34, 29, 25, 21])
S = np.repeat(300, len(price))
flag = np.array([1, 1, 0, 1, 1])
def Val(S, flag = 0):
return np.where(flag, S + price, S - price )
val = lambda x: Val(S, flag) - price
print fsolve(val, np.repeat(35, len(S)))
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**