# NDSolve with varying parameter using for loop?

**URL:** <https://community.wolfram.com/t/ndsolve-with-varying-parameter-using-for-loop/11198>\
**Category:** Questions\
**Tags:** calculus, equation-solving, graphics-and-visualization, numerical-computation\
**Created:** [September 4, 2017, 10:01pm UTC](https://community.wolfram.com/t/ndsolve-with-varying-parameter-using-for-loop/11198 "2017-09-04T22:01:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_91240867](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@anon\_91240867](https://community.wolfram.com/u/anon_91240867)\
**Post date:** [September 4, 2017, 10:01pm UTC](https://community.wolfram.com/t/ndsolve-with-varying-parameter-using-for-loop/11198/1 "2017-09-04T22:01:56Z")

</div>

Hello, I have a coupled differential equation and I would like to vary the value k1 between 1000 and 1500 by an increment of 100 using for loop. Can you please help?

```
m1 = 1;
m2 = 1;
k1 = 1000;
k2 = 1000;
eom1 = m1 x1''[t] + k1 (x1[t] - x2[t]) == 0;
eom2 = m2 x2''[t] + k1 (x2[t] - x1[t]) == 0;
sol = NDSolve[{eom1, eom2, x1[0] == 0, x2[0] == 0, x1'[0] == 1, 
    x2'[0] == 0}, {x1[t], x1'[t], x2[t], x2'[t]}, {t, 0, 5}];
Plot[Evaluate[x1'[t] /. sol], {t, 0, 5}]

```

---

<div class="post-metadata">

**Author:** ![mthiel](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mthiel/32/42877_2.png) [@mthiel](https://community.wolfram.com/u/mthiel)\
**Post date:** [September 4, 2017, 11:57pm UTC](https://community.wolfram.com/t/ndsolve-with-varying-parameter-using-for-loop/11198/2 "2017-09-04T23:57:09Z")

</div>

Hi Joeseph,

what have you tried? I would have thought that the most obvious way is something like:

```
Table[
 m1 = 1;
 m2 = 1;
 k1 = loop;
 k2 = 1000;
 eom1 = m1 x1''[t] + k1 (x1[t] - x2[t]) == 0;
 eom2 = m2 x2''[t] + k1 (x2[t] - x1[t]) == 0;
 sol = NDSolve[{eom1, eom2, x1[0] == 0, x2[0] == 0, x1'[0] == 1, x2'[0] == 0}, {x1[t], x1'[t], x2[t], x2'[t]}, {t, 0, 5}];
 Plot[Evaluate[x1'[t] /. sol], {t, 0, 5}, PlotPoints -> 100], 
{loop, 1000, 1500, 100 }]

```

which seems to work just fine…?

 ![ScreenShot2017-09-05at00.56.17.jpg](https://global.discourse-cdn.com/wolfram/original/3X/e/6/e6598f598ff599b6175005721dce2413e5976c0c.jpeg)

Am I missing the point?

Best wishes,

Marco

---

<div class="post-metadata">

**Author:** ![anon\_91240867](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@anon\_91240867](https://community.wolfram.com/u/anon_91240867)\
**Post date:** [September 5, 2017, 3:26am UTC](https://community.wolfram.com/t/ndsolve-with-varying-parameter-using-for-loop/11198/3 "2017-09-05T03:26:20Z")

</div>

Dear Marco I meant using the for loop inside the NDSolve. It seems fine, but I am kinda wanting to use the for loop within the NDSolve.  
Thanks.

---

<div class="post-metadata">

**Author:** ![mthiel](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mthiel/32/42877_2.png) [@mthiel](https://community.wolfram.com/u/mthiel)\
**Post date:** [September 5, 2017, 2:08pm UTC](https://community.wolfram.com/t/ndsolve-with-varying-parameter-using-for-loop/11198/4 "2017-09-05T14:08:27Z")

</div>

Dear Joeseph,

the thing is that if you put it in a for loop it calculates but does not display everything. You can generate a list of all the results and append the images to that like so:

```
m1 = 1;
m2 = 1;
k2 = 1000;
eom1 = m1 x1''[t] + k1 (x1[t] - x2[t]) == 0;
eom2 = m2 x2''[t] + k1 (x2[t] - x1[t]) == 0; results = {}; For[
 k1 = 1000, k1 <= 1500, k1 = k1 + 100, 
 sol = NDSolve[{eom1, eom2, x1[0] == 0, x2[0] == 0, x1'[0] == 1, 
    x2'[0] == 0}, {x1[t], x1'[t], x2[t], x2'[t]}, {t, 0, 5}];
 AppendTo[results, Plot[Evaluate[x1'[t] /. sol], {t, 0, 5}, PlotPoints -> 100]]]

```

If you then evaluate “results” you obtain he same image as above. This is not elegant though. Have you thought about using the powerful ParametricNDSolve function?

```
m1 = 1;
m2 = 1;
k2 = 1000;
eom1 = m1 x1''[t] + k1 (x1[t] - x2[t]) == 0;
eom2 = m2 x2''[t] + k1 (x2[t] - x1[t]) == 0; results = {}; sol = 
 ParametricNDSolve[{eom1, eom2, x1[0] == 0, x2[0] == 0, x1'[0] == 1, 
   x2'[0] == 0}, {x1[t], x1'[t], x2[t], x2'[t]}, {t, 0, 5}, {k1}];
Manipulate[Plot[Evaluate[x1'[t][k1] /. sol], {t, 0, 5}, PlotPoints -> 100], {k1,1000, 1500}]

```

This gives a dynamics interface:

![Sep-05-201715-06-58.gif](https://global.discourse-cdn.com/wolfram/original/3X/8/0/80d9e4fd1c07abfe0d0d5d0f16def6c375032185.gif)

This here for example would give you the list of graphics in steps of 100:

```
Table[Plot[Evaluate[x1'[t][k1] /. sol], {t, 0, 5}, PlotPoints -> 100], {k1, 1000, 1500, 100}]

```

Best wishes,  
Marco
