4
$\begingroup$

I'm trying to plot the solution of a set of differential equations and see how the solution changes when the value of a certain variable de is changed. The equations have solutions to the values of de which I have gotten individually using NDSolve but I am unable to replicate the result for all required values of de in one single program. I code I have written is:

om = 1;
k = 1;
L = 0.001;
P = 1.3;
eqns = {
a'[t] == I*((de/om)*a[t] - (b[t] + Conjugate[b[t]])*a[t] 
-1/2) - (k/(2*om))*a[t], 
b'[t] == -I*((P*(Abs[a[t]])^2)/2 + b[t]) - (L/(2*om))*b[t], 
b[0] == 0, a[0] == 0};
s = ParametricNDSolve[eqns, {a[t], b[t]}, {t, 0, 100}, de, 
MaxSteps -> \[Infinity]]
x[de] = b[de] + Conjugate[b[de]]
Manipulate[Plot[Evaluate[x[de][t] ], {t, 0, 99}, PlotRange 
-> All], {de, 0.1, 1}]

I have tried everything that I knew in my limited knowledge of Mathematica but I couldn't get a solution. I hope someone can help me with this.

Thank you very much for your help!

$\endgroup$
6
$\begingroup$
Clear["Global`*"]

om = 1;
k = 1;
L = 1/1000;
P = 13/10;

eqns = {a'[t] == 
    I*((de/om)*a[t] - (b[t] + Conjugate[b[t]])*a[t] - 1/2) - 
        (k/(2*om))*a[t], 
   b'[t] == -I*((P*(Abs[a[t]])^2)/2 + b[t]) - (L/(2*om))*b[t], 
   b[0] == 0, a[0] == 0};

s = ParametricNDSolve[eqns, {a, b}, {t, 0, 100}, de, MaxSteps -> ∞];

x[de_?NumericQ][t_?NumericQ] = (b[de][t] + Conjugate[b[de][t]]) /. s;

Manipulate[
 Plot[x[de][t], {t, 0, 99}, PlotRange -> {-4, 4}], {de, 0.1, 1, 
  Appearance -> "Labeled"}]

enter image description here

$\endgroup$
4
$\begingroup$
om = 1;
k = 1;
L = 0.001;
P = 1.3;
eqns = {a'[t] == 
    I*((de/om)*a[t] - (b[t] + Conjugate[b[t]])*a[t] - 
        1/2) - (k/(2*om))*a[t], 
   b'[t] == -I*((P*(Abs[a[t]])^2)/2 + b[t]) - (L/(2*om))*b[t], 
   b[0] == 0, a[0] == 0};
s = ParametricNDSolve[eqns, {a[t], b[t]}, {t, 0, 100}, de, 
   MaxSteps -> \[Infinity]];
x = b[t]/. s;
Manipulate[
 Plot[x[de]+Conjugate[x[de]], {t, 0, 99}, PlotRange -> All], {de, 
  0.1, 1}]

When solving equations, Mathematica always returns solutions as substitution rules, then you have to use the /. operator to get a working function.

Also, there is no need to write [t] when calling x[de] in the Plot command, as x[*some value*] returns PInterpolatingFunction[{{0., 100.}}, <>][t], which already has the [t] argument.

$\endgroup$
4
$\begingroup$

Here is another variation that is somewhat more succinct than the other solutions.

m = 1;
k = 1;
L = 0.001;
P = 1.3;

pF = ParametricNDSolveValue[eqns, {a, b}, {t, 0, 100}, de];

Manipulate[
  With[{bF = pF[de][[2]]}, Plot[2 Re[bF[t]], {t, 0, 99}, PlotRange -> 4.1]],
  {de, .1, 1., .1, Appearance -> "Labeled"}]

demo

Notes

  • Reduce[z + Conjugate[z] == 2 Re[z], z]

    True

  • Specifying 4.1 for the plot range, fixes the y-axis scale and better demonstrates the growth of the curve as the parameter de varies.

$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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