How to include an equation model in python code

Multi tool use
Multi tool use












-5















So um I wanted to know how to include an equational model in python such that I put in the values and get the output according to the equation . Like for example if my model was x^2 + y^2 = output. How do I incorporate this model into my code , so that when I give values for x and y I get the output .










share|improve this question


















  • 1





    Write it as a function.

    – Paritosh Singh
    Dec 28 '18 at 21:36











  • Look at function definitions in Python

    – Daniel Mesejo
    Dec 28 '18 at 21:37











  • link to python math operations : en.wikibooks.org/wiki/Python_Programming/Basic_Math

    – 123abc321bca
    Dec 28 '18 at 21:41













  • In python ^ is treated as XOR so you might want to look for operators in Python as well. You can use **.

    – Rishabh Mishra
    Dec 28 '18 at 21:42
















-5















So um I wanted to know how to include an equational model in python such that I put in the values and get the output according to the equation . Like for example if my model was x^2 + y^2 = output. How do I incorporate this model into my code , so that when I give values for x and y I get the output .










share|improve this question


















  • 1





    Write it as a function.

    – Paritosh Singh
    Dec 28 '18 at 21:36











  • Look at function definitions in Python

    – Daniel Mesejo
    Dec 28 '18 at 21:37











  • link to python math operations : en.wikibooks.org/wiki/Python_Programming/Basic_Math

    – 123abc321bca
    Dec 28 '18 at 21:41













  • In python ^ is treated as XOR so you might want to look for operators in Python as well. You can use **.

    – Rishabh Mishra
    Dec 28 '18 at 21:42














-5












-5








-5








So um I wanted to know how to include an equational model in python such that I put in the values and get the output according to the equation . Like for example if my model was x^2 + y^2 = output. How do I incorporate this model into my code , so that when I give values for x and y I get the output .










share|improve this question














So um I wanted to know how to include an equational model in python such that I put in the values and get the output according to the equation . Like for example if my model was x^2 + y^2 = output. How do I incorporate this model into my code , so that when I give values for x and y I get the output .







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '18 at 21:35









reliance69reliance69

31




31








  • 1





    Write it as a function.

    – Paritosh Singh
    Dec 28 '18 at 21:36











  • Look at function definitions in Python

    – Daniel Mesejo
    Dec 28 '18 at 21:37











  • link to python math operations : en.wikibooks.org/wiki/Python_Programming/Basic_Math

    – 123abc321bca
    Dec 28 '18 at 21:41













  • In python ^ is treated as XOR so you might want to look for operators in Python as well. You can use **.

    – Rishabh Mishra
    Dec 28 '18 at 21:42














  • 1





    Write it as a function.

    – Paritosh Singh
    Dec 28 '18 at 21:36











  • Look at function definitions in Python

    – Daniel Mesejo
    Dec 28 '18 at 21:37











  • link to python math operations : en.wikibooks.org/wiki/Python_Programming/Basic_Math

    – 123abc321bca
    Dec 28 '18 at 21:41













  • In python ^ is treated as XOR so you might want to look for operators in Python as well. You can use **.

    – Rishabh Mishra
    Dec 28 '18 at 21:42








1




1





Write it as a function.

– Paritosh Singh
Dec 28 '18 at 21:36





Write it as a function.

– Paritosh Singh
Dec 28 '18 at 21:36













Look at function definitions in Python

– Daniel Mesejo
Dec 28 '18 at 21:37





Look at function definitions in Python

– Daniel Mesejo
Dec 28 '18 at 21:37













link to python math operations : en.wikibooks.org/wiki/Python_Programming/Basic_Math

– 123abc321bca
Dec 28 '18 at 21:41







link to python math operations : en.wikibooks.org/wiki/Python_Programming/Basic_Math

– 123abc321bca
Dec 28 '18 at 21:41















In python ^ is treated as XOR so you might want to look for operators in Python as well. You can use **.

– Rishabh Mishra
Dec 28 '18 at 21:42





In python ^ is treated as XOR so you might want to look for operators in Python as well. You can use **.

– Rishabh Mishra
Dec 28 '18 at 21:42












3 Answers
3






active

oldest

votes


















0














You should use sympy for cleaner calculations



from sympy import *
x = Symbol('x') # define first symbol
y = Symbol('y') # define second symbol

output = x**2 + y**2 # form the equation

print(output) # print the equation on console


Output



x**2 + y**2


Now substitute the values of x and y like we do in any math equation



output.subs({x:1,y:1}) #substitue x::1 and y::1 to get the result


Output



2 # 1**2 ==1 and 1**2==1 and 1+1 =2


For completeness, you can define the equation inside the function as well but it will be less descriptive for complex equations



def func(x,y): return x**2 + y**2


Now you can use the function to get the output



func(1,1) #2





share|improve this answer

































    1














    A function can receive one or more inputs, and return a value: https://www.w3schools.com/python/python_functions.asp



    def my_equation(x, y):
    return x**2 + y**2





    share|improve this answer
























    • oh yes should have done that. thanks a lot !!

      – reliance69
      Dec 29 '18 at 3:41



















    0














    There are two ways to do this,




    1. @paritosh-singh said, write it as a function

    2. use a library that allows you to do symbolic calculations, such as sympy


    with sympy, you can do this as



    from sympy import *
    x, y, z= symbols('x y z')
    z = (x^2)+(y^2)


    you can now assign values to x and y and get output as z.



    sympy documentation






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53964514%2fhow-to-include-an-equation-model-in-python-code%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









      0














      You should use sympy for cleaner calculations



      from sympy import *
      x = Symbol('x') # define first symbol
      y = Symbol('y') # define second symbol

      output = x**2 + y**2 # form the equation

      print(output) # print the equation on console


      Output



      x**2 + y**2


      Now substitute the values of x and y like we do in any math equation



      output.subs({x:1,y:1}) #substitue x::1 and y::1 to get the result


      Output



      2 # 1**2 ==1 and 1**2==1 and 1+1 =2


      For completeness, you can define the equation inside the function as well but it will be less descriptive for complex equations



      def func(x,y): return x**2 + y**2


      Now you can use the function to get the output



      func(1,1) #2





      share|improve this answer






























        0














        You should use sympy for cleaner calculations



        from sympy import *
        x = Symbol('x') # define first symbol
        y = Symbol('y') # define second symbol

        output = x**2 + y**2 # form the equation

        print(output) # print the equation on console


        Output



        x**2 + y**2


        Now substitute the values of x and y like we do in any math equation



        output.subs({x:1,y:1}) #substitue x::1 and y::1 to get the result


        Output



        2 # 1**2 ==1 and 1**2==1 and 1+1 =2


        For completeness, you can define the equation inside the function as well but it will be less descriptive for complex equations



        def func(x,y): return x**2 + y**2


        Now you can use the function to get the output



        func(1,1) #2





        share|improve this answer




























          0












          0








          0







          You should use sympy for cleaner calculations



          from sympy import *
          x = Symbol('x') # define first symbol
          y = Symbol('y') # define second symbol

          output = x**2 + y**2 # form the equation

          print(output) # print the equation on console


          Output



          x**2 + y**2


          Now substitute the values of x and y like we do in any math equation



          output.subs({x:1,y:1}) #substitue x::1 and y::1 to get the result


          Output



          2 # 1**2 ==1 and 1**2==1 and 1+1 =2


          For completeness, you can define the equation inside the function as well but it will be less descriptive for complex equations



          def func(x,y): return x**2 + y**2


          Now you can use the function to get the output



          func(1,1) #2





          share|improve this answer















          You should use sympy for cleaner calculations



          from sympy import *
          x = Symbol('x') # define first symbol
          y = Symbol('y') # define second symbol

          output = x**2 + y**2 # form the equation

          print(output) # print the equation on console


          Output



          x**2 + y**2


          Now substitute the values of x and y like we do in any math equation



          output.subs({x:1,y:1}) #substitue x::1 and y::1 to get the result


          Output



          2 # 1**2 ==1 and 1**2==1 and 1+1 =2


          For completeness, you can define the equation inside the function as well but it will be less descriptive for complex equations



          def func(x,y): return x**2 + y**2


          Now you can use the function to get the output



          func(1,1) #2






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 28 '18 at 21:59

























          answered Dec 28 '18 at 21:51









          mad_mad_

          3,89011021




          3,89011021

























              1














              A function can receive one or more inputs, and return a value: https://www.w3schools.com/python/python_functions.asp



              def my_equation(x, y):
              return x**2 + y**2





              share|improve this answer
























              • oh yes should have done that. thanks a lot !!

                – reliance69
                Dec 29 '18 at 3:41
















              1














              A function can receive one or more inputs, and return a value: https://www.w3schools.com/python/python_functions.asp



              def my_equation(x, y):
              return x**2 + y**2





              share|improve this answer
























              • oh yes should have done that. thanks a lot !!

                – reliance69
                Dec 29 '18 at 3:41














              1












              1








              1







              A function can receive one or more inputs, and return a value: https://www.w3schools.com/python/python_functions.asp



              def my_equation(x, y):
              return x**2 + y**2





              share|improve this answer













              A function can receive one or more inputs, and return a value: https://www.w3schools.com/python/python_functions.asp



              def my_equation(x, y):
              return x**2 + y**2






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 28 '18 at 21:40









              user8181134user8181134

              32614




              32614













              • oh yes should have done that. thanks a lot !!

                – reliance69
                Dec 29 '18 at 3:41



















              • oh yes should have done that. thanks a lot !!

                – reliance69
                Dec 29 '18 at 3:41

















              oh yes should have done that. thanks a lot !!

              – reliance69
              Dec 29 '18 at 3:41





              oh yes should have done that. thanks a lot !!

              – reliance69
              Dec 29 '18 at 3:41











              0














              There are two ways to do this,




              1. @paritosh-singh said, write it as a function

              2. use a library that allows you to do symbolic calculations, such as sympy


              with sympy, you can do this as



              from sympy import *
              x, y, z= symbols('x y z')
              z = (x^2)+(y^2)


              you can now assign values to x and y and get output as z.



              sympy documentation






              share|improve this answer




























                0














                There are two ways to do this,




                1. @paritosh-singh said, write it as a function

                2. use a library that allows you to do symbolic calculations, such as sympy


                with sympy, you can do this as



                from sympy import *
                x, y, z= symbols('x y z')
                z = (x^2)+(y^2)


                you can now assign values to x and y and get output as z.



                sympy documentation






                share|improve this answer


























                  0












                  0








                  0







                  There are two ways to do this,




                  1. @paritosh-singh said, write it as a function

                  2. use a library that allows you to do symbolic calculations, such as sympy


                  with sympy, you can do this as



                  from sympy import *
                  x, y, z= symbols('x y z')
                  z = (x^2)+(y^2)


                  you can now assign values to x and y and get output as z.



                  sympy documentation






                  share|improve this answer













                  There are two ways to do this,




                  1. @paritosh-singh said, write it as a function

                  2. use a library that allows you to do symbolic calculations, such as sympy


                  with sympy, you can do this as



                  from sympy import *
                  x, y, z= symbols('x y z')
                  z = (x^2)+(y^2)


                  you can now assign values to x and y and get output as z.



                  sympy documentation







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 28 '18 at 21:42









                  AuxilusAuxilus

                  20719




                  20719






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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%2fstackoverflow.com%2fquestions%2f53964514%2fhow-to-include-an-equation-model-in-python-code%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







                      QoVBOGEaD3eKO,0Zg6vOUtMu,c,W5bYl3lz,QYqeWX,SEGrC6pTk lO PzHTEKN 61Y9
                      tK fuApW

                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas