Solve Nonlinear Equations with Python

Поділитися
Вставка
  • Опубліковано 5 січ 2025

КОМЕНТАРІ • 59

  • @sunillamichhane2459
    @sunillamichhane2459 2 роки тому +1

    is that guess (1,1) center of the circle?

    • @apm
      @apm  2 роки тому

      The circle x^2 + y^2 = 20 has a center at (0,0) and radius sqrt(20).

  • @kedralawabela5542
    @kedralawabela5542 2 роки тому +1

    Thank you,
    I want to solve multiplicative exponential models. I know the base values and the value of f, I want to find the value of exponents. the form is a^x*b^y*c^z = f. I have the value of a, b and c. the required variables are exponents (x,y,z). Please do have video on python to solve these type of equations?
    Thank you,

    • @apm
      @apm  2 роки тому

      Here is a similar nonlinear regression application: apmonitor.com/me575/index.php/Main/NonlinearRegression

    • @kedralawabela5542
      @kedralawabela5542 2 роки тому

      Thank you so much, I will try to solve my equations using this video.

  • @AJ-et3vf
    @AJ-et3vf 3 роки тому +1

    Awesome tutorial! Lucid and very helpful

  • @msjahan7697
    @msjahan7697 2 роки тому +1

    Can you make a video on how to solve more than three coupled non linear equations? I solved for three equations using sympy and I got the solution but for more than three equations it is showing error.

    • @apm
      @apm  2 роки тому

      You typically need to use a numerical approach for anything over 3 equations. With a numerical approach, it is easy to solve 10,000+ equations: apmonitor.com/pds/notebooks/10_solve_equations.html Gekko (Python package) is a large-scale equation solver. More details are here: apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization

  • @kedralawabela5542
    @kedralawabela5542 2 роки тому +1

    Hello sir,
    I am looking for Genetic Algorithms codes or videos to solve optimization problems. Could you help me please?

    • @apm
      @apm  2 роки тому

      See this book chapter apmonitor.com/me575/index.php/Main/GeneticAlgorithms There are packages such as pypi.org/project/genetic-algorithm/ that can help.

  • @javadjeddizahed1967
    @javadjeddizahed1967 Рік тому +1

    Thanks for your amazing video. I have got a question. This function only gives a single answer for the equations while a set of equations might have numerous answers. Could you please explain how to solve this issue ?

    • @apm
      @apm  Рік тому +1

      Sympy can find multiple solutions. Another method is to use different initial guesses with a package such as fsolve: apmonitor.com/pds/notebooks/10_solve_equations.html

  • @oktabramantio4709
    @oktabramantio4709 6 років тому +3

    It's a great video and explanation! Thank you very much!

  • @msjahan7697
    @msjahan7697 2 роки тому +1

    I need to find the exact solution for the 4 equations. Is it possible to find the solution without any initial guess?

    • @apm
      @apm  2 роки тому

      Try Sympy. If it doesn't work then there are other symbolic packages such as Mathematica. If those don't work then maybe try simplification through substitution or a numerical method.

  • @kedralawabela5542
    @kedralawabela5542 2 роки тому +1

    Hello Sir,
    Thank you once again.
    Please do you have videos on python to solve optimization equations with Lagrange multiplier? Or what python libraries I should use to solve such problems?

    • @apm
      @apm  2 роки тому

      For simple problems use: apmonitor.com/me575/index.php/Main/KuhnTucker For more complicated problems, use Python Gekko: apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization

    • @kedralawabela5542
      @kedralawabela5542 2 роки тому

      Thanks

  • @hassenmedjahed3299
    @hassenmedjahed3299 5 років тому +2

    Hello sir, thank you for this elustration
    I have a question about optimizing a set of formulas (equations).
    Actually, I have a program with a set of nested equations whose I need to find the optimum of two variables
    With your example, the number of variables is equal to the number of equations, whereas in my case the number of variables is 2 and the number of equations is much larger (about 20), one of which is nested in the others.
    Thank you for your answer

    • @apm
      @apm  5 років тому +1

      Nested solves can be very challenging because the sub-problems have numerical errors that may produce bad gradients for the upper level problems. I recommend a simultaneous approach to solving the equations as shown in several Python Gekko tutorials: apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization If you have 20 equations and 2 variables then the equations should be inequality constraints else the problem is over-determined and there is no solution.

  • @theultimatereductionist7592
    @theultimatereductionist7592 4 роки тому +2

    You need DOZENS more videos on JUST solving systems of nonlinear equations.
    NO differential equations. NO optimization. Because, I have cut & pasted your code into my Python:
    I get syntax error messages all over the place.

    • @apm
      @apm  4 роки тому

      Here are many more videos on optimization: apmonitor.com/me575 and apmonitor.com/do The syntax errors may be from print statements if it is Python 2. Just add parenthesis for Python 3 such as print(x) instead of print x.

    • @apm
      @apm  4 роки тому +1

      @@theultimatereductionist7592 oops - you are right. Here are more tutorials on solving equations: github.com/APMonitor/data_science/blob/master/10.%20Solve_Equations.ipynb Optimizers can typically solve equations just as well but you can just leave out the objective function and have the same number of equations and variables.

    • @theultimatereductionist7592
      @theultimatereductionist7592 4 роки тому +2

      @@apm Ok. THANK YOU!

  • @christianmohl6039
    @christianmohl6039 3 роки тому +1

    Thank you for the nice example :)

  • @benbell9170
    @benbell9170 2 роки тому +1

    Dear sir,
    thank you very much for this tutorial!
    I'm very new in coding and for my master thesis I need a solution to find the point, where 3 circles coincide.
    I have 3 equations in the format of (x-a)² + (y-b)² = (r-c)², where [a, b, c] are the distances of the center of each circle to my reference point.
    Could you please share with me some of your insight, how I should approach this problem?
    I'd very appreciate that!
    Sincerely!

    • @apm
      @apm  2 роки тому

      Does this help? apmonitor.com/me575/index.php/Main/CircleChallenge

  • @hassenmedjahed3299
    @hassenmedjahed3299 5 років тому +2

    Thank you, I will look on the site you sent me
    to answer your question :
    the condition is to minimize these two terms
    abs [(NbrS1-nbrs) / nbrs]

    • @apm
      @apm  5 років тому

      If you use abs() in your objective function, I recommend that you use the Gekko m.abs3 or m.abs2 functions. They have continuous first and second derivatives so it will help the solver converge. Here is additional information on functions like absolute value: apmonitor.com/me575/index.php/Main/LogicalConditions

  • @careylin1913
    @careylin1913 5 років тому +1

    what are the guess values for?

    • @apm
      @apm  5 років тому

      They are used as a starting point for the solver. Sometimes the solver can't solve the problem without sufficiently close guess values.

    • @careylin1913
      @careylin1913 5 років тому +1

      @@apmthanks!your video really helps!

  • @musixbox7652
    @musixbox7652 Рік тому +1

    the code gives error

    • @apm
      @apm  Рік тому

      Try the code here apmonitor.com/che263/index.php/Main/PythonSolveEquations You may have a syntax error.

  • @surabhisnath1622
    @surabhisnath1622 4 роки тому +1

    How can we find all solutions?

    • @apm
      @apm  4 роки тому +1

      You need to start with different initial guesses. If it is a simple problem then you can use SymPy to find all solutions analytically. Here are examples: github.com/APMonitor/data_science/blob/master/10.%20Solve_Equations.ipynb

  • @eduardosufan1
    @eduardosufan1 6 років тому +2

    Very clear, thanks

  • @dodogo777
    @dodogo777 8 років тому

    Thank you sir, the example is very helpful!
    Can I know how do we decide the zGuess's values ?

    • @dodogo777
      @dodogo777 8 років тому

      I think I got the answer by reading one of the comments. Thanks! What is the best way to validate the correctness of the answer using scipy? and what would be the result if fsolve fails to solve the equations ?

    • @apm
      @apm  8 років тому +1

      The solver will return a status once it finishes. If it says that it was successful then it should be a (locally) optimal solution. You can try different starting points if you are worried that it is not the global optimum.

    • @dodogo777
      @dodogo777 8 років тому

      Thanks! I just realized that we can set the optional parameter "full_output = True" to get fsolve generates the status.

  • @halehallamehhaery1803
    @halehallamehhaery1803 8 років тому

    many thanks for your video. I have a question If we have two equations like:
    F(1)=x^2+y^2+R
    F(2)=x*y+2R
    and R=(x+y)/y
    how then we can solve this? as here R is not another function just a parameter to make the function look more simple.

    • @apm
      @apm  8 років тому +2

      You can input the following into apmonitor.com/online/view_pass.php (APMonitor Online Optimization). I re-arranged the equation R=(x+y)/y to avoid divide by zero.
      Variables
      x
      y
      R
      Equations
      R * y = (x+y)
      0=x^2+y^2+R
      0=x*y+2*R
      If you want to solve this in Python, use:
      import numpy as np
      from scipy.optimize import fsolve
      def myFunction(z):
      x = z[0]
      y = z[1]
      R = z[2]
      F = np.empty((3))
      F[0] = R * y - x - y
      F[1] = pow(x,2)+pow(y,2)+R
      F[2] = x * y - 2.0*R
      return F
      zGuess = np.array([-1,1,1])
      z = fsolve(myFunction,zGuess)
      print(z)
      A solution appears to be [0,0,0].

    • @halehallamehhaery1803
      @halehallamehhaery1803 8 років тому

      Dear Sir,
      first of all I am really appreciative of your help. I don t know how to express my gratitude.
      The question I asked was due to the code I am writing for "self_consistent scheme in composite". I stuck in iteration as there are two main formula with two main unknown variables (like x and y)which I need to determine. the problem is that each of these main formula have parameters (like R in above example) which itself are a function of the two unknown variables (x, y). In above example, R was easy to convert to an equation but in case of the equations I am using are not easy.
      there is no attachment option here, otherwise I would attach the formula.
      how about I follow your scheme but I introduce those R-like parameters before the equation?

    • @rrc
      @rrc 8 років тому +1

      +Haleh Allameh Haery yes, introducing the R equation before the residual calculation would be a great way to accomplish that. if you need to post files, you can visit the APMonitor user's group.

  • @tomgreg2008
    @tomgreg2008 3 роки тому +1

    Okay, Thanks!

  • @sounakmojumder5689
    @sounakmojumder5689 2 роки тому +1

    thank you

  • @hassenmedjahed3299
    @hassenmedjahed3299 5 років тому +1

    Thanks for your help
    Here is what I would like to achieve with my script
    The objective is to calculate the optimal value of PC and F which allows to verify the contrait
    Do you think there is a solution with python
    thank you
    """""EC=1650 # constant
    DIC=1510# constant
    loC=1125 # constant
    Surface=pi*DEC*loC*1e-6# constant
    RhoC=1e-5 # constant
    Coef_rhoT=0 # constant
    Tchauffe=0# constant
    BSat=0 # constant
    EtaMag='amag' # constant
    mur=1# constant
    mu0=4e-7*pi# constant
    DII=1950 # constant
    loI =1125# constant
    Coef_Remp=0.5# constant
    RhoCu=1.72e-8 # constant
    SelfPara=0# constant
    Rap_Trans1=1# constant
    Tens_Capa=480 # constant
    NbrS =8 # constant
    Capa= 650# constant
    Capa1=0 # initiale varlue
    #*********************************calcul**********************************
    PC=0.01 # Initial value of variable
    F=3 # Initial value of variable
    NbrS1=0 # Initial value
    Qcon=0 #Initial value
    EC=(DEC-DIC)/2 # Constant
    REL=(DII-DEC)/(2*loI)# Constant
    VindCon=Tens_Capa/Rap_Trans1 # Constant
    if RELloC:
    lochauffe=loC #Constant
    else:
    lochauffe=loI #Constant
    Rho_T=RhoC*(1+Coef_rhoT*Tchauffe)# Constant
    VeExt=pi*((DII*1e-3)**2*(loI*1e-3)-(DEC*1e-3)**2*(lochauffe*1e-3))/4 # Constant
    VeInt=pi*((DIC*1e-3)**2*(loI*1e-3))/4 # Constant
    if DIC

    • @apm
      @apm  5 років тому

      Yes, it is possible to solve an optimization problem with Python. Please see apmonitor.com/che263/index.php/Main/PythonOptimization for more information. I recommend Method #3 but Method #2 could also work. The one thing that I see that you are missing from your problem is a statement of your objective function. If you are optimizing the values of PC and F, what is the quantity that you are trying to minimize or maximize?

  • @andyoohhh2061
    @andyoohhh2061 5 років тому +1

    Thank you!

  • @PaulNahay
    @PaulNahay 4 роки тому +2

    You should NOT be doing "import *". Readers need to know/see where your functions are coming from.

    • @apm
      @apm  4 роки тому

      I've gotten a lot of feedback about that approach. That is good advice!

  • @gabrielomelo
    @gabrielomelo 4 роки тому +1

    Farewell matlab!

    • @apm
      @apm  4 роки тому +1

      I switched over to Python a few years ago. The only reason I go back to Matlab now is for some occasional Simulink.

  • @ShinjiCarlos
    @ShinjiCarlos 3 дні тому

    "from... import *" is a bad practice. Import only what you are going to use.