Do calculation for array of values - Python

user2992169

I am trying to calculate and plot the amplitude amp of the solution of a time dependent t differential equation of motion (see rhs6) as a function of wd for multiple values of the force coefficient f found in f_array.

So far I have got the code working for plotting amp against wd for a single value of f. The result is a resonance peak:

Resonance peak

The code for plotting amp against wd for one value of f (which produced the above image) is given below.

from pylab import *
from scipy.integrate import odeint
from numpy import *
import math

#Parameters.
k = 2.0
m = 1.0
w0 = (k/m)**(1/2)
alpha = 0.2
l = alpha/(2*m)
f = 1.0
wd = w0 + 0.025
beta = 0.2
t_fixed = 2.0

#Arrays.
t = linspace(0.0, 400.0, 400.0)
wd_array = linspace(w0-1.0, w0+1, 400.0)
f_array = linspace(10.0, 100.0, 3.0)

#Time step.
init_x = 0.0
init_v = 0.0
dx = 15.0
dv = 0.0
init_cond = [init_x,init_v]
init_cond2 = [init_x + dx,init_v + dv]

def rhs6(c,t,wd):
    c0dot = c[1]
    c1dot = -2*l*c[1] - w0*w0*c[0] + (f/m)*cos((wd)*t)
    return [c0dot, c1dot]

amp_array=[]
for wd in wd_array:
    res = odeint(rhs6, init_cond, t, args=(wd,))
    amp = max(res[:,0]) 
    amp_array.append(amp)

plot(wd_array, amp_array)
xlabel('Driving frequency, wd')
ylabel('Ampltiude, amp')    
show()

Now I wish to find amp against wd for multiple values of f. I have tried to do this by basically making a for loop statement over the f_array. However, my approach does not work and I get the error:

setting an element with a sequence.

As it's good to show an attempt, below is mine.

from pylab import *
from scipy.integrate import odeint
from numpy import *
import math

#Parameters.
k = 2.0
m = 1.0
w0 = (k/m)**(1/2)
alpha = 0.2
l = alpha/(2*m)
f = 1.0
wd = w0 + 0.025
beta = 0.2
t_fixed = 2.0

#Arrays.
t = linspace(0.0, 200.0, 200.0)
wd_array = linspace(w0-1.0, w0+1, 200.0)
f_array = linspace(10.0, 200.0, 3.0)

#Time step.
init_x = 0.0
init_v = 0.0
dx = 15.0
dv = 0.0
init_cond = [init_x,init_v]
init_cond2 = [init_x + dx,init_v + dv]

def rhs6(c,t,wd,f):
    c0dot = c[1]
    c1dot = -2*l*c[1] - w0*w0*c[0] + (f/m)*cos((wd)*t)
    return [c0dot, c1dot]

full_array = zeros(len(f_array))    
for index,force in enumerate(f_array):
    amp_list = []
    for wd in wd_array:
        res = odeint(rhs6, init_cond, t, args=(wd,force))
        amp = max(res[:,0]) 
        amp_list.append(amp)
        print(res)
    amp_array = array(amp_list)
    full_array[index] = amp_array

for f in full_array:
    plot(wd, amp)

show()

Any ideas?

Ffisegydd

Your issue is that full_array is a numpy array and you're trying to set an element inside it as a list, hence the setting an element with a sequence.

To solve this you can instead create a two-dimensional numpy array and then set each of the rows to be amp_array as below

from pylab import *
from scipy.integrate import odeint
from numpy import *
import math

#Parameters.
k = 2.0
m = 1.0
w0 = (k/m)**(1/2)
alpha = 0.2
l = alpha/(2*m)
f = 1.0
wd = w0 + 0.025
beta = 0.2
t_fixed = 2.0

#Arrays.
t = linspace(0.0, 200.0, 200.0)
wd_array = linspace(w0-1.0, w0+1, 200.0)
f_array = linspace(10.0, 200.0, 3.0)

#Time step.
init_x = 0.0
init_v = 0.0
dx = 15.0
dv = 0.0
init_cond = [init_x,init_v]
init_cond2 = [init_x + dx,init_v + dv]

def rhs6(c,t,wd,f):
    c0dot = c[1]
    c1dot = -2*l*c[1] - w0*w0*c[0] + (f/m)*cos((wd)*t)
    return [c0dot, c1dot]

full_array = zeros((len(f_array),len(wd_array)))
for index,force in enumerate(f_array):
    amp_list = []
    for wd in wd_array:
        res = odeint(rhs6, init_cond, t, args=(wd,force))
        amp = max(res[:,0]) 
        amp_list.append(amp)
        # print(res)
    amp_array = array(amp_list)
    full_array[index,:] = amp_array

for f in full_array:
    plot(wd_array, f)

show()

Alternatively you could follow gboffi's advice and use either np.vstack or np.hstack to create your full_array.

Your for loop at the end to plot the arrays was also incorrect and so I've edited that. The graph looks like Plot

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Find Numeric Values among string from Array of Objects and do calculation

分類Dev

Score calculation from array values

分類Dev

Python Calculation

分類Dev

Python - Values do not save

分類Dev

SCSS calculation with multiple values

分類Dev

How do I replace values of array by accessing its index in python 3

分類Dev

Convert Dictionary Values into Array in Python

分類Dev

Javascript array calculation and transformation

分類Dev

python geometric mean calculation

分類Dev

Concatenating all list values to an array in python

分類Dev

How to efficiently match two array values in Python?

分類Dev

Python Numpy Array geht values of neighbours

分類Dev

Python Pandas - Quantile calculation manually

分類Dev

How do I optimally distribute values over an array of percentages?

分類Dev

How do I move values out of an array one at a time?

分類Dev

How do I ORDER BY a JSON Array with its String Values?

分類Dev

How do I assign the same array values to 2 different variables

分類Dev

Excel - Find Strings in Column That Do Not Equal Array of Values

分類Dev

How to feed looped WorksheetFunction calculation outputs into an array?

分類Dev

how to do conditional calculation with strange requirement

分類Dev

Python (pandas) loop through values in a column, do a calc with each value

分類Dev

Does python lists in recursion do not change to its previous values?

分類Dev

How to make the calculation of the median faster in Python

分類Dev

Python CRC calculation for the NTAG 424 NFC chip

分類Dev

prevent divide by zero when both values are obtained from a calculation

分類Dev

How do I read uint32_t values out of a nested PROGMEM array?

分類Dev

How do I get code to read both first and last and values of a randomly generated array?

分類Dev

In C, how do I create a `static` array of constant size with zeroed values, but without calloc?

分類Dev

How do I set values inside a global, fixed-size array, in C (in Visual Studio)?

Related 関連記事

  1. 1

    Find Numeric Values among string from Array of Objects and do calculation

  2. 2

    Score calculation from array values

  3. 3

    Python Calculation

  4. 4

    Python - Values do not save

  5. 5

    SCSS calculation with multiple values

  6. 6

    How do I replace values of array by accessing its index in python 3

  7. 7

    Convert Dictionary Values into Array in Python

  8. 8

    Javascript array calculation and transformation

  9. 9

    python geometric mean calculation

  10. 10

    Concatenating all list values to an array in python

  11. 11

    How to efficiently match two array values in Python?

  12. 12

    Python Numpy Array geht values of neighbours

  13. 13

    Python Pandas - Quantile calculation manually

  14. 14

    How do I optimally distribute values over an array of percentages?

  15. 15

    How do I move values out of an array one at a time?

  16. 16

    How do I ORDER BY a JSON Array with its String Values?

  17. 17

    How do I assign the same array values to 2 different variables

  18. 18

    Excel - Find Strings in Column That Do Not Equal Array of Values

  19. 19

    How to feed looped WorksheetFunction calculation outputs into an array?

  20. 20

    how to do conditional calculation with strange requirement

  21. 21

    Python (pandas) loop through values in a column, do a calc with each value

  22. 22

    Does python lists in recursion do not change to its previous values?

  23. 23

    How to make the calculation of the median faster in Python

  24. 24

    Python CRC calculation for the NTAG 424 NFC chip

  25. 25

    prevent divide by zero when both values are obtained from a calculation

  26. 26

    How do I read uint32_t values out of a nested PROGMEM array?

  27. 27

    How do I get code to read both first and last and values of a randomly generated array?

  28. 28

    In C, how do I create a `static` array of constant size with zeroed values, but without calloc?

  29. 29

    How do I set values inside a global, fixed-size array, in C (in Visual Studio)?

ホットタグ

アーカイブ