0

I am trying to create a 2d-array with values taken from the array's previous value, added to values computed from a function. I am however getting the error :

ValueError: operands could not be broadcast together with shapes (2,) (0,) 

Now while I know what the error means, I can't seem to find where the problem is exactly or how to fix it.

The program's use is to create a custom euler solver which adds previous values of the array with the function

Code:

import numpy as np
from scipy.integrate import odeint


def func(): 
    d1 = #equation
    d2 = #equation
    return [[d1], [d2]]

l = 1
m = 1
g = 1
mu = 1

max = 1

arr_in = np.array(1, 1)

arr_2 = np.linspace(1, 4, maxx) 
dt = 1

arr_out = np.zeros((max, 2))
arr_out[0] = arr_in[0]
for i in range(len(arr_2)):
    arr_out[i+1] = arr_out[i] + func(arr_out[i-1], arr_2[i-1], l, m, g, mu) * dt

Traceback:

Traceback (most recent call last):
  File "task3.py", line 31, in <module>
    res_arr[i] = solver(res_arr, i)
ValueError: could not broadcast input array from shape (2,2) into shape (2,)

2
  • To find the error look at the traceback (I should downvote you for not showing it). If the problem line is too long, split it until you identify the problem variables.
    – hpaulj
    Mar 14, 2022 at 22:41
  • The subject line listed shapes (2,) and (0,), different from the added traceback. The traceback problem is that res_out[i+1] is a (2,) shape, a "row' of a 2d, while solver returns a (2,2). That's a shape mismatch. I don't see that traceback line in your code sample.
    – hpaulj
    Mar 15, 2022 at 7:26

2 Answers 2

1

The issue was that the pend_func function was returning an array of a different shape than the rest of the arrays. This was fixed by changing

return [[d1], [d2]]

to

return [d1, d2]
0

You're either missing a pair of parentheses:

for i in range(len(time_arr)):
    #              v                                                                v
    res_arr[i+1] = (res_arr[i] + pend_func(res_arr[i-1], time_arr[i-1], l, m, g, mu)) * dt
    #              ^                                                                ^

Or you need to convert the return type of pend_func to np.array:

for i in range(len(time_arr)):
    #                           vvvvvvvvv                                                   v
    res_arr[i+1] = res_arr[i] + np.array(pend_func(res_arr[i-1], time_arr[i-1], l, m, g, mu)) * dt
    #                           ^^^^^^^^^                                                   ^

Both will solve the problem, but they'll produce different results, so I'm not sure which one is appropriate for your purpose.

1
  • The first suggested method retained the same error. The second suggested method outputted the same error, however this time "from shape (2,2) into shape (2,)". The issue is that res_arr has shape (500, 2) which is why I find it confusing. Mar 14, 2022 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.