Тёмный

How to Solve Differential Equations in PYTHON 

Mr. P Solver
Подписаться 134 тыс.
Просмотров 98 тыс.
50% 1

Check out my course on UDEMY: learn the skills you need for coding in STEM:
www.udemy.com/course/python-s...
Examined are first order ordinary differential equations (ODEs), coupled first order ODEs, and higher order ODEs.
All code can be found on Github:
github.com/lukepolson/youtube...
Discord
/ discord
Air friction While Falling Video:
• Skydiving with Frictio...
3 Body Motion Video:
• 3 Body Simulation in P...
0:00 Introduction
0:45 First Order ODEs
8:42 Coupled First Order ODEs
13:32 Second Order ODEs
16:12 Example: Coupled Higher Order Equations
19:52 Dealing with Messy ODEs...Be Careful

Наука

Опубликовано:

 

29 июн 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 88   
@flaviopineda1392
@flaviopineda1392 Год назад
I love your channel. I'm a PhD student in theoretical cosmology and I've always had troubles with computational part. Your videos have really helped me to improve my code skills. Please, don't stop making videos. Keep it up!
@MatUserName
@MatUserName Год назад
you are concised and cut through the basic elements of understanding. Not many have these skills, not even all teachers/professors.
@kananvirkar7360
@kananvirkar7360 9 месяцев назад
I am a physics student and these videos have been really helpful to help me solve physics problems using python. keep up the great work!
@Eighty_80_WAW
@Eighty_80_WAW 2 года назад
Great work. Very impressive approach as usual. You are the best! Keep it up. For the next work, please do data fitting and parameter estimation for epidemiological models (or system of ODEs) with data that has more than one column
@erfanbensaeed486
@erfanbensaeed486 Год назад
The best description I could've find in the internet. many thanks. I like the color of your eyes btw :))))
@upgraduate
@upgraduate Год назад
Very useful to apply for any first order and second order differential equations. Thank you so much.
@renegaed
@renegaed 2 года назад
Absolutely loved this video and learnt a lot. More basics videos please.
@AJ-et3vf
@AJ-et3vf 2 года назад
Awesome video once again! ❤️❤️❤️ I love differential equations. I would like to add that one can also use the Runge-Kutta-Nystrom method(s) to specifically solve 2nd order ODEs (or systems of 2nd order ODEs) directly without needing to transform them into a system of 1st order ODEs. 2nd order ODEs occurs often IRL that it was developed to specifically and directly solve ODEs. I compared the Runge-Kutta-Nystrom method to the classical fourth-order Runge-Kutta method and it's actually 1 to 3 orders of magnitude more accurate than the latter, while allowing you to directly solve a 2nd order ODE as it is.
@MrPSolver
@MrPSolver 2 года назад
Interesting, thank you AJ! I will take a look at these methods, may be interesting for a future video! And glad you're enjoying the channel :)
@AJ-et3vf
@AJ-et3vf 2 года назад
​@@MrPSolver (note: I already replied to you, but it didn't show ughhhh) Nice! I'm happy to hear that
@kshitijshah4754
@kshitijshah4754 2 года назад
extremely helpful Thank you, Mr. P Solver
@dsds-rj9rg
@dsds-rj9rg 2 года назад
Just a heads up, you don't need to do the transpose and then index to get the column, you can just select the column directly if you want using the correct numpy slice syntax, e.g. "sol[:, 0]"... Thanks for all the videos you make BTW they're super helpful
@AJ-et3vf
@AJ-et3vf 2 года назад
Yes, you're right, but it's actually more convenient to do than slicing the 2d NumPy array. When it is transposed, index just by 0, 1, 2, etc, allows you to immediately access the entire time series without needing to slice the array. I think this is why in the newer solve_ivp function, the y values are outputted in rows instead of columns like in the older, classic odeint function.
@blackguardian89
@blackguardian89 2 года назад
Yesss! Differential equations in Python - time for some numerical Rock'n'Roll :) Thanks, man!
@rollinas1
@rollinas1 Год назад
Oh god, I had to solve these by hand in my first year in Uni 15 years ago and it still gives me chills and cold sweat seeing them.
@brawnstein
@brawnstein 2 года назад
Great vid. How do you solve PDE in python, specially coupled PDEs?
@NTC
@NTC 2 года назад
awesome lecture! That's all I've been looking for today!
@sanketdave3086
@sanketdave3086 2 года назад
You are a lifesaver!
@Prometosermejor
@Prometosermejor 2 года назад
Thanks for the easy explanation!
@SarthakGamer
@SarthakGamer 2 года назад
You keep making amazing videos
@philipeleal9617
@philipeleal9617 2 года назад
Awesome stuff! Just loved your post. Would you have some study material which could further elucidate your examples? Ideally, I would like to know how to evaluate the success of a differential equation solver, as well as the stability of the model. Would you be so kind as to provide us with some material (if not a new youtube video) describing how to do that? SIncerely,
@joseftrojan7664
@joseftrojan7664 2 года назад
Very cool. Thanks!
@AZ-mi2wj
@AZ-mi2wj 7 месяцев назад
Really nicely explained!! 🎉❤
@dorfinhorg9458
@dorfinhorg9458 Год назад
Damn, this video is extremely informative and helpful for my research! Thanks dude!
@user-ng2jh9hv1u
@user-ng2jh9hv1u 8 месяцев назад
Thank you for such a wonderful video lesson. Request you to make a video on finding Lyapunov exponents of coupled differential equation. Thank you
@prateek3167
@prateek3167 5 месяцев назад
love you big bro, you really help me as a physics graduate
@dashoelorenz2677
@dashoelorenz2677 2 года назад
I love your style of teaching. I was wondering if you could also do a tutorial for LATEX? Thank you! :)
@mistercraftpiggy
@mistercraftpiggy 2 года назад
Hi classmate
@frankkoslowski6917
@frankkoslowski6917 2 года назад
Let's say `dfdx` is an expression of some equation. Plug that expression into the script below: latex_script = sp.latex(sp.S(dfdx, evaluate=False)) print(f'{latex_script}') It printed this without the `$$! I put those symbols in for you to use the code as is inside a Jupyter Markdown cell. Just copy and paste to see what the differential of f(x) of some equation looks like. Perhaps you might then want to find the anti-derivative and then stick that expression of f(x) into sympy.latex(), to see what a monster the function really is: $$ - 2 a x e^{- a \sin{\left(x^{2} ight)}} \log{\left(\frac{c \sin^{2}{\left(x ight)}}{x} ight)} \sin{\left(b^{x} ight)} \cos{\left(x^{2} ight)} + b^{x} e^{- a \sin{\left(x^{2} ight)}} \log{\left(b ight)} \log{\left(\frac{c \sin^{2}{\left(x ight)}}{x} ight)} \cos{\left(b^{x} ight)} + \frac{x \left(\frac{2 c \sin{\left(x ight)} \cos{\left(x ight)}}{x} - \frac{c \sin^{2}{\left(x ight)}}{x^{2}} ight) e^{- a \sin{\left(x^{2} ight)}} \sin{\left(b^{x} ight)}}{c \sin^{2}{\left(x ight)}} $$ This is of course LaTex script of the expression `dfdx`, generated by the sympy.latex() method. I found it to be a very elegant way of writing LaTex inside Jupyter Notebook's cells set to 'Markdown', instead of `Code`, which is the default setting. sympy.latex() has thus far provided me with perfectly accurate LaTex Script for any mathematical expression. Cool eh? Hope that will give you a kick start with LaTex.
@alekseykozin8108
@alekseykozin8108 Год назад
Brilliant as usual
@coldmilk7606
@coldmilk7606 2 года назад
Your vids are legendary!
@ujjwalchetan4907
@ujjwalchetan4907 11 месяцев назад
thanks for the useful content. The dependent variable in all the cases is only one. If it is more than one, what is the way out?
@renzy4584
@renzy4584 Год назад
Really useful. Thank U!
@Daniel_Zhu_a6f
@Daniel_Zhu_a6f 3 месяца назад
thank you, this was super helpful
@cyrilt9331
@cyrilt9331 Год назад
Thanks for sharing such useful tutorials! I've just a question please: I don't understand how to declare the vector S. Does it give the same result if I write S=(x,y) instead of S=x,y? Thanks !
@stephenumaoji9298
@stephenumaoji9298 11 месяцев назад
You are a good man. Keep it up.
@starkissed5795
@starkissed5795 Год назад
❤ UR ARE SUCH A LIFE-SAVER RN OMG PCE AND LOVE TO YOU AND YOUR FAMILY ❤
@frankkoslowski6917
@frankkoslowski6917 Год назад
Love your lectures, particularly in view of the `interp2d` depreciation and the host of wonderful alternatives scipy these days has to offer. 🤤 Clearly, there are people at work who really know their math.
@arahman3897
@arahman3897 2 года назад
Thank you Mr. P Solver Now I can solve ODEs in python. 🕺🕺
@dhaufjebzjchseis3828
@dhaufjebzjchseis3828 2 месяца назад
great video although gosh I cant even begin to wrap my head around the math behind this, im glad you treated it as a black box for the video. Do you have any resources if someone did want to learn the math behind how these are solved
@lajuelar
@lajuelar 2 года назад
I have a question for you sir. In your first example you write the initial condition v0=0 for v=0 at time t=0. How about : how do you write the initial condition for example v=5 at time t= 3?
@user-bs5ue3bu3p
@user-bs5ue3bu3p 7 месяцев назад
Thank you!
@haifeng.michaelyang4179
@haifeng.michaelyang4179 8 месяцев назад
Amazing 😍
@abc2008100
@abc2008100 Год назад
Thanks for the Great Video. Support in second order ode, we solve for y, for t=0,0.01... and now if we want y' and y'' how to get that.?
@alirezaghasrimanesh2431
@alirezaghasrimanesh2431 3 месяца назад
Great!❤
@erikp6614
@erikp6614 2 года назад
Isn't there an error at around 15:50? The definition of the diffeq should be dSdt(t,S): x,v = S ... !? Here it does not seem to matter since the diff eqs do not explicitly depend on t.
@MrPSolver
@MrPSolver 2 года назад
Yes, you are correct here! Should have dSdt(t, S) and not dSdx(x,S). The second one *technically* still gives the right solution in this case, but it is sloppy as it reuses the variable x and only confuses what's going on
@erikp6614
@erikp6614 2 года назад
@@MrPSolver You have a lot of very impressive videos on your channel! Nice!
@MrEo89
@MrEo89 2 года назад
If I may make a request, I’d love to see you do these again, but in Julia. Julia is the Rickest Rick.
@aliexpress.official
@aliexpress.official 2 года назад
Is Julia ripe enough for this? Does it have packages like scipy, sympy or IDE like jupyterlab?
@AJ-et3vf
@AJ-et3vf 2 года назад
@@aliexpress.official hi! Those are great questions! Julia actually has the DifferentialEquations.jl package for solving differential equations diffeq.sciml.ai/stable/ . It is much more extensive than SciPy with respect to ODEs because it has DOZENS more solvers for ODEs alone. You can also solve other kinds of Differential Equations with it such as Differential Algebraic Equations, Boundary-Value Problem ODEs, etc. There is also the ModelingToolkit.jl package that allows you to symbolically compose and model ODEs (and also Nonlinear System of Equations, PDEs, etc) then pass them on to DifferentialEquations.jl which will solve them numerically: mtk.sciml.ai/stable/ Julia has Symbolics.jl for its symbolic computing symbolics.juliasymbolics.org/stable/ . However, it's purpose is different than SymPy in that it's designed for symbolic-numeric computing symbolics.juliasymbolics.org/stable/comparison/ These packages I mentioned are part of the SciML packages that get updated very regularly like almost everyday.
@aliexpress.official
@aliexpress.official 2 года назад
@@AJ-et3vf thank you. I definitely need to give julia a shot
@AJ-et3vf
@AJ-et3vf 2 года назад
@@aliexpress.official VS Code and IJulia extension for JupyterLab/Notebook are your IDE options for Julia.
@foldedalpaca4990
@foldedalpaca4990 Год назад
​@@aliexpress.official fun fact the "ju" jupyter stands for julia. JUlia PYThon and R
@madmaths2568
@madmaths2568 2 года назад
good vid thank u
@martinnovak7988
@martinnovak7988 2 года назад
Hi, i got a question, please. I study nuclear engineering and we never did any programing. Our teacher at University want us to make a program for solving a second degree differential equation, using a shooting method or FEM and one or two others methods (we just pick one of them) (i do not know the name of the other methods in english). We will be given an equation during a semester and i have no idea, how am i suppose to make it. The problematics in this course is in form of the pure mathematics. So my question is, if it is possible to make a video on this topic? if not, i would like to ask, if you possibly have something i would be able to study these methods from. Unfortunately we also do not have any form of practice of programing and only programing that i do is the one from your videos. (they are amazing btw). Thank you very much.
@tristotech
@tristotech 2 года назад
Bro, I use Symbolab to solve associated legendre diferrential by substitution cos t = x, can we do that in Python?
@jimnoel3025
@jimnoel3025 Год назад
What if you have more than 1 variable? Like what if it was dYdt= x^2 +y^2
@acharyavivek51
@acharyavivek51 2 года назад
Greetings, brother. Please tell me your area of research. Love your work😊
@MrPSolver
@MrPSolver 2 года назад
Machine learning for tumour segmentation in medical physics (specifically PET and CT scans).
@acharyavivek51
@acharyavivek51 2 года назад
@@MrPSolver just brilliant brother👏👏👏👌👌 solve cancer for us 😃😃😃😃😃😃😃
@physicsworld1687
@physicsworld1687 7 месяцев назад
Love from India ❤
@varunsohanda2601
@varunsohanda2601 2 года назад
I have started with Numpy, Scipy, Sympy, Matplotlib, how should I practice these?
@AJ-et3vf
@AJ-et3vf 2 года назад
Watch tutorials in RU-vid, read the documentations for those packages, and get any math book preferably numerical methods book and solve the problems you find there.
@varunsohanda2601
@varunsohanda2601 2 года назад
@@AJ-et3vf Do you have book-suggestions?
@banditadas3420
@banditadas3420 Год назад
Hi I have a question, what if your right side function has its coefficient varies periodically...how to pass those argument! Is there any way I can contact you.
@drewnotdrey5156
@drewnotdrey5156 Год назад
What kind of integrator does odeint use? Runge-Kutta??
@nicolameoli
@nicolameoli 2 года назад
What if I have a second order equation and two conditions like v(x=0)=0 and v(x=L)=0 ? This is the case of a Beam/Truss solver problem in Structural engineering.
@AJ-et3vf
@AJ-et3vf 2 года назад
Hi! That is actually a boundary-value problem ODE that you're saying, specifically one with a Neumann boundary condition at the right endpoint because the derivative is specified at the right instead of the function value. For such ODEs, the common methods for them are the shooting method and the finite difference method. In the shooting method, you make guesses for the initial slope and try to "shoot" for the boundary value, essentially turning the bvp ODE into an ivp ODE. This shooting for the boundary value can be treated as a root-finding problem and you can use the SciPy root_scalar function for this, using either bracketing methods or newton-type methods, like secant method. You can plot the shooting function vs your initial guesses so you can know what a good initial guess for the slope should be. Once you got the right initial slope, you now solve the ODE like any other. The finite difference method ( ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-Upu3t8Ac2i8.html ) involves discretizing the 1st and 2nd derivatives using centered finite differences. You end up with a tridiagonal system if linear equations to solve. Note that this is doable only if the bvp ODE is linear. If it is nonlinear, you will have a system of nonlinear equations so you won't get the special tridiagonal system of linear equations from earlier. You can use the Gauss-Seidel method to iteratively solve the function values. You'll have to use Numba to speed up the for loop iterations An interesting method for solving bvp ODEs is the collocation method, applicable to both linear and nonlinear bvp ODEs: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-u8dVrzxTvSA.html . You approximate the solution with a polynomial at least quadratic or cubic order and solve for the coefficients by applying the boundary conditions and solving the ODE at "collocation points". Lastly, SciPy actually has a bvp ode solver named solve_bvp docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_bvp.html . It solves most bvp ODEs fine, but for bvp ODEs where either left or right boundary is actually a singularity due to either 1/x or 1/(x-b) being in the ODE, solve_bvp can end up failing because of the nature of the problem. The shooting method will also be very sensitive too for this, so this is where the finite difference method shines because it is more stable than the shooting method despite being more computationally expensive.
@nicolameoli
@nicolameoli 2 года назад
@@AJ-et3vf I know methods you cited but never tried to implement them. I'll check all the links you posted. It's very useful your reply. Thanks 🙏
@AJ-et3vf
@AJ-et3vf 2 года назад
@@nicolameoli I am very happy to be of great help! Here are more links and resources to help you man: Shooting Method ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-PDxYtycAUEQ.html ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-3hf2v39HJQE.html ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-0hwQEfAqsqM.html Orthogonal Collocation Method ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-jK2UtrrDO6U.html Finite Difference Method ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-qrS1L1VfP-k.html ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-OJ4SQuua-f0.html ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-Dxi0gc8-f8c.html Gauss-Seidel Finite Difference Method (In here, Mr P Solver solves Laplace' equation which is an elliptic PDE using numba-accelerated for loops. This is what I'm referring to when I mentioned the gauss-seidel method for the finite difference method solution of ODE bvps. ODE bvps after all are elliptic PDEs with only one independent variable so you have one less for-loop here. The "mesh" that you're solving is only one dimensional, not two like in elliptic PDEs.) ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-dKCAVteveYc.html
@rafisics
@rafisics 3 месяца назад
6:57 Why do we need to transpose the array to exact it?
@tomletomtom
@tomletomtom 2 года назад
Hey man. Im having a Center Problem for my Master thesis. Its about a System of differential equations for discribing a heat an moisture Transport trough insulation Materials. And i Need a approach. Is there a way to get in contact with you or is there a Platform ?
@MrPSolver
@MrPSolver 2 года назад
Come join the discord server and ask there!
@ns4235
@ns4235 2 года назад
time to make an EM field
@adarshkarekkat488
@adarshkarekkat488 2 года назад
ILY
@mastercan19952
@mastercan19952 Год назад
Can you make a video for Pandas?
@TheWrighteousOne
@TheWrighteousOne 6 месяцев назад
I hope you are still there. When you talk about solving higher order ODE’s you mean creating a new variable. How did you solve for the initial conditions for that new variable? V(0)=5
@mbgcenter
@mbgcenter 2 года назад
like it
@frankkoslowski6917
@frankkoslowski6917 2 года назад
In regards to your initializations. . . # v_0 = 0 # This one does not appear to exist at all! Whereas one had imagined, `S_0` ought to be defined in terms of all previously initialized variables: S_0 = (x1_0, v1_0, a1_0, x2_0, v2_0, a2_0) So as to be congruent with vector-matrix `S`. Should it not?
@natorgas
@natorgas Год назад
Why are you saying that the very first dv/dt doesn't depend on t? Doesn't v depend on t? If thats true then everything that depends on v also depends on t or not?
@ryanthegooch5952
@ryanthegooch5952 Год назад
FUCKING AMAZING VIDEO
@user-lc6jq1hi1r
@user-lc6jq1hi1r 2 года назад
you're a good boy, mr P. I like the beard.
@MrPSolver
@MrPSolver 2 года назад
Thanks buddy!
@h.e.a311
@h.e.a311 2 года назад
your place at the university not here on youtube channel take revenge on, from them.
@zhukunwang5496
@zhukunwang5496 Год назад
Thanks so much for the tutorial. Do you have videos for using python solving partial differential equations?
Далее
All Types of Fourier Transforms in PYTHON
30:44
Просмотров 51 тыс.
This is why you're learning differential equations
18:36
The last one surprised me! 👀 🎈
00:30
Просмотров 3 млн
1st Year Calculus, But in PYTHON
32:13
Просмотров 120 тыс.
Differential equations, a tourist's guide | DE1
27:16
When Your Code Runs Without Errors...
2:23
Просмотров 115 тыс.
Grad School Be Like...
3:35
Просмотров 96 тыс.
Ordinary Differential Equations - SymPy Tutorial 10
12:42
Curve Fitting in Python (2022)
24:50
Просмотров 89 тыс.
Optimize with Python
38:59
Просмотров 12 тыс.
Самый СТРАННЫЙ смартфон!
0:57
Просмотров 35 тыс.