How to Solve Differential Equations in PYTHON

  Рет қаралды 97,368

Mr. P Solver

Mr. P Solver

Күн бұрын

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

Пікірлер: 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!
@erfanbensaeed486
@erfanbensaeed486 Жыл бұрын
The best description I could've find in the internet. many thanks. I like the color of your eyes btw :))))
@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
@blackguardian89
@blackguardian89 2 жыл бұрын
Yesss! Differential equations in Python - time for some numerical Rock'n'Roll :) Thanks, man!
@kshitijshah4754
@kshitijshah4754 2 жыл бұрын
extremely helpful Thank you, Mr. P Solver
@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.
@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.
@NTC
@NTC 2 жыл бұрын
awesome lecture! That's all I've been looking for today!
@Prometosermejor
@Prometosermejor 2 жыл бұрын
Thanks for the easy explanation!
@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
@sanketdave3086
@sanketdave3086 2 жыл бұрын
You are a lifesaver!
@SarthakGamer
@SarthakGamer 2 жыл бұрын
You keep making amazing videos
@prateek3167
@prateek3167 5 ай бұрын
love you big bro, you really help me as a physics graduate
@dorfinhorg9458
@dorfinhorg9458 Жыл бұрын
Damn, this video is extremely informative and helpful for my research! Thanks dude!
@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.
@joseftrojan7664
@joseftrojan7664 2 жыл бұрын
Very cool. Thanks!
@AZ-mi2wj
@AZ-mi2wj 7 ай бұрын
Really nicely explained!! 🎉❤
@coldmilk7606
@coldmilk7606 2 жыл бұрын
Your vids are legendary!
@alekseykozin8108
@alekseykozin8108 Жыл бұрын
Brilliant as usual
@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
@Daniel_Zhu_a6f
@Daniel_Zhu_a6f 3 ай бұрын
thank you, this was super helpful
@brawnstein
@brawnstein 2 жыл бұрын
Great vid. How do you solve PDE in python, specially coupled PDEs?
@starkissed5795
@starkissed5795 Жыл бұрын
❤ UR ARE SUCH A LIFE-SAVER RN OMG PCE AND LOVE TO YOU AND YOUR FAMILY ❤
@stephenumaoji9298
@stephenumaoji9298 11 ай бұрын
You are a good man. Keep it up.
@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,
@arahman3897
@arahman3897 2 жыл бұрын
Thank you Mr. P Solver Now I can solve ODEs in python. 🕺🕺
@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.
@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.
@renzy4584
@renzy4584 Жыл бұрын
Really useful. Thank U!
@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?
@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
@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 !
@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!
@tristotech
@tristotech 2 жыл бұрын
Bro, I use Symbolab to solve associated legendre diferrential by substitution cos t = x, can we do that in Python?
@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?
@physicsworld1687
@physicsworld1687 7 ай бұрын
Love from India ❤
@rafisics
@rafisics 3 ай бұрын
6:57 Why do we need to transpose the array to exact it?
@drewnotdrey5156
@drewnotdrey5156 Жыл бұрын
What kind of integrator does odeint use? Runge-Kutta??
@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.
@jimnoel3025
@jimnoel3025 Жыл бұрын
What if you have more than 1 variable? Like what if it was dYdt= x^2 +y^2
@madmaths2568
@madmaths2568 2 жыл бұрын
good vid thank u
@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!
@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
@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.
@varunsohanda2601
@varunsohanda2601 2 жыл бұрын
I have started with Numpy, Scipy, Sympy, Matplotlib, how should I practice these?
@AJ-et3vf
@AJ-et3vf 2 жыл бұрын
Watch tutorials in KZfaq, 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?
@mastercan19952
@mastercan19952 Жыл бұрын
Can you make a video for Pandas?
@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 😃😃😃😃😃😃😃
@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 ( kzfaq.info/get/bejne/i9alZtdppsmWmWw.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: kzfaq.info/get/bejne/q56UidWr3brag3U.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 kzfaq.info/get/bejne/hqqojNeqyKe5dYU.html kzfaq.info/get/bejne/ac6WZdlknq6ugXk.html kzfaq.info/get/bejne/Zs6nhKiXptfXoYE.html Orthogonal Collocation Method kzfaq.info/get/bejne/oLFiiNej16qzZok.html Finite Difference Method kzfaq.info/get/bejne/p9iDZK9iu8y0XZ8.html kzfaq.info/get/bejne/hbBkhrSm2seRlmQ.html kzfaq.info/get/bejne/et6ZY8qUnZPKaJc.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.) kzfaq.info/get/bejne/mrFzdLmlytzJiZc.html
@ns4235
@ns4235 2 жыл бұрын
time to make an EM field
@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
@adarshkarekkat488
@adarshkarekkat488 2 жыл бұрын
ILY
@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?
@mbgcenter
@mbgcenter 2 жыл бұрын
like it
@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
Mr. P Solver
Рет қаралды 51 М.
This is why you're learning differential equations
18:36
Zach Star
Рет қаралды 3,4 МЛН
small vs big hoop #tiktok
00:12
Анастасия Тарасова
Рет қаралды 19 МЛН
Increíble final 😱
00:37
Juan De Dios Pantoja 2
Рет қаралды 106 МЛН
터키아이스크림🇹🇷🍦Turkish ice cream #funny #shorts
00:26
Byungari 병아리언니
Рет қаралды 25 МЛН
MEGA BOXES ARE BACK!!!
08:53
Brawl Stars
Рет қаралды 33 МЛН
Integration in PYTHON (Symbolic AND Numeric)
15:20
Mr. P Solver
Рет қаралды 78 М.
Differential equations, a tourist's guide | DE1
27:16
3Blue1Brown
Рет қаралды 4 МЛН
When Your Code Runs Without Errors...
2:23
Mr. P Solver
Рет қаралды 115 М.
Making a Game in Python with No Experience
5:57
Goodgis
Рет қаралды 1,6 МЛН
Ordinary Differential Equations - SymPy Tutorial 10
12:42
TM Quest
Рет қаралды 14 М.
Solving Differential Equations using scipy.odeint in Python!
25:10
1st Year Calculus, But in PYTHON
32:13
Mr. P Solver
Рет қаралды 120 М.
Curve Fitting in Python (2022)
24:50
Mr. P Solver
Рет қаралды 88 М.
Solving PDEs with the FFT [Python]
14:56
Steve Brunton
Рет қаралды 49 М.
Хотела заскамить на Айфон!😱📱(@gertieinar)
0:21
Взрывная История
Рет қаралды 3,9 МЛН
#miniphone
0:16
Miniphone
Рет қаралды 3,6 МЛН
Will the battery emit smoke if it rotates rapidly?
0:11
Meaningful Cartoons 183
Рет қаралды 31 МЛН