# Solve ODE for Parameter Arrays (Python)

* I know this question is pretty simple, but I would like to know the best way to set up such a for loop in Python.

I already wrote a program to calculate and construct a solution to a 2nd order differential equation (this code is shown below).

I would like to know the best techniques for iterating this calculation for an array of f parameters (hence `f_array`

). That is, so the graph shows the `20`

data sets related to solutions as a function of `t`

, each with a different value `f`

.

Cheers for any ideas.

``````from pylab import *
from scipy.integrate import odeint

#Arrays.
tmax = 100
t = linspace(0, tmax, 4000)
fmax = 100
f_array = linspace(0.0, fmax, 20)

#Parameters
l = 2.5
w0 = 0.75
f = 5.0
gamma = w0 + 0.05
m = 1.0
alpha = 0.15
beta = 2.5

def rhs(c,t):
c0dot = c
c1dot = -2*l*c - w0*w0*c + (f/m)*cos((gamma)*t)-alpha*c - beta*c*c*c
return [c0dot, c1dot]

init_x = 15.0
init_v = 0.0
init_cond = [init_x,init_v]
ces = odeint(rhs, init_cond, t)

s_no = 1
subplot(s_no,1,1)
xlabel("Time, t")
ylabel("Position, x")
grid('on')
plot(t,ces[:,0],'-b')
title("Position x vs. time t for a Duffing oscillator.")
show()
```

```

Here is a graph showing the solution to this equation with respect to a single value `f`

for an array of values `t`

. I would like to quickly repeat this plot for an array of values `f`

. +3

source to share

Here's one approach:

Modify `rhs`

to accept a third argument, parameter `f`

. The definition `rhs`

must begin

``````def rhs(c, t, f):
...
```

```

Iterating over `f_array`

with a loop `for`

. In the loop, call `odeint`

with an argument `args`

to `odeint`

give the value `f`

as the third argument `rhs`

. Save the results of each call `odeint`

in a list. Basically replace

``````ces = odeint(rhs, init_cond, t)
```

```

from

``````solutions = []
for f in f_array:
ces = odeint(rhs, init_cond, t, args=(f,))
solutions.append(ces)
```

```

For each value `f`

in `f_array`

, you now have a solution listed `solutions`

.

To build them, you can place your call `plot`

in another loop `for`

:

``````for ces in solutions:
plot(t, ces[:, 0], 'b-')
```

```
+4

source

All Articles
{"ft_lang_label":"__label__en","ft_lang_prob":0.680379,"math_prob":0.9951652,"size":2037,"snap":"2021-43-2021-49","text_gpt3_token_len":640,"char_repetition_ratio":0.106246926,"word_repetition_ratio":0.027247956,"special_character_ratio":0.31713304,"punctuation_ratio":0.15652174,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9990442,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-23T13:28:58Z\",\"WARC-Record-ID\":\"<urn:uuid:2fff4af1-c734-4e00-806b-d332e8fdb720>\",\"Content-Length\":\"36557\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b0d91c15-93a4-4bc0-9257-0bf2b8ac0cf9>\",\"WARC-Concurrent-To\":\"<urn:uuid:87ae7666-ac80-4b94-88a1-4473af3a14eb>\",\"WARC-IP-Address\":\"67.207.81.229\",\"WARC-Target-URI\":\"https://daily-blog.netlify.app/questions/2164638/index.html\",\"WARC-Payload-Digest\":\"sha1:6X6TTYU75OXC4T5KOSRQSNVGL3ZJVUB2\",\"WARC-Block-Digest\":\"sha1:2HIILXOK7LH57T6JYKAKIUKBOKK7XFYX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585696.21_warc_CC-MAIN-20211023130922-20211023160922-00609.warc.gz\"}"}