0

I keep getting a value error when I try to add the last step to my array x[i+1]=... What am I doing wrong here? allocated arrays with similar code and had no issues earlier. Ay help is appreciated.

Source Code:

import numpy as np
import matplotlib.pyplot as plt
#parameters
sigma=10.
rho=28.
beta=8/3.
ti=0.
tf=100
dt=0.01
#pre-allocation
x = np.zeros(tf)
y = np.zeros(tf)
z = np.zeros(tf)
#initial conditions
x[0]=1.
y[0]=1.
z[0]=1.
#functions
fx= lambda x: sigma*(y-x)  #y too?
fy= lambda y: x*(rho-z)-y
fz= lambda z: x*y-(beta*z)
#euler-richardson
for i in np.arange(0,tf-1):
    k1_x = fx(x[i])
    k1_y = fy(y[i])
    k1_z = fz(z[i])
    
    k2_x = fx((x[i]+(0.5*k1_x))*dt)  #maybe just dt?
    k2_y = fy((y[i]+(0.5*k1_y))*dt)
    k2_z = fz((z[i]+(0.5*k1_z))*dt)
    
    x[i+1] = x[i] + k2_x
    y[i+1] = y[i] + k2_y
    z[i+1] = z[i] + k2_z

Error:

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    TypeError: only size-1 arrays can be converted to Python scalars

    The above exception was the direct cause of the following exception:

    ValueError                                Traceback (most recent call last)
    Input In [10], in <cell line: 2>()
          8 k2_y = fy((y[i]+(0.5*k1_y))*dt)
          9 k2_z = fz((z[i]+(0.5*k1_z))*dt)
    ---> 11 x[i+1] = x[i] + k2_x
         12 y[i+1] = y[i] + k2_y
         13 z[i+1] = z[i] + k2_z

    ValueError: setting an array element with a sequence.
GodWin1100
  • 1,380
  • 1
  • 6
  • 14
  • Does it help? https://stackoverflow.com/questions/4674473/valueerror-setting-an-array-element-with-a-sequence – Eftal Gezer Sep 25 '22 at 19:17
  • The `fx`, `fy`, `fz` functions are returning arrays because they reference the previously declared `x`, `y`, `z` arrays. So you're trying to set `x[i+1]` to an array instead of a scalar – August Sep 25 '22 at 19:21

2 Answers2

0

Steps needed to prevent this error:

  1. Easiest way to fix this problem is to use the data-type which support all type of data-type.
  2. Second way to fix this problem is to match the default data-type of array and assigning value.

You need to pass the dtype of array as object, as we must have same dtype for element in the array while in the List we can have a different dtype. It will work fine, I have run your code, you need to change only these lines

#pre-allocation
x = np.zeros(tf, dtype=object)
y = np.zeros(tf, dtype=object)
z = np.zeros(tf, dtype=object)

This solution is helpful.

0

Let's try this in IPython to see what happens:

In [1]: import numpy as np

In [2]: sigma=10.
   ...: rho=28.
   ...: beta=8/3.
   ...: ti=0.
   ...: tf=100
   ...: dt=0.01
   ...: #pre-allocation
   ...: x = np.zeros(tf)
   ...: y = np.zeros(tf)
   ...: z = np.zeros(tf);

In [3]: #initial conditions
   ...: x[0]=1.
   ...: y[0]=1.
   ...: z[0]=1.

In [5]: # functions
   ...: fx= lambda x: sigma*(y-x)
   ...: fy= lambda y: x*(rho-z)-y
   ...: fz= lambda z: x*y-(beta*z);
Out[5]: 

Personally, I find the usage of x, y and z as both global names and function parameters confusing.

Now we call one of your functions:

In [6]: fx(x[4])
Out[6]: 
array([10.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

Your functions only have one parameter. That means they will use the other referenced objects from the context they are called in.

In this case these are the numpy arrays x, y and z. Hence the return value of an array.

Effectivtly, fx(x[4]) is the same as:

In [10]: sigma*(y-x[4])
Out[10]: 
array([10.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
Roland Smith
  • 42,427
  • 3
  • 64
  • 94