Avoided the loop by passing u as a function representing the step function: def first_order(y, t, tau, K, u): dydt = (-y + K*u(t)) / tau return dydt t0 = 0 t1 = 10 intervals = 100 t = np.linspace(t0, t1, intervals) tau = 5.0 K = 2.0 u = lambda t: 0 if t < 3 else 1 y = odeint(first_order, 0, t, args=(tau, K, u)) plt.plot(t, y)
Hi, what if we had different values for U at every timepoint? It will be difficult to call u(t) in the ODE because the integrator adapts the time steps.
to use varying input parameters in the ODE in general it is advised to provide the input values via an own function instead of storing them in a list which you build a loop around. This saves code and also avoids lower precision issues that arise when using large time steps (like those in the video) for the loop. In this example the function would look somewhat like: def u(t): if t < 3: return 0.0 else: return 1.0 this way you can just use u(t) in the 'firstorder'-function and conveniently stick to the standard odeint syntax.
I appreciate that suggestion. One thing that I found with the ODEINT integrator is that even though a larger step size is requested the integrator will internally and adaptively modify the steps size to maintain accuracy and then only report the final value of the interval that is requested. I often use the models in control applications where we need to update the inputs frequently and at a given time interval. I teach the way you mentioned as a first approach if we don't need to have many successive input changes that would overwhelm a conditional statement in the model. apmonitor.com/pdc/index.php/Main/SolveDifferentialEquations One more benefit of your approach is that the integrator may even select larger step sizes than the loop increments if it is deemed that the accuracy can be maintained. This could save a lot of time for repeat calculations of points that could be interpolated.
I seem to get an error when setting u = np.zeros(len(t)) . Get an error saying "RuntimeError: The size of the array returned by func (11) does not match the size of y0 (1)" Any advice?
Try the source code here: apmonitor.com/pdc/index.php/Main/TankBlending There are also related examples here: apmonitor.com/pdc/index.php/Main/SolveDifferentialEquations or here: apmonitor.com/pdc/index.php/Main/StirredReactor
Great video! Around 14:25 you speak of accessing an array for the argument u. Is it possible to set up a seperate function that loops that array(11 terms) through your ode function, without manually updating that value one run at a time? I am currently struggling in my own research with a similar problem.
Sure, you could create some type of lookup function inside the derivative function. Here are other examples: apmonitor.com/pdc/index.php/Main/SolveDifferentialEquations
Thank you for wonderful lecture! I wonder what if can solve ODEs which is q(coefficient) is function for example function of temperature and there are equation for temperature too
Hello Sir, thank you for this fantastic tutorial. Just one thing I am really struggling to understand, what is meant by states shown at 27:39 , is it to do with state space form or something else? What confuses me is what is meant by Ca = x[0] and T = x[1] ? I would be grateful for your reply!
The states are the variables that show up at differential terms in the equations. Here is more information on state space: apmonitor.com/pdc/index.php/Main/StateSpaceModel (it may confuse you more, however).
@@apm Thank you for the prompt response Sir. I have a follow up question, if I run the source code attached in the link of your description, and I print the values of Ca inside the mixer function just after the line Ca = x[0], they are different compared to when I print them after Ca = odeint is solved. What is the difference between the values of Ca stored in array x[0] compared to the Ca values that you solve for using odeint?
@@Matt-lc6bs the values inside the function depend on what the ODEINT solver is requesting to solve the equations. It is after the solution is completed that you can use the values.
Here are several example problems: apmonitor.com/pdc/index.php/Main/SolveDifferentialEquations If you have a large-scale system of nonlinear differential equations then I recommend Python Gekko.
The URL for the example problem is accessible from a new Card at 00:12. I also put the link in the video description: apmonitor.com/pdc/index.php/Main/TankBlending If you are looking for information on how to download Python, there are tutorials here: apmonitor.com/pdc/index.php/Main/InstallPython
hello, I am from Germany and a great fan of your videos and I want to ask you, wether it is possible to make a modeling and simulation video about an engineering application where the fluid momentum balance equation have to be applied. Another very interesting problem to model and simulate, would be a simple hydraulic transmission system/ pump, hydraulic circuit, motor, where the hydraulic resistance, inductance and capacitance is present. And could you show an example how you can solve optimization problems without the apm solver, in python and matlab great channel, thanks for all your videos and the teaching of this interesting topics
Here is one video that shows a network of pipes (th-cam.com/video/Y7YBPqGKr9Y/w-d-xo.html) and solving for the fluid flow. A 2nd example is posted here: apmonitor.com/pdc/index.php/Main/FirstOrderOptimization as an example of how to use scipy.optimize.minimize - I'm currently teaching Process Dynamics and Control this semester so I'll likely have more on related topics. Thanks again for the suggestions.
Thanks for the answer and making references to the respective videos. I am very pleased, that my suggestions were noted, but I know of course that there have to be time for such additional topics besides the traditional lecture and your work. So if something of my suggestions would be eventually realized, it would be great:) I know that your work has a strong chemical/ process engineering background and control theory is maybe more a means to an end, but if you have experiences in Nonlinear control and Robust Control and its applications in engineering and of course within the context of Matlab and python, that would be great. Suggestions for the future:) - state feedback linearization/ input output linearization - Lyapunov based controller design - lure system, if you ever heard from that - Robust control (H_Infinity, Norms at all for controller design, Weighting transfer functions…) - Sliding Mode Control - Describing Functions - LQR, LQG, Kalman filter - Differential Geometry and its applications in Control Theory,Lie derivative/ brackets, Dynamical Systems on Manifolds/ for all this points, I not only want to learn the methodology(I learned about it in university, more than a less) I want to get an Intuition, a feeling how and why it works. This is my intention for all I am learning and it also should the intention of the teachers, I think Thanks for all your work and giving people the chance to learn this cool engineering stuff. and Excuse my english…
Hello! I admire your time spent demonstrating the use of Python for application programming. Wonderful stuff! Do you have any examples with State Space differential equation with feedback??? For example, dotx = Ax + Bu y = Cx +Du for u = U_ref - Kx. K = const. Could Python be used to iterate x through Newton's method (Jacobian Matrix) with saved results?
Avoided the loop by passing u as a function representing the step function:
def first_order(y, t, tau, K, u):
dydt = (-y + K*u(t)) / tau
return dydt
t0 = 0
t1 = 10
intervals = 100
t = np.linspace(t0, t1, intervals)
tau = 5.0
K = 2.0
u = lambda t: 0 if t < 3 else 1
y = odeint(first_order, 0, t, args=(tau, K, u))
plt.plot(t, y)
Thanks for the tip!
Hi, what if we had different values for U at every timepoint? It will be difficult to call u(t) in the ODE because the integrator adapts the time steps.
Great suggestion as this is faster
So easy to understand. It helps a lot for beginners. Thank you so much.
I'm glad it helped. Here are additional tutorials: apmonitor.com/pdc/index.php/Main/SolveDifferentialEquations
Excellent video, easy to follow and customise to different applications!
to use varying input parameters in the ODE in general it is advised to provide the input values via an own function instead of storing them in a list which you build a loop around. This saves code and also avoids lower precision issues that arise when using large time steps (like those in the video) for the loop.
In this example the function would look somewhat like:
def u(t):
if t < 3:
return 0.0
else:
return 1.0
this way you can just use u(t) in the 'firstorder'-function and conveniently stick to the standard odeint syntax.
I appreciate that suggestion. One thing that I found with the ODEINT integrator is that even though a larger step size is requested the integrator will internally and adaptively modify the steps size to maintain accuracy and then only report the final value of the interval that is requested. I often use the models in control applications where we need to update the inputs frequently and at a given time interval. I teach the way you mentioned as a first approach if we don't need to have many successive input changes that would overwhelm a conditional statement in the model. apmonitor.com/pdc/index.php/Main/SolveDifferentialEquations One more benefit of your approach is that the integrator may even select larger step sizes than the loop increments if it is deemed that the accuracy can be maintained. This could save a lot of time for repeat calculations of points that could be interpolated.
Ah ok, I see. Thanks for the answer!
I seem to get an error when setting u = np.zeros(len(t)) . Get an error saying "RuntimeError: The size of the array returned by func (11) does not match the size of y0 (1)"
Any advice?
Try the source code here: apmonitor.com/pdc/index.php/Main/TankBlending There are also related examples here: apmonitor.com/pdc/index.php/Main/SolveDifferentialEquations or here: apmonitor.com/pdc/index.php/Main/StirredReactor
In mixer model Ca value is changing with every new iteration. Can you please explain how it is happening, why it's value is not replaced by dCadt.
dCadt is the derivative of the Ca value. Here is additional help with this problem: apmonitor.com/pdc/index.php/Main/ControlBlending
Great video! Around 14:25 you speak of accessing an array for the argument u. Is it possible to set up a seperate function that loops that array(11 terms) through your ode function, without manually updating that value one run at a time? I am currently struggling in my own research with a similar problem.
Sure, you could create some type of lookup function inside the derivative function. Here are other examples: apmonitor.com/pdc/index.php/Main/SolveDifferentialEquations
Thank you for wonderful lecture! I wonder what if can solve ODEs which is q(coefficient) is function for example function of temperature and there are equation for temperature too
Hye, excellent video. May i know how to do life ploting using odeint. Usually people doing life ploting graph using already existing datas and values.
Here is an example: apmonitor.com/pdc/index.php/Main/ArduinoTemperatureControl There are other examples at apmonitor.com/pdc as well.
Could you upload any video for partial differential equations (Diffusion).
Here are examples of hyperbolic and parabolic PDE solutions in Python: apmonitor.com/do/index.php/Main/PartialDifferentialEquations
@@apm Many thanks Prof.
Hello Sir, thank you for this fantastic tutorial. Just one thing I am really struggling to understand, what is meant by states shown at 27:39 , is it to do with state space form or something else? What confuses me is what is meant by Ca = x[0] and T = x[1] ? I would be grateful for your reply!
The states are the variables that show up at differential terms in the equations. Here is more information on state space: apmonitor.com/pdc/index.php/Main/StateSpaceModel (it may confuse you more, however).
@@apm Thank you for the prompt response Sir. I have a follow up question, if I run the source code attached in the link of your description, and I print the values of Ca inside the mixer function just after the line Ca = x[0], they are different compared to when I print them after Ca = odeint is solved. What is the difference between the values of Ca stored in array x[0] compared to the Ca values that you solve for using odeint?
@@Matt-lc6bs the values inside the function depend on what the ODEINT solver is requesting to solve the equations. It is after the solution is completed that you can use the values.
Here are some additional tutorials on ODEs: apmonitor.com/pdc/index.php/Main/SolveDifferentialEquations
@@apm Thank you very much Sir, your tutorials are excellent and appreciated by many of us!
Good explanation l want to try get similar results
how to solve non-linear differential equation in python?
Here are several example problems: apmonitor.com/pdc/index.php/Main/SolveDifferentialEquations If you have a large-scale system of nonlinear differential equations then I recommend Python Gekko.
Very helpful! Thank you so much.
Dear Professor,
Thanks for your lessons. I couldn't find the links that you have given here, I think the site was changed.
The URL for the example problem is accessible from a new Card at 00:12. I also put the link in the video description: apmonitor.com/pdc/index.php/Main/TankBlending If you are looking for information on how to download Python, there are tutorials here: apmonitor.com/pdc/index.php/Main/InstallPython
@@apm Thank you!
Nice video for beginners
Thank you, this really helps for me
Could you please make a video on th finite difference method? i'm having a lot of trouble implementing it for laplace's equation
Here are 3 methods. I think you need Method 2 for the finite difference method. apmonitor.com/che263/index.php/Main/PythonDynamicSim
hello, I am from Germany and a great fan of your videos and I want to ask you, wether it is possible to make a modeling and simulation video about an engineering application where the fluid momentum balance equation have to be applied.
Another very interesting problem to model and simulate, would be a simple hydraulic transmission system/ pump, hydraulic circuit, motor, where the hydraulic resistance, inductance and capacitance is present.
And could you show an example how you can solve optimization problems without the apm solver, in python and matlab
great channel, thanks for all your videos and the teaching of this interesting topics
Those are great suggestions!
Here is one video that shows a network of pipes (th-cam.com/video/Y7YBPqGKr9Y/w-d-xo.html) and solving for the fluid flow. A 2nd example is posted here: apmonitor.com/pdc/index.php/Main/FirstOrderOptimization as an example of how to use scipy.optimize.minimize - I'm currently teaching Process Dynamics and Control this semester so I'll likely have more on related topics. Thanks again for the suggestions.
Thanks for the answer and making references to the respective videos. I am very pleased, that my suggestions were noted, but I know of course that there have to be time for such additional topics besides the traditional lecture and your work. So if something of my suggestions would be eventually realized, it would be great:)
I know that your work has a strong chemical/ process engineering background and control theory is maybe more a means to an end, but if you have experiences in Nonlinear control and Robust Control and its applications in engineering and of course within the context of Matlab and python, that would be great.
Suggestions for the future:)
- state feedback linearization/ input output linearization
- Lyapunov based controller design
- lure system, if you ever heard from that
- Robust control (H_Infinity, Norms at all for controller design, Weighting transfer functions…)
- Sliding Mode Control
- Describing Functions
- LQR, LQG, Kalman filter
- Differential Geometry and its applications in Control Theory,Lie derivative/ brackets, Dynamical Systems on Manifolds/
for all this points, I not only want to learn the methodology(I learned about it in university, more than a less) I want to get an Intuition, a feeling how and why it works. This is my intention for all I am learning and it also should the intention of the teachers, I think
Thanks for all your work and giving people the chance to learn this cool engineering stuff.
and Excuse my english…
Saludos desde México.
Hello! I admire your time spent demonstrating the use of Python for application programming. Wonderful stuff! Do you have any examples with State Space differential equation with feedback??? For example,
dotx = Ax + Bu
y = Cx +Du for u = U_ref - Kx. K = const.
Could Python be used to iterate x through Newton's method (Jacobian Matrix) with saved results?
apmonitor.com/pdc/index.php/Main/StateSpaceModel
@@gemmfdz6116 Thank you
amazing man!
Gracias infinitas gracias.