How to Solve Differential Equations in PYTHON

  Рет қаралды 113,114

Mr. P Solver

Mr. P Solver

Күн бұрын

Пікірлер: 91
@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!
@kananvirkar7360
@kananvirkar7360 Жыл бұрын
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!
@AJ-et3vf
@AJ-et3vf 3 жыл бұрын
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 3 жыл бұрын
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 3 жыл бұрын
​@@MrPSolver (note: I already replied to you, but it didn't show ughhhh) Nice! I'm happy to hear that
@erikp6614
@erikp6614 3 жыл бұрын
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 3 жыл бұрын
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 3 жыл бұрын
@@MrPSolver You have a lot of very impressive videos on your channel! Nice!
@dsds-rj9rg
@dsds-rj9rg 3 жыл бұрын
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 3 жыл бұрын
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.
@MatUserName
@MatUserName 2 жыл бұрын
you are concised and cut through the basic elements of understanding. Not many have these skills, not even all teachers/professors.
@rollinas1
@rollinas1 2 жыл бұрын
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 3 жыл бұрын
Great vid. How do you solve PDE in python, specially coupled PDEs?
@rafisics
@rafisics 9 ай бұрын
6:57 Why do we need to transpose the array to exact it?
@Eighty_80_WAW
@Eighty_80_WAW 3 жыл бұрын
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 :))))
@blackguardian89
@blackguardian89 3 жыл бұрын
Yesss! Differential equations in Python - time for some numerical Rock'n'Roll :) Thanks, man!
@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?
@RanjibBanerjee
@RanjibBanerjee Жыл бұрын
Thank you for such a wonderful video lesson. Request you to make a video on finding Lyapunov exponents of coupled differential equation. Thank you
@dashoelorenz2677
@dashoelorenz2677 3 жыл бұрын
I love your style of teaching. I was wondering if you could also do a tutorial for LATEX? Thank you! :)
@mistercraftpiggy
@mistercraftpiggy 3 жыл бұрын
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.
@varunsohanda2601
@varunsohanda2601 3 жыл бұрын
I have started with Numpy, Scipy, Sympy, Matplotlib, how should I practice these?
@AJ-et3vf
@AJ-et3vf 3 жыл бұрын
Watch tutorials in KZbin, read the documentations for those packages, and get any math book preferably numerical methods book and solve the problems you find there.
@varunsohanda2601
@varunsohanda2601 3 жыл бұрын
@@AJ-et3vf Do you have book-suggestions?
@prateek3167
@prateek3167 11 ай бұрын
love you big bro, you really help me as a physics graduate
@dhaufjebzjchseis3828
@dhaufjebzjchseis3828 8 ай бұрын
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
@jimnoel3025
@jimnoel3025 Жыл бұрын
What if you have more than 1 variable? Like what if it was dYdt= x^2 +y^2
@zhukunwang5496
@zhukunwang5496 2 жыл бұрын
Thanks so much for the tutorial. Do you have videos for using python solving partial differential equations?
@ernstsauer1978
@ernstsauer1978 2 ай бұрын
Excellent video, thank you. I have two questions, I have a beam with a fourth order differential equation but with boundary conditions at both ends. Is such a problem solvable in python? My second question: can a function have discontinuities like \/? Greetings from Bavaria.
@drewnotdrey5156
@drewnotdrey5156 Жыл бұрын
What kind of integrator does odeint use? Runge-Kutta??
@nicolameoli
@nicolameoli 3 жыл бұрын
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 3 жыл бұрын
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 ( kzbin.info/www/bejne/i6HYZKdudshln5o ) 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: kzbin.info/www/bejne/q2nHh6WwrbmpiaM . 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 3 жыл бұрын
@@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 3 жыл бұрын
@@nicolameoli I am very happy to be of great help! Here are more links and resources to help you man: Shooting Method kzbin.info/www/bejne/hnXbiqevmKaIe7M kzbin.info/www/bejne/aZnJY6lpbq19h6c kzbin.info/www/bejne/Zpnagnicdtamp68 Orthogonal Collocation Method kzbin.info/www/bejne/oHyVhqeop6mCbLc Finite Difference Method kzbin.info/www/bejne/p6O2Yn9ni8uDY80 kzbin.info/www/bejne/hXuXhISrqsZgnJI kzbin.info/www/bejne/eqnMYZqZbZKZbsU 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.) kzbin.info/www/bejne/mnymcomqmtuYj8U
@ujjwalchetan4907
@ujjwalchetan4907 Жыл бұрын
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?
@ymharshe81
@ymharshe81 4 ай бұрын
can you make a video on PDEs also?
@banditadas3420
@banditadas3420 2 жыл бұрын
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.
@zktec1
@zktec1 9 күн бұрын
Dude that was fantastic! Very helpful, thank you. Subscribed
@abc2008100
@abc2008100 2 жыл бұрын
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.?
@kshitijshah4754
@kshitijshah4754 3 жыл бұрын
extremely helpful Thank you, Mr. P Solver
@philipeleal9617
@philipeleal9617 3 жыл бұрын
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,
@upgraduate
@upgraduate 2 жыл бұрын
Very useful to apply for any first order and second order differential equations. Thank you so much.
@gunsnroses82
@gunsnroses82 3 ай бұрын
Unrelated but your camera quality is unreal!
@sanketdave3086
@sanketdave3086 3 жыл бұрын
You are a lifesaver!
@arahman3897
@arahman3897 2 жыл бұрын
Thank you Mr. P Solver Now I can solve ODEs in python. 🕺🕺
@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?
@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.
@starkissed5795
@starkissed5795 2 жыл бұрын
❤ UR ARE SUCH A LIFE-SAVER RN OMG PCE AND LOVE TO YOU AND YOUR FAMILY ❤
@acharyavivek51
@acharyavivek51 3 жыл бұрын
Greetings, brother. Please tell me your area of research. Love your work😊
@MrPSolver
@MrPSolver 3 жыл бұрын
Machine learning for tumour segmentation in medical physics (specifically PET and CT scans).
@acharyavivek51
@acharyavivek51 3 жыл бұрын
@@MrPSolver just brilliant brother👏👏👏👌👌 solve cancer for us 😃😃😃😃😃😃😃
@SarthakGamer
@SarthakGamer 3 жыл бұрын
You keep making amazing videos
@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?
@tristotech
@tristotech 3 жыл бұрын
Bro, I use Symbolab to solve associated legendre diferrential by substitution cos t = x, can we do that in Python?
@renegaed
@renegaed 3 жыл бұрын
Absolutely loved this video and learnt a lot. More basics videos please.
@cyrilt9331
@cyrilt9331 2 жыл бұрын
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 !
@AZ-mi2wj
@AZ-mi2wj Жыл бұрын
Really nicely explained!! 🎉❤
@MrEo89
@MrEo89 3 жыл бұрын
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 3 жыл бұрын
Is Julia ripe enough for this? Does it have packages like scipy, sympy or IDE like jupyterlab?
@AJ-et3vf
@AJ-et3vf 3 жыл бұрын
@@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 3 жыл бұрын
@@AJ-et3vf thank you. I definitely need to give julia a shot
@AJ-et3vf
@AJ-et3vf 3 жыл бұрын
@@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
@dorfinhorg9458
@dorfinhorg9458 2 жыл бұрын
Damn, this video is extremely informative and helpful for my research! Thanks dude!
@mastercan19952
@mastercan19952 2 жыл бұрын
Can you make a video for Pandas?
@tomletomtom
@tomletomtom 3 жыл бұрын
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 3 жыл бұрын
Come join the discord server and ask there!
@NTC
@NTC 2 жыл бұрын
awesome lecture! That's all I've been looking for today!
@TheWrighteousOne
@TheWrighteousOne Жыл бұрын
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
@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.
@Prometosermejor
@Prometosermejor 2 жыл бұрын
Thanks for the easy explanation!
@physicsworld1687
@physicsworld1687 Жыл бұрын
Love from India ❤
@joseftrojan7664
@joseftrojan7664 3 жыл бұрын
Very cool. Thanks!
@stephenumaoji9298
@stephenumaoji9298 Жыл бұрын
You are a good man. Keep it up.
@alekseykozin8108
@alekseykozin8108 Жыл бұрын
Brilliant as usual
@coldmilk7606
@coldmilk7606 2 жыл бұрын
Your vids are legendary!
@ns4235
@ns4235 3 жыл бұрын
time to make an EM field
@MongolianBatman
@MongolianBatman 5 ай бұрын
Just do Laplace transform of all of them then numerically invert them back
@Daniel_Zhu_a6f
@Daniel_Zhu_a6f 9 ай бұрын
thank you, this was super helpful
@renzy4584
@renzy4584 2 жыл бұрын
Really useful. Thank U!
@youkaihou
@youkaihou Жыл бұрын
Amazing 😍
@羊要吃草
@羊要吃草 Жыл бұрын
Thank you!
@alirezaghasrimanesh2431
@alirezaghasrimanesh2431 9 ай бұрын
Great!❤
@h.e.a311
@h.e.a311 3 жыл бұрын
your place at the university not here on youtube channel take revenge on, from them.
@madmaths2568
@madmaths2568 2 жыл бұрын
good vid thank u
@adarshkarekkat488
@adarshkarekkat488 2 жыл бұрын
ILY
@ryanthegooch5952
@ryanthegooch5952 2 жыл бұрын
FUCKING AMAZING VIDEO
@mbgcenter
@mbgcenter 2 жыл бұрын
like it
@bjornthomas4632
@bjornthomas4632 2 ай бұрын
good video thanks! but you are very intense
All Types of Fourier Transforms in PYTHON
30:44
Mr. P Solver
Рет қаралды 58 М.
Ordinary Differential Equations - SymPy Tutorial 10
12:42
TM Quest
Рет қаралды 16 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
Differential equations, a tourist's guide | DE1
27:16
3Blue1Brown
Рет қаралды 4,2 МЛН
Integration in PYTHON (Symbolic AND Numeric)
15:20
Mr. P Solver
Рет қаралды 87 М.
This is why you're learning differential equations
18:36
Zach Star
Рет қаралды 3,6 МЛН
Solving Partial Differential Equations in Python
6:05
NeuralNine
Рет қаралды 6 М.
Why Runge-Kutta is SO Much Better Than Euler's Method #somepi
13:32
Phanimations
Рет қаралды 162 М.
SciPy Tutorial: For Physicists, Engineers, and Mathematicians
1:33:29
Mr. P Solver
Рет қаралды 159 М.
1st Year Calculus, But in PYTHON
32:13
Mr. P Solver
Рет қаралды 133 М.
How to Solve Coupled Differential Equations ODEs in Python
10:12
Vincent Stevenson
Рет қаралды 54 М.
SymPy is REVOLUTIONARY For Mathematical Tasks
25:15
NeuralNine
Рет қаралды 70 М.
Solving Differential Equations using scipy.odeint in Python!
25:10
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН