Explicit Euler method












1














I am looking to find the explicit and implicit codes for Euler's method. In my search I am across the following code, I am wondering if this code is explicit? if not how could I change it to make it so?



import math
# First Order ODE (y' = f(x, y)) Solver using Euler method
# xa: initial value of independent variable
# xb: final value of independent variable
# ya: initial value of dependent variable
# n : number of steps (higher the better)
# Returns value of y at xb.
def Euler(f, xa, xb, ya, n):
h = (xb - xa) / float(n)
x = xa
y = ya
for i in range(n):
y += h * f(x, y)
x += h
return y
print(Euler(lambda x, y: math.cos(x) + math.sin(y), 0, 1, 1, 1000))









share|cite|improve this question





























    1














    I am looking to find the explicit and implicit codes for Euler's method. In my search I am across the following code, I am wondering if this code is explicit? if not how could I change it to make it so?



    import math
    # First Order ODE (y' = f(x, y)) Solver using Euler method
    # xa: initial value of independent variable
    # xb: final value of independent variable
    # ya: initial value of dependent variable
    # n : number of steps (higher the better)
    # Returns value of y at xb.
    def Euler(f, xa, xb, ya, n):
    h = (xb - xa) / float(n)
    x = xa
    y = ya
    for i in range(n):
    y += h * f(x, y)
    x += h
    return y
    print(Euler(lambda x, y: math.cos(x) + math.sin(y), 0, 1, 1, 1000))









    share|cite|improve this question



























      1












      1








      1







      I am looking to find the explicit and implicit codes for Euler's method. In my search I am across the following code, I am wondering if this code is explicit? if not how could I change it to make it so?



      import math
      # First Order ODE (y' = f(x, y)) Solver using Euler method
      # xa: initial value of independent variable
      # xb: final value of independent variable
      # ya: initial value of dependent variable
      # n : number of steps (higher the better)
      # Returns value of y at xb.
      def Euler(f, xa, xb, ya, n):
      h = (xb - xa) / float(n)
      x = xa
      y = ya
      for i in range(n):
      y += h * f(x, y)
      x += h
      return y
      print(Euler(lambda x, y: math.cos(x) + math.sin(y), 0, 1, 1, 1000))









      share|cite|improve this question















      I am looking to find the explicit and implicit codes for Euler's method. In my search I am across the following code, I am wondering if this code is explicit? if not how could I change it to make it so?



      import math
      # First Order ODE (y' = f(x, y)) Solver using Euler method
      # xa: initial value of independent variable
      # xb: final value of independent variable
      # ya: initial value of dependent variable
      # n : number of steps (higher the better)
      # Returns value of y at xb.
      def Euler(f, xa, xb, ya, n):
      h = (xb - xa) / float(n)
      x = xa
      y = ya
      for i in range(n):
      y += h * f(x, y)
      x += h
      return y
      print(Euler(lambda x, y: math.cos(x) + math.sin(y), 0, 1, 1, 1000))






      linear-algebra numerical-methods






      share|cite|improve this question















      share|cite|improve this question













      share|cite|improve this question




      share|cite|improve this question








      edited Dec 4 '18 at 22:04









      Moo

      5,53131020




      5,53131020










      asked Dec 4 '18 at 21:41









      user104user104

      1077




      1077






















          1 Answer
          1






          active

          oldest

          votes


















          3














          This code implements the explicit Euler scheme.



          In order to be implicit, there would be an instruction like



          y = solve(z == y + h*f(x,z), z)


          in the inner loop, where solve(equation, variable) is a suitable function to solve an equation (see here).






          share|cite|improve this answer























          • how would I add a step size to this code?
            – user104
            Dec 4 '18 at 21:52










          • The step size is h, which is currently controlled by the argument n
            – Federico
            Dec 4 '18 at 21:54










          • so what do I need to replace that line of code you have in my code?
            – user104
            Dec 4 '18 at 22:03










          • You need to implement the code that finds the fixed point in the implicit Euler scheme. You can either use a fixed-point iteration, or a generic nonlinear solver, such as docs.scipy.org/doc/scipy/reference/optimize.html#root-finding
            – Federico
            Dec 4 '18 at 22:06










          • either way, thanks for your help!
            – user104
            Dec 4 '18 at 22:07











          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: "69"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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
          },
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3026217%2fexplicit-euler-method%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          This code implements the explicit Euler scheme.



          In order to be implicit, there would be an instruction like



          y = solve(z == y + h*f(x,z), z)


          in the inner loop, where solve(equation, variable) is a suitable function to solve an equation (see here).






          share|cite|improve this answer























          • how would I add a step size to this code?
            – user104
            Dec 4 '18 at 21:52










          • The step size is h, which is currently controlled by the argument n
            – Federico
            Dec 4 '18 at 21:54










          • so what do I need to replace that line of code you have in my code?
            – user104
            Dec 4 '18 at 22:03










          • You need to implement the code that finds the fixed point in the implicit Euler scheme. You can either use a fixed-point iteration, or a generic nonlinear solver, such as docs.scipy.org/doc/scipy/reference/optimize.html#root-finding
            – Federico
            Dec 4 '18 at 22:06










          • either way, thanks for your help!
            – user104
            Dec 4 '18 at 22:07
















          3














          This code implements the explicit Euler scheme.



          In order to be implicit, there would be an instruction like



          y = solve(z == y + h*f(x,z), z)


          in the inner loop, where solve(equation, variable) is a suitable function to solve an equation (see here).






          share|cite|improve this answer























          • how would I add a step size to this code?
            – user104
            Dec 4 '18 at 21:52










          • The step size is h, which is currently controlled by the argument n
            – Federico
            Dec 4 '18 at 21:54










          • so what do I need to replace that line of code you have in my code?
            – user104
            Dec 4 '18 at 22:03










          • You need to implement the code that finds the fixed point in the implicit Euler scheme. You can either use a fixed-point iteration, or a generic nonlinear solver, such as docs.scipy.org/doc/scipy/reference/optimize.html#root-finding
            – Federico
            Dec 4 '18 at 22:06










          • either way, thanks for your help!
            – user104
            Dec 4 '18 at 22:07














          3












          3








          3






          This code implements the explicit Euler scheme.



          In order to be implicit, there would be an instruction like



          y = solve(z == y + h*f(x,z), z)


          in the inner loop, where solve(equation, variable) is a suitable function to solve an equation (see here).






          share|cite|improve this answer














          This code implements the explicit Euler scheme.



          In order to be implicit, there would be an instruction like



          y = solve(z == y + h*f(x,z), z)


          in the inner loop, where solve(equation, variable) is a suitable function to solve an equation (see here).







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited Dec 4 '18 at 21:58

























          answered Dec 4 '18 at 21:48









          FedericoFederico

          4,829514




          4,829514












          • how would I add a step size to this code?
            – user104
            Dec 4 '18 at 21:52










          • The step size is h, which is currently controlled by the argument n
            – Federico
            Dec 4 '18 at 21:54










          • so what do I need to replace that line of code you have in my code?
            – user104
            Dec 4 '18 at 22:03










          • You need to implement the code that finds the fixed point in the implicit Euler scheme. You can either use a fixed-point iteration, or a generic nonlinear solver, such as docs.scipy.org/doc/scipy/reference/optimize.html#root-finding
            – Federico
            Dec 4 '18 at 22:06










          • either way, thanks for your help!
            – user104
            Dec 4 '18 at 22:07


















          • how would I add a step size to this code?
            – user104
            Dec 4 '18 at 21:52










          • The step size is h, which is currently controlled by the argument n
            – Federico
            Dec 4 '18 at 21:54










          • so what do I need to replace that line of code you have in my code?
            – user104
            Dec 4 '18 at 22:03










          • You need to implement the code that finds the fixed point in the implicit Euler scheme. You can either use a fixed-point iteration, or a generic nonlinear solver, such as docs.scipy.org/doc/scipy/reference/optimize.html#root-finding
            – Federico
            Dec 4 '18 at 22:06










          • either way, thanks for your help!
            – user104
            Dec 4 '18 at 22:07
















          how would I add a step size to this code?
          – user104
          Dec 4 '18 at 21:52




          how would I add a step size to this code?
          – user104
          Dec 4 '18 at 21:52












          The step size is h, which is currently controlled by the argument n
          – Federico
          Dec 4 '18 at 21:54




          The step size is h, which is currently controlled by the argument n
          – Federico
          Dec 4 '18 at 21:54












          so what do I need to replace that line of code you have in my code?
          – user104
          Dec 4 '18 at 22:03




          so what do I need to replace that line of code you have in my code?
          – user104
          Dec 4 '18 at 22:03












          You need to implement the code that finds the fixed point in the implicit Euler scheme. You can either use a fixed-point iteration, or a generic nonlinear solver, such as docs.scipy.org/doc/scipy/reference/optimize.html#root-finding
          – Federico
          Dec 4 '18 at 22:06




          You need to implement the code that finds the fixed point in the implicit Euler scheme. You can either use a fixed-point iteration, or a generic nonlinear solver, such as docs.scipy.org/doc/scipy/reference/optimize.html#root-finding
          – Federico
          Dec 4 '18 at 22:06












          either way, thanks for your help!
          – user104
          Dec 4 '18 at 22:07




          either way, thanks for your help!
          – user104
          Dec 4 '18 at 22:07


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Mathematics 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%2fmath.stackexchange.com%2fquestions%2f3026217%2fexplicit-euler-method%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

          Basket-ball féminin

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

          I want to find a topological embedding $f : X rightarrow Y$ and $g: Y rightarrow X$, yet $X$ is not...