# How to plot several plots of numerical solutions on one graph

I have a coupled first order system of differential equations which I'm solving numerically (my actual system is more complicated so I use this simple example). I'm trying to plot the solutions for different values of $$n$$ on one graph:

sol = Table[{n,
NDSolve[{y'[x] + n z[x] == 0, z'[x] - n y[x] == 0, y == 1,
z == 1}, {y, z}, {x, 0, 1}]}, {n, 1, 2}]
Plot[Evaluate[y[x] /. sol[][]], {x, 0, 1}, PlotRange -> All]
Plot[Evaluate[y[x] /. sol[][]], {x, 0, 1}, PlotRange -> All]
Plot[Table[Evaluate[y[x] /. sol[[n]][]], {n, 1, 2}], {x, 0, 1},
PlotRange -> All]


The last line doing this gives me an error "Part::pkspec1: The expression n cannot be used as a part specification." However I can still plot solutions for different $$n$$s on different graphs, as illustrated by running the previous two lines. What is going wrong and how do I fix it?

Thanks in advance for any help.

• Have a look at Dimensions[sol[][]]. You can change the last replacement to y[x] /. sol[[n]][]][]. – Natas Oct 22 '20 at 7:31

## 2 Answers

In my opinion ParametricNDSolve is the ideal command for such situation.

sol = ParametricNDSolve[{y'[x] + n z[x] == 0, z'[x] - n y[x] == 0,
y == 1, z == 1}, {y, z}, {x, 0, 1}, {n}]

Plot[Evaluate[Table[y[n][x] /. sol, {n, 1, 2}]], {x, 0, 1}, PlotRange -> All] Altenative ParametricNDSolveValue

Y = ParametricNDSolveValue[{y'[x] + n z[x] == 0, z'[x] - n y[x] == 0,y == 1, z == 1}, y, {x, 0, 1}, {n}]

Plot[Table[ Y[n][x] , {n, 1, 2}], {x, 0, 1}, Evaluated -> True] 