# Nested For loop to plot multiple plots on a single graph

**URL:** <https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019>\
**Category:** Questions\
**Tags:** equation-solving, graphics-and-visualization, numerical-computation\
**Created:** [November 18, 2019, 11:15am UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019 "2019-11-18T11:15:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![abikrg](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@abikrg](https://community.wolfram.com/u/abikrg)\
**Post date:** [November 18, 2019, 11:15am UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/1 "2019-11-18T11:15:18Z")

</div>

Please help me to plot these 8 graphs on the same figure by changing the values of eta and epsilon. I can plot it separately and use Show to draw it together, but I wanted to use loop to get the all cases on a single figure using hold on and hold off but it seems not to work.

```
    s = 2.6 10^4;
    d = 0.0026;
   \[Beta] = 3 10^(-7);
    \[Delta] = 0.5;
    c = 5;
    p = 100;
    \[Eta] = {0.00, 1.00};
    \[Epsilon] = {0.80, 0.95, 0.99, 1};
    eqn1 = {0, 0, 0, 0};
    (*Initial Conditions*)
    t0 = (c*\[Delta])/(p*\[Beta]); x0 = 2.0* 10^(6); v0 = 10^7;
    For[i = 1, j = 1; i < 3, j < 5; j++, i++; 
     eqn1[[i]] := {T'[t] == 
        s - d * T[t] - (1 - \[Eta][[i]])*\[Beta] *T [t]*V[t], 
       x'[t] == (1 - \[Eta][[i]])*\[Beta] *T[t]*V[t] - \[Delta] *x[t], 
       V'[t] == (1 - \[Epsilon][[j]])*p *x[t] - c *V[t], T[0] == t0, 
       x[0] == x0, V[0] == v0}; 
     soln11[[i]] = NDSolve[eqn1[[i]], {T, x, V}, {t, 0, 14}]; 
     p1 = LogPlot[
       Evaluate[{V[t]} /. First@soln11[[i]], {t, 0, 14}, 
        PlotRange -> {100, 10^8}, PlotPoints -> 200, Frame -> True, 
        PlotStyle -> {Orange, Thick}]]

```

[neumannwidloop.nb](https://community.wolfram.com/uploads/short-url/nNfKdpCMiSg39vWZfgXjZ8Xt4Jk.nb) (7.33 KB)

---

<div class="post-metadata">

**Author:** ![anon21378091](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@anon21378091](https://community.wolfram.com/u/anon21378091)\
**Post date:** [November 18, 2019, 3:24pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/2 "2019-11-18T15:24:30Z")

</div>

s = 2.6 10^4;(_cells per ml per day)_)  
d = 0.0026;(_day^(-1)_)  
[Beta] = 3 10^(-7);(_(Virion per mililiter)^(-1)_)  
[Delta] = 0.5;(_day^(-1)_)  
c = 5;(_day^(-1)_)  
p = 100;(_virions per ml per cell per day_)  
[Eta] = {0.00, 1.00};  
[Epsilon] = {0.80, 0.95, 0.99, 1};  
eqn1 = {0, 0, 0, 0};  
(_Initial Conditions_)  
t0 = (c\*[Delta])/(p\*[Beta]); x0 = 2.0\* 10^(6); v0 = 10^7;  
For[i = 1, j = 1; i \< 3, j \< 5; j++, i++;  
eqn1[[i]] := {T’[t] ==  
s - d \* T[t] - (1 - [Eta][[i]])\*[Beta] \*T [t]_V[t],  
x’[t] == (1 - [Eta][[i]])_[Beta] \*T[t]\*V[t] - [Delta] \*x[t],  
V’[t] == (1 - [Epsilon][[j]])\*p \*x[t] - c \*V[t], T[0] == t0,  
x[0] == x0, V[0] == v0};  
soln11[[i]] = NDSolve[eqn1[[i]], {T, x, V}, {t, 0, 14}];  
p1 = LogPlot[  
Evaluate[{V[t]} /. First@soln11[[i]], {t, 0, 14},  
PlotRange → {100, 10^8}, PlotPoints → 200, Frame → True,  
PlotStyle → {Orange, Thick}]]

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [November 19, 2019, 9:49am UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/3 "2019-11-19T09:49:25Z")

</div>

For reasons unknown to me the above code didn’t work.  
I do not understand why the i-loop starts with 2 and j doesn’t reach 4:

```
For[i = 1, j = 1; i < 3, j < 5; j++, i++;
  Print[" i ", i];
  Print[" j ", j]
  ];

```

Here it does

```
For[i = 1, i < 3, i++,
 For[j = 1, j < 5, j++,
  Print[" i ", i];
  Print[" j ", j]
  ]
 ]

```

But what about this (without For…)?

```
s = 2.6 10^4;(*cells per ml per day)*)
d = 0.0026;(*day^(-1)*)
\[Beta] = 3 10^(-7);(*(Virion per mililiter)^(-1)*)
\[Delta] =0.5;(*day^(-1)*)
c = 5;(*day^(-1)*)
p = 100;(*virions per ml per cell per day*)
\[Eta] = {0.00, 1.00};
\[Epsilon] = {0.80, 0.95, 0.99, 1};
       (*Initial Conditions*)
t0 = (c*\[Delta])/(p*\[Beta]); x0 = 2.0*10^(6); v0 = 10^7;

eqn1a[i_, j_] := {T'[t] == s - d*T[t] - (1 - \[Eta][[i]])*\[Beta]*T[t]*V[t], 
  x'[t] == (1 - \[Eta][[i]])*\[Beta]*T[t]*V[t] - \[Delta]*x[t], 
  V'[t] == (1 - \[Epsilon][[j]])*p*x[t] - c*V[t], T[0] == t0, 
  x[0] == x0, V[0] == v0}

soln11[eq_] := NDSolve[eq, {T, x, V}, {t, 0, 14}]

tt = Flatten[Table[eqn1a[i, j], {i, 2}, {j, 4}], 1];
sol = Flatten[soln11 /@ tt];
solv = (V /. #)[t] & /@ Select[sol, MemberQ[#, V] &];

LogPlot[solv, {t, 0, 14}, PlotRange -> {100, 10^8}, PlotPoints -> 200,
  Frame -> True, PlotStyle -> {Orange, Thick}]

```

Similarly

```
solx = (x /. #)[t] & /@ Select[sol, MemberQ[#, x] &];    
solT = (T /. #)[t] & /@ Select[sol, MemberQ[#, T] &];

```

---

<div class="post-metadata">

**Author:** ![abikrg](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@abikrg](https://community.wolfram.com/u/abikrg)\
**Post date:** [November 19, 2019, 10:57am UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/4 "2019-11-19T10:57:41Z")

</div>

Thank you Sir!! It seems working.

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [November 19, 2019, 3:18pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/5 "2019-11-19T15:18:42Z")

</div>

Hi Hans

I do not understand why the i-loop starts with 2 and j doesn’t reach 4.

In WL the role of `;` and `,` is reversed relative to languages like C.

```
For[i = 1; j = 1, i < 3, j++; i++,
  Print[" i ", i];
  Print[" j ", j]];

```

As you suggested, `Table` is a much better option that `For`.

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [November 19, 2019, 4:16pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/6 "2019-11-19T16:16:32Z")

</div>

> “In WL the role of ; and , is reversed relative to languages like C.”

OK, but

```
For[i = 1; j = 1, i < 3, j++; i++, Print[" i ", i];
  Print[" j ", j]];

 i 1    
 j 1    
 i 2    
 j 2

```

Obviously you miss the pairs i = 1, j = 2 and so on, and j never reaches 4. But I think Abiy wanted all combinations of i and j. Seems this construct isn’t appropriate for what he intended.

Look at

```
For[i = 1; j = 1, i < 3; j < 5, j++; i++, Print[{i, j}]];

{1,1}

{2,2}

{3,3}

{4,4}

```

which is obviously equivalent to

```
For[i = 1; j = 1, Or[i < 3, j < 5], j++; i++, Print[{i, j}]];

{1,1}

{2,2}

{3,3}

{4,4}

```

then, and this is according to what is said in the Help-Section : _The sequence of evaluation is test, body, incr ._

So  
i and j are set to 1, the test gives True , and i AND j are incremented and so on, until the test yields False. Try setting j to 3 and substitute j\<5 with j \< 10 for example.

---

<div class="post-metadata">

**Author:** ![abikrg](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@abikrg](https://community.wolfram.com/u/abikrg)\
**Post date:** [November 19, 2019, 6:00pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/7 "2019-11-19T18:00:02Z")

</div>

I want to have a dashed lines for eta=1 and thick for eta=0 like on the code below. But without the for loop, it becomes redundant. So Can you please suggest a better way?

```
  s = 2.6 10^4;
d = 0.0026;
\[Beta] =3 *10^(-7);\[Delta] = \
        0.5;c = 5;p = 100;\[Eta] = {0.00, 1.00};
        \[Epsilon] = {0.80, 0.95, 0.99, 1};
        (*Initial Conditions*)
        t0 = (c*\[Delta])/(p*\[Beta]); x0 = 2.0*10^(6); v0 = 10^7;
        
        eqn1a[i_, 
          j_] := {T'[t] == s - d*T[t] - (1 - \[Eta][[i]])*\[Beta]*T[t]*V[t], 
          x'[t] == (1 - \[Eta][[i]])*\[Beta]*T[t]*V[t] - \[Delta]*x[t], 
          V'[t] == (1 - \[Epsilon][[j]])*p*x[t] - c*V[t], T[0] == t0, 
          x[0] == x0, V[0] == v0}
        
        soln11[eq_] := NDSolve[eq, {T, x, V}, {t, 0, 14}]
        
        tt1 = Flatten[Table[eqn1a[i, j], {i, 1}, {j, 4}], 1];
        sol1 = Flatten[soln11 /@ tt1];
        solv1 = (V /. #)[t] & /@ Select[sol1, MemberQ[#, V] &];
        
        pfig1 = LogPlot[solv1, {t, 0, 14}, PlotRange -> {3*100, 2*10^7}, 
           PlotPoints -> 200, Frame -> True, PlotStyle -> {Orange, Thick}];
        soln12[eq_] := NDSolve[eq, {T, x, V}, {t, 0, 14}]
        
        tt2 = Flatten[Table[eqn1a[i, j], {i = 2}, {j, 4}], 1];
        sol2 = Flatten[soln12 /@ tt2];
        solv2 = (V /. #)[t] & /@ Select[sol2, MemberQ[#, V] &];
        
        pfig2 = LogPlot[solv2, {t, 0, 14}, PlotRange -> {3*100, 2*10^7}, 
           PlotPoints -> 200, Frame -> True, PlotStyle -> {Blue, Dashed}];
        Show[pfig1, pfig2]

```

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [November 19, 2019, 11:10pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/8 "2019-11-19T23:10:45Z")

</div>

Do you mean this? (Note that the first four functions are for eta = 0 )

```
s = 2.6 10^4;(*cells per ml per day)*)d = 0.0026;(*day^(-1)*)\[Beta] =
  3 10^(-7);(*(Virion per mililiter)^(-1)*)\[Delta] = \
0.5;(*day^(-1)*)c = 5;(*day^(-1)*)p = 100;(*virions per ml per cell \
per day*)\[Eta] = {0.00, 1.00};
\[Epsilon] = {0.80, 0.95, 0.99, 1};
(*Initial Conditions*)
t0 = (c*\[Delta])/(p*\[Beta]); x0 = 2.0*10^(6); v0 = 10^7;

eqn1a[i_, 
  j_] := {T'[t] == s - d*T[t] - (1 - \[Eta][[i]])*\[Beta]*T[t]*V[t], 
  x'[t] == (1 - \[Eta][[i]])*\[Beta]*T[t]*V[t] - \[Delta]*x[t], 
  V'[t] == (1 - \[Epsilon][[j]])*p*x[t] - c*V[t], T[0] == t0, 
  x[0] == x0, V[0] == v0}

soln11[eq_] := NDSolve[eq, {T, x, V}, {t, 0, 14}]

tt = Flatten[Table[eqn1a[i, j], {i, 2}, {j, 4}], 1];
sol = Flatten[soln11 /@ tt];
solv = (V /. #)[t] & /@ Select[sol, MemberQ[#, V] &];

p1 = LogPlot[solv, {t, 0, 14}, PlotRange -> {100, 10^7}, 
  PlotPoints -> 200, Frame -> True, 
  PlotStyle -> Table[If[i <= 4, Thick, Dashed], {i, 8}]]

```

---

<div class="post-metadata">

**Author:** ![abikrg](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@abikrg](https://community.wolfram.com/u/abikrg)\
**Post date:** [November 20, 2019, 4:13am UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/9 "2019-11-20T04:13:33Z")

</div>

Yes Sir you understood me well, but the above code makes all 8 plots dashed, no thick line (just changed the thick lines of the first code by dashed, I want it to be mixed: first four thick and the rest Dashed).

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [November 20, 2019, 10:47am UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/10 "2019-11-20T10:47:49Z")

</div>

In fact, I am afraid that I don’t understand you. What do you mean?  
And what do you say considering (in my code)

```
p1 = LogPlot[solv, {t, 0, 14}, PlotRange -> {100, 10^7}, 
  PlotPoints -> 200, Frame -> True, 
  PlotStyle -> Table[If[i <= 4, {Red, Thickness[.01]}, {Blue, Thickness[.008], Dashed}], {i, 8}]]

```

You might as well want to look at

first four functions:

```
p2 = LogPlot[Evaluate[Join[Take[solv, 4], {1, 1, 1, 1}]], {t, 0, 14},
PlotRange -> {100, 10^7}, PlotPoints -> 200, Frame -> True, 
PlotStyle -> Table[If[i <= 4, {Red, Thickness[.01]}, {Blue, Thickness[.005], Dashed}], {i, 8}]]

```

last four functions

```
p3 = LogPlot[Evaluate[Join[{1, 1, 1, 1}, Take[solv, -4]]], {t, 0, 14},
   PlotRange -> {100, 10^7}, PlotPoints -> 200, Frame -> True, 
  PlotStyle -> Table[If[i <= 4, {Red, Thickness[.01]}, {Blue, Thickness[.008], Dashed}], {i, 8}]]

```

---

<div class="post-metadata">

**Author:** ![abikrg](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@abikrg](https://community.wolfram.com/u/abikrg)\
**Post date:** [December 2, 2019, 3:47pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/11 "2019-12-02T15:47:06Z")

</div>

I would like to plot V and VNI together as V+VNI and the code below is not helping. Mr. Hans Dolhaine, May I get the usual help please?

```
  s = 2.6 10^4;
      d = 0.0026;
      \[Beta] = 2.25 10^(-7);
      \[Delta] = 1;
      c = 6;
      p = 2.9;  
    \[Epsilon] = {0.96, 0.5};
        \[Rho] = {0, 1};
        eqn1g[i_, j_] := {x'[t] == \[Beta] *T*V[t] - \[Delta] *x[t], 
           V'[t] == (1 - \[Rho][[i]]) (1 - \[Epsilon][[j]])*p *x[t] - 
             c *V[t], 
           VNI'[t] == \[Rho][[i]]*(1 - \[Epsilon][[j]])*p *x[t] - c *VNI[t], 
           x[0] == x0, V[0] == v0, VNI[0] == 0};
        
        soln112[eq_] := NDSolve[eq, {x, V, VNI}, {t, 0, 14}];
        tableway1 = Flatten[Table[eqn1g[i, j], {i, 2}, {j, 2}], 1];
        Solsn1 = Flatten[soln112 /@ tableway];
        Solv1 = (V /. #)[t] & /@ Select[Solsn1, MemberQ[#, V] &];
        Solv2 = (VNI /. #)[t] & /@ Select[Solsn1, MemberQ[#, VNI] &];
        p3 = LogPlot[
          Evaluate[Join[Take[Solv1 + Solv2, 4], {1, 1, 1, 1}]], {t, 0, 14}, 
          PlotRange -> {100, 10^7}, PlotPoints -> 200, Frame -> True, 
          FrameLabel -> {"Time" , "copies "}]

```

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [December 2, 2019, 7:05pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/12 "2019-12-02T19:05:39Z")

</div>

Good evening Abiy,

You have a typo in your code:

> `Solsn1 = Flatten[soln112 /@ tableway];`

there you should refer to

```
tableway1

```

as defined further above. Moreover you have to define values for v0, x0 and T. I guess T means Temperature and I assigned a value of 273.

With these modifications I arrive at a code which gives a plot. Is it this what you meant?

```
s = 2.6 10^4;
d = 0.0026;
\[Beta] = 2.25 10^(-7);
\[Delta] = 1;
c = 6;
p = 2.9;
\[Epsilon] = {0.96, 0.5};
\[Rho] = {0, 1};
v0 = 10^7;
x0 = 2. 10^6;
T = 273;
eqn1g[i_, j_] := {x'[t] == \[Beta]*T*V[t] - \[Delta]*x[t], 
   V'[t] == (1 - \[Rho][[i]]) (1 - \[Epsilon][[j]])*p*x[t] - c*V[t], 
   VNI'[t] == \[Rho][[i]]*(1 - \[Epsilon][[j]])*p*x[t] - c*VNI[t], 
   x[0] == x0, V[0] == v0, VNI[0] == 0};

soln112[eq_] := NDSolve[eq, {x, V, VNI}, {t, 0, 14}];

tableway1 = Flatten[Table[eqn1g[i, j], {i, 2}, {j, 2}], 1];
Solsn1 = Flatten[soln112 /@ tableway1];

Solv1 = (V /. #)[t] & /@ Select[Solsn1, MemberQ[#, V] &];
Solv2 = (VNI /. #)[t] & /@ Select[Solsn1, MemberQ[#, VNI] &];
p3 = LogPlot[
  Evaluate[Join[Take[Solv1 + Solv2, 4], {1, 1, 1, 1}]], {t, 0, 14}, 
  PlotRange -> {100, 10^7}, PlotPoints -> 200, Frame -> True, 
  FrameLabel -> {"Time", "copies "}]

```

---

<div class="post-metadata">

**Author:** ![abikrg](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@abikrg](https://community.wolfram.com/u/abikrg)\
**Post date:** [December 3, 2019, 6:32am UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/13 "2019-12-03T06:32:48Z")

</div>

Sure correcting the typo error solved everything. Thank you for you quick and unreserved help. U are helping me get better with Mathematica everyday. Big respect SIR!!

---

<div class="post-metadata">

**Author:** ![abikrg](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@abikrg](https://community.wolfram.com/u/abikrg)\
**Post date:** [December 9, 2019, 5:54pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/14 "2019-12-09T17:54:51Z")

</div>

I want to have the y-axis ticks at {0,0.2,0.4,0.6,0.8,1, 1.2,1.4, 1.6} instead of in {0,10^(-4), 0.001, 0.01, 0.1,1}. Please see the plot of the code below and help me in improving it by plotting the y-axis in step size of 0.2. Both Ticks and Frame Ticks were not able to help me.

```
 k = 6.7 *10^-8; c = 
         2*10^6; \[Alpha] = 1.34; \[Lambda] = 3.3; \[Mu] = 2; X = 10^6;
        r = 0.17*10^6;
        S = 0.83 *10^6;
        P = 10^7;
        a = \[Alpha]/(k*c);
         l = \[Lambda]/(k*c);
        m = \[Mu]/(k*c);
        s1 = S/c; R1 = r/c; p1 = P/c;
        b = 15;
    (*Initial Conditions*)
    
    x0 = 0.3; y0 = 0.2; z0 = 5;
    
    (*Differential Equations*)
    
    rhs1[x_, y_, z_] := x (a (1 - (x + y)) - z);
    rhs2[x_, y_, z_] := -l y + x z;
    rhs3[x_, y_, z_] := -z x + b l y - m z;

   detstab := {x'[t] == rhs1[x[t], y[t], z[t]], 
       y'[t] == rhs2[x[t], y[t], z[t]], z'[t] == rhs3[x[t], y[t], z[t]], 
       x[0] == x0, y[0] == y0, z[0] == z0};
    soln11 = NDSolve[detstab, {x, y, z}, {t, 0, 10}];
    LogPlot[Evaluate[{x[t], y[t], z[t]} /. First@soln11], {t, 0, 10}, 
     PlotRange -> {{0, 10}, {0, 1.2}}, 
     Ticks -> {Table[i + 2, {i, 0, 10}], Table[i + 0.2, {i, 0, 1}]}, 
     Frame -> True]

```

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [December 9, 2019, 6:16pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/15 "2019-12-09T18:16:08Z")

</div>

Hi Abiy,

In a `LogPlot` the `y` axis is always logarithmic. You could plot the `Log` of the function on a linear `y` scale. e.g.

```
Plot[Evaluate[Log@{x[t], y[t], z[t]} /. First@soln11], {t, 0, 10}, 
 Frame -> True, PlotLegends -> {"x", "y", "z"}]

```

 ![9333plot.png](https://global.discourse-cdn.com/wolfram/original/3X/f/e/fecf3d6c3f06c2146222f3d7adf2122bd5cfe43d.png)

Or a linear plot with a specific range / ticks

```
Plot[Evaluate[{x[t], y[t], z[t]} /. First@soln11], {t, 0, 10},
 PlotRange -> {0, 1.6},
 Frame -> True,
 FrameTicks -> {{Range[0, 1.6, .2], None}, {Automatic, None}},
 PlotLegends -> {"x", "y", "z"}]

```

 ![9601plot.png](https://global.discourse-cdn.com/wolfram/original/3X/6/8/6814f90cf3a2cfc2f331af5e3d0a834f2b1dd630.png)

---

<div class="post-metadata">

**Author:** ![abikrg](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@abikrg](https://community.wolfram.com/u/abikrg)\
**Post date:** [December 9, 2019, 7:32pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/16 "2019-12-09T19:32:04Z")

</div>

Dear Rohit Namjoshi, thank you, this is what I wanted. However, I have one more question, as you can see below I have been using LogPlot and the y-axis keeps giving me logarithmic values but what I need is to plot the Log of the function on a linear y scale, as your first suggestion, I tried to improve the code but keeps giving me a wrong result. So will you help in changing the codes below to plot the Log of the function instead of using Logplot.

```
s = 2.6 10^4;
d = 0.0026;
\[Beta] = 2.25 10^(-7);
\[Delta] = 1;
c = 6;
p = 2.9;
\[Epsilon] = {0.96, 0.5};
\[Rho] = {0, 1};
v0 = 10^7;
x0 = 2. 10^6;
T = 273;
eqn1g[i_, j_] := {x'[t] == \[Beta]*T*V[t] - \[Delta]*x[t], 
   V'[t] == (1 - \[Rho][[i]]) (1 - \[Epsilon][[j]])*p*x[t] - c*V[t], 
   VNI'[t] == \[Rho][[i]]*(1 - \[Epsilon][[j]])*p*x[t] - c*VNI[t], 
   x[0] == x0, V[0] == v0, VNI[0] == 0};

soln112[eq_] := NDSolve[eq, {x, V, VNI}, {t, 0, 14}];

tableway1 = Flatten[Table[eqn1g[i, j], {i, 2}, {j, 2}], 1];
Solsn1 = Flatten[soln112 /@ tableway1];

Solv1 = (V /. #)[t] & /@ Select[Solsn1, MemberQ[#, V] &];
Solv2 = (VNI /. #)[t] & /@ Select[Solsn1, MemberQ[#, VNI] &];
p3 = LogPlot[
  Evaluate[Join[Take[Solv1 + Solv2, 4], {1, 1, 1, 1}]], {t, 0, 14}, 
  PlotRange -> {100, 10^7}, PlotPoints -> 200, Frame -> True, 
  FrameLabel -> {"Time", "copies "}]

```

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [December 9, 2019, 11:53pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/17 "2019-12-09T23:53:02Z")

</div>

Hi Abiy,

What is the reason for joining `{1, 1, 1, 1}` to the list of plots? It is just going to plot a straight line at `y = 1`. Is this what you are looking for?

```
p3 = Plot[Evaluate[Log@Take[Solv1 + Solv2, 4]], {t, 0, 14},
  PlotRange -> Log@{100, 10^7},
  PlotPoints -> 200,
  Frame -> True,
  FrameLabel -> {"Time", "copies "}]

```

 ![7032plot.png](https://global.discourse-cdn.com/wolfram/original/3X/e/9/e933307b8adc558fc4c8a84a7b03d5471d654189.png)

---

<div class="post-metadata">

**Author:** ![abikrg](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@abikrg](https://community.wolfram.com/u/abikrg)\
**Post date:** [December 10, 2019, 5:22am UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/18 "2019-12-10T05:22:25Z")

</div>

I appreciate the help. Sure joining is not important !!

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [December 10, 2019, 1:13pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/19 "2019-12-10T13:13:43Z")

</div>

@Rohit: I introduced

```
Join[Take[solv, 4], {1, 1, 1, 1}]

```

in a post far above to have easy acces to the “dashed lines” by then using (see p3 far above)

```
Join[{1,1,1,1},Take[solv, - 4]]

```

avoiding to have to change (which of course could be done as well)

```
PlotStyle -> Table[If[i <= 4, {Red, Thickness[.01]}, {Blue, Thickness[.008], Dashed}], {i, 8}]]

```

and knowing that Log[1] == 0 will coincide with the x-axis and not disturb the plot.

Abyi wanted to have thick and dashed lines.

It seems that this is not necessary later on.

@Abiy : By the way, you said

> I would like to plot V and VNI together as V+VNI

Look at your equations and initial values (with the input in the code above):

```
In[40]:= eee = Flatten[Table[eqn1g[i, j], {i, 2}, {j, 2}]];
Select[eee, MemberQ[#, VNI'[t]] &]
Select[eee, MemberQ[#, VNI[0]] &]

Out[41]= {
\!\(\*SuperscriptBox["VNI", "\[Prime]",
MultilineFunction->None]\)[t] == -6 VNI[t], 
\!\(\*SuperscriptBox["VNI", "\[Prime]",
MultilineFunction->None]\)[t] == -6 VNI[t], 
\!\(\*SuperscriptBox["VNI", "\[Prime]",
MultilineFunction->None]\)[t] == -6 VNI[t] + 0.116 x[t], 
\!\(\*SuperscriptBox["VNI", "\[Prime]",
MultilineFunction->None]\)[t] == -6 VNI[t] + 1.45 x[t]}

Out[42]= {VNI[0] == 0, VNI[0] == 0, VNI[0] == 0, VNI[0] == 0}

```

The first two VNI can be integrated immediately giving

```
VNI[t_] := VNI[0] Exp[- 6 t]

```

and with

VNI[0] == 0

this is always zero (check it with the results of NDSolve).

A similar direct integration is possible for the last two V’s

---

<div class="post-metadata">

**Author:** ![abikrg](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@abikrg](https://community.wolfram.com/u/abikrg)\
**Post date:** [December 10, 2019, 4:44pm UTC](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019/20 "2019-12-10T16:44:04Z")

</div>

Dear Hans Dolhaine, you are right that I needed it at the beginning not to repeat the same code for different plots( dashed(p2) and thick(p3)) using an if loop(which can easily be applied on Matlab). But the if loop were not able to give me the plot I want(had the if loop were working p2 would have been enough to plot all and p3 was not needed). Thus I finally decided to ignore the join as Rohit suggested and plot thick and dashed lines on different codes as below. I honestly thank both of you for being a big help in my Mathematica coding. If possible, help me to keep p2 and p3 in a single code (i=1(j=1:4)thick lines and i=2(j=1:4) dashed lines) using if loop than being redundant and not to use the Show function. Here is the code for both to see and if possible improve.  
Kind regards. (Thank you again!!)

```
s = 2.6*10^4;
d = 0.0026;
\[Beta] = 3 *10^(-7);
\[Delta] = {0.5, 5};
c = 5;
p = {5, 100};

\[Epsilon] = {0.00, 0.00};
\[Eta] = {0.80, 0.95, 0.99, 1};

\[Eta]a = {0.00, 1.00};
\[Epsilon]a = {0.80, 0.95, 0.99, 1};
(*Initial Conditions*)
t0 = {(c*\[Delta][[2]])/(p[[2]]*\[Beta]), (c*\[Delta][[1]])/(p[[
       2]]*\[Beta])};
x0 = 2.0*10^(6); v0 = 4.0*10^7;
(* SDES for p=100, delta=0.5 for fig A*)
eqn1a[i_, 
  j_] := {T'[t] == s - d*T[t] - (1 - \[Eta]a[[i]])*\[Beta]*T[t]*V[t], 
  x'[t] == (1 - \[Eta]a[[i]])*\[Beta]*T[t]*V[t] - \[Delta][[1]]*x[t], 
  V'[t] == (1 - \[Epsilon]a[[j]])*p[[2]]*x[t] - c*V[t], 
  T[0] == t0[[2]], x[0] == x0, V[0] == v0}

 soln11[eq_] := NDSolve[eq, {T, x, V}, {t, 0, 14}]

(* Solving the SDES *) 
tt2 = Flatten[Table[eqn1a[i, j], {i, 2}, {j, 4}], 1];
sol2 = Flatten[soln11 /@ tt2];
solv2 = (V /. #)[t] & /@ Select[sol2, MemberQ[#, V] &];

p2 = Plot[Evaluate[Log10@Take[solv2, 4]], {t, 0, 14}, 
   PlotRange -> {2.6, 8}, Frame -> True, 
   FrameLabel -> {Days , "Simulated Decrease Log10 HCV"},
   PlotStyle -> Table[If[i <= 4, {Red, Thin}, {Blue, "--"}], {i, 8}]];
(*Plot B eta=0 *)
p3 = Plot[Evaluate[Log10@Take[solv2, -4]], {t, 0, 14}, 
   PlotRange -> {2.6, 8}, Frame -> True, 
   FrameLabel -> {Days , "Simulated Decrease Log10 HCV"}, 
   PlotStyle -> Table[If[i <= 4, {Blue, Dashed}], {i, 8}]];
(* Draw together*)
p41 = Show[p2, p3]

```

[Next page](https://community.wolfram.com/t/nested-for-loop-to-plot-multiple-plots-on-a-single-graph/15019.md?page=2)
