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!
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.
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
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.
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.
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
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
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.
Watch tutorials in UA-cam, read the documentations for those packages, and get any math book preferably numerical methods book and solve the problems you find there.
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.
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?
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.
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 ( ua-cam.com/video/Upu3t8Ac2i8/v-deo.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: ua-cam.com/video/u8dVrzxTvSA/v-deo.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 I am very happy to be of great help! Here are more links and resources to help you man: Shooting Method ua-cam.com/video/PDxYtycAUEQ/v-deo.html ua-cam.com/video/3hf2v39HJQE/v-deo.html ua-cam.com/video/0hwQEfAqsqM/v-deo.html Orthogonal Collocation Method ua-cam.com/video/jK2UtrrDO6U/v-deo.html Finite Difference Method ua-cam.com/video/qrS1L1VfP-k/v-deo.html ua-cam.com/video/OJ4SQuua-f0/v-deo.html ua-cam.com/video/Dxi0gc8-f8c/v-deo.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.) ua-cam.com/video/dKCAVteveYc/v-deo.html
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.
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.
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
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,
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?
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 !
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.
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?
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 ?
@@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.
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
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!
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.
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 :)
@@MrPSolver (note: I already replied to you, but it didn't show ughhhh)
Nice! I'm happy to hear that
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!
you are concised and cut through the basic elements of understanding. Not many have these skills, not even all teachers/professors.
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
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.
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.
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
@@MrPSolver You have a lot of very impressive videos on your channel! Nice!
The best description I could've find in the internet. many thanks. I like the color of your eyes btw :))))
Yesss! Differential equations in Python - time for some numerical Rock'n'Roll :) Thanks, man!
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.
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
extremely helpful
Thank you, Mr. P Solver
Great vid.
How do you solve PDE in python, specially coupled PDEs?
love you big bro, you really help me as a physics graduate
You are a lifesaver!
Dude that was fantastic! Very helpful, thank you. Subscribed
Very useful to apply for any first order and second order differential equations. Thank you so much.
6:57 Why do we need to transpose the array to exact it?
You keep making amazing videos
❤ UR ARE SUCH A LIFE-SAVER RN OMG PCE AND LOVE TO YOU AND YOUR FAMILY ❤
I love your style of teaching. I was wondering if you could also do a tutorial for LATEX? Thank you! :)
Hi classmate
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.
I have started with Numpy, Scipy, Sympy, Matplotlib, how should I practice these?
Watch tutorials in UA-cam, read the documentations for those packages, and get any math book preferably numerical methods book and solve the problems you find there.
@@AJ-et3vf Do you have book-suggestions?
Thank you for such a wonderful video lesson. Request you to make a video on finding Lyapunov exponents of coupled differential equation.
Thank you
Thank you Mr. P Solver Now I can solve ODEs in python. 🕺🕺
Absolutely loved this video and learnt a lot. More basics videos please.
awesome lecture! That's all I've been looking for today!
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.
Damn, this video is extremely informative and helpful for my research! Thanks dude!
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?
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.
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 ( ua-cam.com/video/Upu3t8Ac2i8/v-deo.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: ua-cam.com/video/u8dVrzxTvSA/v-deo.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.
@@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 🙏
@@nicolameoli I am very happy to be of great help! Here are more links and resources to help you man:
Shooting Method
ua-cam.com/video/PDxYtycAUEQ/v-deo.html
ua-cam.com/video/3hf2v39HJQE/v-deo.html
ua-cam.com/video/0hwQEfAqsqM/v-deo.html
Orthogonal Collocation Method
ua-cam.com/video/jK2UtrrDO6U/v-deo.html
Finite Difference Method
ua-cam.com/video/qrS1L1VfP-k/v-deo.html
ua-cam.com/video/OJ4SQuua-f0/v-deo.html
ua-cam.com/video/Dxi0gc8-f8c/v-deo.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.)
ua-cam.com/video/dKCAVteveYc/v-deo.html
Really nicely explained!! 🎉❤
Very cool. Thanks!
Thanks for the easy explanation!
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?
What kind of integrator does odeint use? Runge-Kutta??
Love from India ❤
You are a good man. Keep it up.
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.?
What if you have more than 1 variable? Like what if it was dYdt= x^2 +y^2
thank you, this was super helpful
Thanks so much for the tutorial. Do you have videos for using python solving partial differential equations?
Brilliant as usual
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.
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.
Your vids are legendary!
can you make a video on PDEs also?
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
Bro, I use Symbolab to solve associated legendre diferrential by substitution cos t = x, can we do that in Python?
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,
Unrelated but your camera quality is unreal!
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?
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 !
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.
Greetings, brother. Please tell me your area of research. Love your work😊
Machine learning for tumour segmentation in medical physics (specifically PET and CT scans).
@@MrPSolver just brilliant brother👏👏👏👌👌 solve cancer for us 😃😃😃😃😃😃😃
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?
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 ?
Come join the discord server and ask there!
Really useful. Thank U!
If I may make a request, I’d love to see you do these again, but in Julia. Julia is the Rickest Rick.
Is Julia ripe enough for this? Does it have packages like scipy, sympy or IDE like jupyterlab?
@@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.
@@AJ-et3vf thank you. I definitely need to give julia a shot
@@aliexpress.official VS Code and IJulia extension for JupyterLab/Notebook are your IDE options for Julia.
@@aliexpress.official fun fact the "ju" jupyter stands for julia. JUlia PYThon and R
Can you make a video for Pandas?
Thank you!
time to make an EM field
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
Just do Laplace transform of all of them then numerically invert them back
Amazing 😍
Great!❤
ILY
good vid thank u
FUCKING AMAZING VIDEO
like it
your place at the university not here on youtube channel take revenge on, from them.
good video thanks! but you are very intense