Simplest way to solve mathematical equations in Python

后端 未结 15 1603
自闭症患者
自闭症患者 2020-12-13 10:15

I want to solve a set of equations, linear, or sometimes quadratic. I don\'t have a specific problem, but often, I have been in this situation often.

It is simple to

相关标签:
15条回答
  • 2020-12-13 10:24

    It depends on your needs:

    If you want an interactive graphical interface, then sage is probably the best solution.

    If you want to avoid using a graphical interface, but you still want to do computer algebra, then sympy or maxima may cover your needs. (sympy looks very promising, but it still have a long way to go before they can replace mathematica).

    If you don't really need symbolic algrebra, but you need a way to program with matrices, solve differential equations, and minimize functions, then scipy or octave are excelent starting points.

    0 讨论(0)
  • 2020-12-13 10:29

    For reference: Wolfram Alpha's solution:

    a-1000!=0,   b = (1000 (a-500))/(a-1000),   c = (-a^2+1000 a-500000)/(a-1000)
    

    In python, using sympy's solver module (note that it assumes all equations are set equal to zero):

    >>> import sympy
    >>> a, b, c = sympy.symbols('a, b, c')
    >>> sympy.solve([a + b + c - 1000, a**2 + b**2 - c**2], b, c)
    [(1000*(a - 500)/(a - 1000), (-a**2 + 1000*a - 500000)/(a - 1000))]
    

    And of course, a != 1000, as a-1000 is the denominator of the two equations.

    0 讨论(0)
  • 2020-12-13 10:30

    Well, I just googled into this page by accident. I see many suggestions regarding this and that software tool, but does any tool actually provide an answer? The actual answer is:

    [a,b,c] = [200,375,425]

    How did I get this? By writing a quick program in the Maxima programming language to find it via "brute force" searching. It only took about 10 minutes to write, seeing as how I'm familiar with the Maxima language. It took a few seconds for the program to run. Here is the program:

    euler_solve():= block ( [ a, b, A, B, end:1000],

    for a thru end do
        (
        for b thru end do
            (
            c: 1000 -a -b,
            if c < 0 then
                b:end
            else if a^2 + b^2 = c^2 then
                (
                A:a,
                B:b,
                a:end,
                b:end
                )
            )
        ),
    return( [A,B,c])
    );
    

    You can just cut and paste the above code into the wxMaxima user interface, which I run under Ubuntu and not MS Windows. Then you just enter the function name: euler_solve(), hit return, wait a few seconds, and out pops the answer. This particular kind of problem is so simple that you could use any general-purpose programming language to do the search.

    0 讨论(0)
  • 2020-12-13 10:31

    A free web-service for solving large-scale systems of nonlinear equations (1 million+) is APMonitor.com. There is a browser interface and an API to Python / MATLAB. The API to Python is a single script (apm.py) that is available for download from the apmonitor.com homepage. Once the script is loaded into a Python code, it gives the ability to solve problems of:

    • Nonlinear equations
    • Differential and algebraic equations
    • Least squares model fitting
    • Moving horizon estimation
    • Nonlinear model predictive control
    • etc.

    For the new user, the APM Python software has a Google Groups forum where a user can post questions. There are bi-weekly webinars that showcase optimization problems in operations research and engineering.

    Below is an example of an optimization problem (hs71.apm).

    Model
    
      Variables
    
        x[1] = 1, >=1, <=5
    
        x[2] = 5, >=1, <=5
    
        x[3] = 5, >=1, <=5
    
        x[4] = 1, >=1, <=5
    
      End Variables
    
    
    
      Equations
    
        x[1] * x[2] * x[3] * x[4] > 25
    
        x[1]^2 + x[2]^2 + x[3]^2 + x[4]^2 = 40
    
    
    
        minimize  x[1] * x[4] * (x[1]+x[2]+x[3]) + x[3]
    
      End Equations
    
    End Model
    

    The optimization problem is solved with the following Python script:

    # Import
    
    from apm import *
    
    # Select server
    
    server = 'http://xps.apmonitor.com'
    
    # Application name
    
    app = 'eqn'
    
    # Clear previous application
    
    apm(server,app,'clear all')
    
    # Load model file
    
    apm_load(server,app,'hs71.apm')
    
    # Option to select solver (1=APOPT, 2=BPOPT, 3=IPOPT)
    
    apm_option(server,app,'nlc.solver',3)
    
    # Solve on APM server
    
    solver_output = apm(server,app,'solve')
    
    
    # Display solver output
    
    print solver_output
    
    
    # Retrieve results
    
    results = apm_sol(server,app)
    
    # Display results
    
    print '--- Results of the Optimization Problem ---'
    
    print results
    
    # Display Results in Web Viewer 
    
    url = apm_var(server,app)
    
    print "Opened Web Viewer: " + url
    
    0 讨论(0)
  • 2020-12-13 10:31

    Take a look at this:

    http://openopt.org/FuncDesignerDoc#Solving_systems_of_nonlinear_equations

    It is extremely easy to use and quite powerful

    0 讨论(0)
  • 2020-12-13 10:36

    I don't think there is a unified way of dealing with both linear and quadratic (or generally nonlinear) equations simultaneously. With linear systems, python has bindings to linear algebra and matrix packages. Nonlinear problems tend to be solved on a case by case basis.

    0 讨论(0)
提交回复
热议问题