Simplest way to solve mathematical equations in Python

后端 未结 15 1742
自闭症患者
自闭症患者 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: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
    

提交回复
热议问题