Solve Differential Equations in Python GEKKO

  Рет қаралды 10,884

APMonitor.com

APMonitor.com

Күн бұрын

Python GEKKO and SciPy ODEINT are used to find a solution to the differential equation: k * dy/dt = - t * y. An analytic solution is available with the method of "separate and integrate". More complex differential equations may require a numeric solution. Additional tutorial information is available from:
ODEINT: apmonitor.com/pdc/index.php/Ma...
GEKKO: apmonitor.com/wiki/index.php/M...
GEKKO solves not only ODEs but also large scale systems of differential and algebraic equations in simulation and for optimization. It is freely available with "pip install gekko".

Пікірлер: 8
@pnachtwey
@pnachtwey 6 жыл бұрын
This is good. I am finally convinced to install Gekko. At this time we are trying to do system identification of a non-linear second order system with 6 parameters and 5000 data samples. It works but it takes forever using orient. Odeint has a flaw. Our data is sampled every millisecond but the orient will use integration steps at odd increment to step from 0.000 to 0.001. There are two many steps. RK4 uses 4 steps. The bad part is that the odeint will cause a fault because it will do a step beyond 0.001 seconds. This is not a problem until we get to the end where is steps past the end of the time array and faults. We put a check in the "model" function to avoid faulting but this slows down the calculations even more and it is not clean. We gave up on odeint and switched to lmfit . Lmfit is faster but still slow. Our product uses lmfit and is very fast because it is written in C. Solutions are found in less than a second. Python is agonizingly slow by comparison. Python takes minutes. To speed up calculation we try using every 5th data point or more. If the step is too big we start getting NAN and INF results. tm=np.linspace(0,1,100) results in odd increments. tm=np.linspace(0,1,100+1) results in 10 ms increments. You need to try putting a print() in your model sometimes to see what it is doing. The video is good for teaching people how to get answers. I am writing this to let everyone know that in real life some of the things you learn are useless except for simple school problems and you really do need to understand what is happening inside the integration functions and not rely on something that someone else has written. I will let you know how Gekko works in real life.
@apm
@apm 6 жыл бұрын
Peter, thanks for providing insight on a challenging problem that you are currently facing. These types of experiences are very valuable to let students know that real problems with a lot of sophistication are possible with numerical methods. Let us know if you need any support as you try out Gekko. You may want to switch to m.options.IMODE=7 for a sequential solution versus IMODE=4 that is a simultaneous solution. Also, you can adjust the accuracy of the integration with m.options.NODES= {2-6}. With 6 nodes, the accuracy is better but the solution time will be slower. Gekko uses the time steps that you provide so you may need to include more intermediate time steps. We are still working on a method that will adjust the step size automatically and this should be available soon.
@pnachtwey
@pnachtwey 6 жыл бұрын
Is Gekko a package you and your students are writing? If so what algorithm is being used to integrate? I like RK because I know what the step size is and that it will not generate a step that goes beyond my time limits. I can also add limits so none of the state parameters can exceed them. So many integration packages provide limits for their parameters but they allow the limits to be exceeded by a little bit. RK is easy to restart after a limit is reached. RKF4 my not be the most accurate integration method but I think it is best for doing system identification. It is more than close enough. BTW, it is the interp1d that blows up when the x scale is exceeded. This is unsatisfactory. We provide 3 options for when a actuator exceeds the x scale. Fault, extrapolate, or use the last x and y value. Most people use the extrapolate option. Often there are rounding errors when the first or last x value is exceeded by 0.0000001 and a unwanted fault occurs that stops production. Not good. I will look into to your suggestion when I am back at work again. BTW, I have my own "Peter Ponders PID" KZfaq channel about control. I have many examples solving ODEs in the videos. The problem is that my Mathcad is obsolete and cost a lot of money. Python is free and people can just copy and paste your on-line examples.
@apm
@apm 6 жыл бұрын
We use orthogonal collocation on finite elements (apmonitor.com/do/index.php/Main/OrthogonalCollocation), very close to a RK method. One advantage is that high index DAEs can be solved with this approach and there is no problem with tyre consistent initial conditions for the algebraic states. Are you talking about the NumPy interp1d function? You should consider submitting this as a bug fix for the developers.
@ahmedtamer9568
@ahmedtamer9568 4 жыл бұрын
Thank you for your effort ! I just wanted to ask what should I do to solve a second order non linear differential equations where there are variables in them calculated from an empirical formula ? I keep getting an attribute error : float ... I followed your reply here stackoverflow.com/questions/57038180/how-do-you-define-a-second-derivative-in-the-python-gekko-module but I have the problem that some variables in equations are calculated from empirical equations that are not function in time ... Thank you in advance
@apm
@apm 4 жыл бұрын
Ahmed, could you create a new Stack Overflow post with specifics of your question? If it needs to call an external black-box model then I recommend that you sample that black-box model and include a cubic-spline or a b-spline function: apmonitor.com/wiki/index.php/Main/ObjectCspline or apmonitor.com/wiki/index.php/Main/ObjectBspline
@billyyank2198
@billyyank2198 4 жыл бұрын
Differential equations are not always easy to solve, especially in Python. But if you're worried about having to solve ODEs, I have good news! I just saved myself from writing hundreds of lines of code by installing Gekko! "Gekko: 5 minutes of installation could save you 500 lines of code, or more."
@apm
@apm 4 жыл бұрын
Best comment ever!!!
Numerical Optimization with Python GEKKO
7:52
APMonitor.com
Рет қаралды 6 М.
Discrete Optimization in Python GEKKO
12:01
APMonitor.com
Рет қаралды 7 М.
Useful gadget for styling hair 🤩💖 #gadgets #hairstyle
00:20
FLIP FLOP Hacks
Рет қаралды 11 МЛН
ОБЯЗАТЕЛЬНО СОВЕРШАЙТЕ ДОБРО!❤❤❤
00:45
Ouch.. 🤕
00:30
Celine & Michiel
Рет қаралды 29 МЛН
Machine Learning with Neural Network in Python GEKKO
17:05
APMonitor.com
Рет қаралды 6 М.
Chemical Reaction Differential Equations in Python
20:34
APMonitor.com
Рет қаралды 52 М.
PID Control Tuning with Python GEKKO
23:42
APMonitor.com
Рет қаралды 25 М.
Simulate Coupled Differential Equations in Python
28:23
APMonitor.com
Рет қаралды 50 М.
Solve ODE by Python - ODEint
6:04
MathPython
Рет қаралды 6 М.
Solve Differential Equations in Python by Using odeint() SciPy Function
12:13
Python Symbolic and Numeric Solutions
16:15
APMonitor.com
Рет қаралды 16 М.
This is one of the coolest integrals ever solved
9:00
Maths 505
Рет қаралды 5 М.
Tag him😳💕 #miniphone #iphone #samsung #smartphone #fy
0:11
Pockify™
Рет қаралды 5 МЛН
📱магазин техники в 2014 vs 2024
0:41
djetics
Рет қаралды 726 М.
Что делать если в телефон попала вода?
0:17
Лена Тропоцел
Рет қаралды 3,7 МЛН
разбил телефон из-за видео
0:15
STANISLAVSKIY Hi
Рет қаралды 816 М.