2
$\begingroup$

I have a coupled differential eqations:

eq1[z_] := y1'[z] == c1 y1[z] + c2 y2[z];
eq2[z_] := y2'[z] == c1 y1[z] - c2 y2[z];
boundary = {y1[0] == 1, y2[0] == 0};
var = {y1, y2};

where c1 and c2 are constants.

To solve this I generally use the following codes:

eq[x1_, x2_, z_] := Flatten[{eq1[x1, x2, z], eq2[x1, x2, z], boundry}];
Sol[x1_, x2_] := NDSolve[eq[x1, x2, z], var, {z, 0, 5}];

Now if there is a table of c1 and c2 values. Lets say: v={{2.65691, 2.26358}, {3.84167, 2.44838}, {3.08054,2.81354}, {3.15585,4.45893}, {1.13949, 3.25856}}. I want to solve these above set of equations using these c1 and c2 values. My query is how can I include these c1,c2 values in NDSolve to get the solutions for each set of {c1,c2}?

$\endgroup$
3
$\begingroup$

You can use ParametricNDSolveValue or ParametricNDSolve

sols = ParametricNDSolveValue[{y1'[z] == c1*y1[z] + c2*y2[z], 
    y2'[z] == c1*y1[z] - c2*y2[z], y1[0] == 1, y2[0] == 0}, {y1, 
    y2}, {z, 0, 5}, {c1, c2}];
v = {{2.65691, 2.26358}, {3.84167, 2.44838}, {3.08054, 
    2.81354}, {3.15585, 4.45893}, {10.13949, 30.25856}};
Table[Through@sols[Sequence @@ index][z], {index, v}]
Table[ParametricPlot[
  Evaluate[Through@sols[Sequence @@ index][z]], {z, 0, 5}], {index, 
  v}]

OP

Your approach is also work.

eq1[c1_, c2_, z_] := y1'[z] == c1 y1[z] + c2 y2[z];
eq2[c1_, c2_, z_] := y2'[z] == c1 y1[z] - c2 y2[z];
boundary = {y1[0] == 1, y2[0] == 0};
var = {y1, y2};
eq[c1_, c2_, z_] := 
  Flatten[{eq1[c1, c2, z], eq2[c1, c2, z], boundary}];
Sol[c1_, c2_] := NDSolve[eq[c1, c2, z], var, {z, 0, 5}];
v = {{2.65691, 2.26358}, {3.84167, 2.44838}, {3.08054, 
    2.81354}, {3.15585, 4.45893}, {1.13949, 3.25856}};
Table[Sol[Sequence @@ index], {index, v}]
(* Sol[Sequence @@ #] & /@ v *)
$\endgroup$
2
$\begingroup$

More an extended comment on the answer (+1) by cvgmt than a distinct answer. Instead of Table[Through@sols[Sequence @@ ... use

s = sols @@@ v;
ParametricPlot[Evaluate@Map[#[z] &, s, {2}], {z, 0, 5}]

to obtain much more compact code. Replacing {y1, y2} by {y1[z], y2[z]} in ParametricNDSolveValue further reduces the code to

s = sols @@@ v;
ParametricPlot[s, {z, 0, 5}]

Formatting the plot,

vs = ToString /@ v;
ParametricPlot[s, {z, 0, 5}, ImageSize -> Large, LabelStyle -> {15, Black, Bold}, 
  PlotLegends -> Placed[LineLegend[vs, LabelStyle -> {Bold, 12}], {.2, .7}]]

yields

enter image description here

The first curve ends at about {5 10^6, 2 10^6] and so cannot be seen on this plot.

$\endgroup$
2
  • $\begingroup$ Thanks your code ! $\endgroup$
    – cvgmt
    Nov 28 '20 at 23:07
  • $\begingroup$ Thank you, @bbgodfrey. This code is very compact. $\endgroup$ Nov 29 '20 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.