Plotting an Equation Using ParametricNDSolve












4














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!










share|improve this question



























    4














    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!










    share|improve this question

























      4












      4








      4


      2





      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!










      share|improve this question













      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!







      differential-equations physics parametric-functions






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 23 '18 at 17:50









      Manik Kapil

      235




      235






















          3 Answers
          3






          active

          oldest

          votes


















          5














          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






          share|improve this answer































            3














            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.






            share|improve this answer





























              3














              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.







              share|improve this answer























                Your Answer





                StackExchange.ifUsing("editor", function () {
                return StackExchange.using("mathjaxEditing", function () {
                StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
                StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
                });
                });
                }, "mathjax-editing");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "387"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188350%2fplotting-an-equation-using-parametricndsolve%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                5














                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






                share|improve this answer




























                  5














                  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






                  share|improve this answer


























                    5












                    5








                    5






                    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






                    share|improve this answer














                    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







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 23 '18 at 18:52

























                    answered Dec 23 '18 at 18:46









                    Bob Hanlon

                    59.1k33595




                    59.1k33595























                        3














                        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.






                        share|improve this answer


























                          3














                          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.






                          share|improve this answer
























                            3












                            3








                            3






                            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.






                            share|improve this answer












                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 23 '18 at 19:11









                            Mat

                            813




                            813























                                3














                                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.







                                share|improve this answer




























                                  3














                                  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.







                                  share|improve this answer


























                                    3












                                    3








                                    3






                                    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.







                                    share|improve this answer














                                    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.








                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Dec 23 '18 at 21:41

























                                    answered Dec 23 '18 at 21:32









                                    m_goldberg

                                    84.3k872195




                                    84.3k872195






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Mathematica Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        Use MathJax to format equations. MathJax reference.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188350%2fplotting-an-equation-using-parametricndsolve%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        How do I know what Microsoft account the skydrive app is syncing to?

                                        When does type information flow backwards in C++?

                                        Grease: Live!