No video

Solve Nonlinear Equations with Python

  Рет қаралды 63,486

APMonitor.com

APMonitor.com

9 жыл бұрын

This tutorial demonstrates how to set up and solve a set of nonlinear equations in Python using the SciPy Optimize package.

Пікірлер: 58
@oktabramantio4709
@oktabramantio4709 6 жыл бұрын
It's a great video and explanation! Thank you very much!
@AJ-et3vf
@AJ-et3vf 3 жыл бұрын
Awesome tutorial! Lucid and very helpful
@eduardosufan1
@eduardosufan1 6 жыл бұрын
Very clear, thanks
@christianmohl6039
@christianmohl6039 3 жыл бұрын
Thank you for the nice example :)
@andyoohhh2061
@andyoohhh2061 5 жыл бұрын
Thank you!
@hassenmedjahed3299
@hassenmedjahed3299 4 жыл бұрын
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 4 жыл бұрын
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.
@sounakmojumder5689
@sounakmojumder5689 2 жыл бұрын
thank you
@hassenmedjahed3299
@hassenmedjahed3299 4 жыл бұрын
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 4 жыл бұрын
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
@tomgreg2008
@tomgreg2008 2 жыл бұрын
Okay, Thanks!
@theultimatereductionist7592
@theultimatereductionist7592 4 жыл бұрын
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 жыл бұрын
@@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 жыл бұрын
@@apm Ok. THANK YOU!
@kedralawabela5542
@kedralawabela5542 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
Thanks
@javadjeddizahed1967
@javadjeddizahed1967 Жыл бұрын
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 Жыл бұрын
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
@benbell9170
@benbell9170 2 жыл бұрын
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
@dodogo777
@dodogo777 7 жыл бұрын
Thank you sir, the example is very helpful! Can I know how do we decide the zGuess's values ?
@dodogo777
@dodogo777 7 жыл бұрын
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 7 жыл бұрын
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 7 жыл бұрын
Thanks! I just realized that we can set the optional parameter "full_output = True" to get fsolve generates the status.
@msjahan7697
@msjahan7697 2 жыл бұрын
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
@msjahan7697
@msjahan7697 2 жыл бұрын
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.
@sunillamichhane2459
@sunillamichhane2459 2 жыл бұрын
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 Жыл бұрын
Hello sir, I am looking for Genetic Algorithms codes or videos to solve optimization problems. Could you help me please?
@apm
@apm Жыл бұрын
See this book chapter apmonitor.com/me575/index.php/Main/GeneticAlgorithms There are packages such as pypi.org/project/genetic-algorithm/ that can help.
@kedralawabela5542
@kedralawabela5542 Жыл бұрын
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 Жыл бұрын
Here is a similar nonlinear regression application: apmonitor.com/me575/index.php/Main/NonlinearRegression
@kedralawabela5542
@kedralawabela5542 Жыл бұрын
Thank you so much, I will try to solve my equations using this video.
@hassenmedjahed3299
@hassenmedjahed3299 4 жыл бұрын
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 4 жыл бұрын
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?
@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 жыл бұрын
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 жыл бұрын
+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.
@careylin1913
@careylin1913 5 жыл бұрын
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 жыл бұрын
@@apmthanks!your video really helps!
@surabhisnath1622
@surabhisnath1622 4 жыл бұрын
How can we find all solutions?
@apm
@apm 4 жыл бұрын
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
@gabrielomelo
@gabrielomelo 4 жыл бұрын
Farewell matlab!
@apm
@apm 4 жыл бұрын
I switched over to Python a few years ago. The only reason I go back to Matlab now is for some occasional Simulink.
@musixbox7652
@musixbox7652 11 ай бұрын
the code gives error
@apm
@apm 11 ай бұрын
Try the code here apmonitor.com/che263/index.php/Main/PythonSolveEquations You may have a syntax error.
@PaulNahay
@PaulNahay 4 жыл бұрын
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!
Conditional (IF) Statement in Excel
8:15
APMonitor.com
Рет қаралды 2,3 М.
Python Symbolic and Numeric Solutions
16:15
APMonitor.com
Рет қаралды 16 М.
Я обещал подарить ему самокат!
01:00
Vlad Samokatchik
Рет қаралды 9 МЛН
Amazing weight loss transformation !! 😱😱
00:24
Tibo InShape
Рет қаралды 68 МЛН
Stay on your way 🛤️✨
00:34
A4
Рет қаралды 28 МЛН
Python Nonlinear Equations with Scipy fsolve
13:03
APMonitor.com
Рет қаралды 63 М.
Solve Linear Equations with Python
7:46
APMonitor.com
Рет қаралды 106 М.
Python 🐍 Solve 4 ODEs
16:56
APMonitor.com
Рет қаралды 19 М.
Solve ODEs in Python: Simple to Complex
34:02
APMonitor.com
Рет қаралды 75 М.
Solving Systems Of Equations Using Sympy And Numpy (Python)
15:23
Andrew Dotson
Рет қаралды 77 М.
This is one of the coolest integrals ever solved
9:00
Maths 505
Рет қаралды 5 М.
Solving equations with Python
11:18
ignite.byu.edu
Рет қаралды 27 М.
Simulate Coupled Differential Equations in Python
28:23
APMonitor.com
Рет қаралды 50 М.