\

Solving First Order Differential Equations with Julia

82 points - last Monday at 6:41 PM

Source
  • forgotpwd16

    today at 2:36 AM

    The first example is also the very first example in the docs[1], but rather f!(du,u,p,t) that modifies du, it passes f(u,p,t) that returns du. Searching the help for ODEProblem() function, it's explained[2] that the first way is more memory-efficient, but if mutation is not allowed, the second way is more suitable. It will be of interest to write a follow-up post elaborating on this part.

    [1]: https://docs.sciml.ai/DiffEqDocs/stable/examples/classical_p... [2]: https://docs.sciml.ai/DiffEqDocs/stable/types/ode_types/#Sci...

      • __rito__

        today at 5:39 AM

        Author here.

        Yes, f(u,p,t) returning du is the suggested way to do things, but this pattern of updating du through in-place updating is more memory efficient when a system of equations (say 10-100) is involved.

        The suggested way, when we don't need to think about memory efficiency, is easier to write and reason about.

        I have plans to write more on this topic, and make it to a series. After I finish that, if there is enough interest, I will try and do a benchmark of mutating versus non-mutating styles with varying numbers of equations in a system of DEs.

        Thanks for reading the article and providing your opinion.

        • selimthegrim

          today at 3:07 AM

          I think I had volunteered to do something similar to this - let me check

      • anigbrowl

        yesterday at 11:16 PM

        X^2 - 3x - 18 =0

        [...]

        This is how the equation is created: x(x+3) = 18

        wat

        Maybe don't do very fast recaps if you're not going to proofread them. Incidentally I assume the formulae in this article were done with MathJax or its Julia equivalent, they render great but can't be copied from the text.

        Overall a good article (and a great ad for Julia) but stumbling blocks like the one above ensure some readers won't make it any farther.

          • grandempire

            today at 12:44 AM

            Is the issue here quadratic factorization is not obvious or that the sign is wrong?

            Note that x(x+3) = 18 and x(x-3) = 18 are valid models of the same problem. One finds the longer side in terms of the shorter, or vice versa

              • zombot

                today at 6:52 AM

                Both variants (+3 & -3) have the same shape, and so do their derivatives, but they do land in different places on the X axis. Is that irrelevant?

                • hansvm

                  today at 1:37 AM

                  Looks like proofreading (sign being wrong) to me.

              • beepbooptheory

                today at 3:18 AM

                Well if they made it that far, they also read that this article is for "people who are already familiar with Differential Equations from Mathematics." I think if you're already familiar, a single wrong sign is not something thats going to be particularly hindering. It's more just something to jump on if you are, for some reason, itching to criticize it.

            • nxpnsv

              today at 7:07 AM

              I didn't find a link to the package in the article. It seems to be a quite capable package, and the given two examples that are trivial to solve analytically don't really do it justice.

              • kelseyfrog

                today at 12:42 AM

                Good on the author for writing a blog post, but this is so elementary as to not be interesting.

                I wrote an ODE solver in GDScript in a weekend that uses RK4, implicit Euler, and BDF. It's not a terribly difficult thing to do and there are much more interesting things I'm sure you can do in Julia like use auto differentiation to easily get the Jacobian.

                  • gerdesj

                    today at 2:16 AM

                    "but this is so elementary as to not be interesting."

                    Well that's a bit desultory. The second stanza of the OP (post Intro) describes the potential Audience that they are aiming at.

                    You may have written something far more exciting but you forgot to publish it and link it here.

                    • __rito__

                      today at 5:57 AM

                      I know this is easy, but I still had to spend ~1 hour going through docs and videos- to start using the package in Julia comfortably . I think that writing it might help somebody in future. And as I use Python almost exclusively in my work, the blogpost also serves as a note for my future self.

                      I believe in some tenets when it comes to writing blogposts. Those tenets also have some elements regarding the choice of topic of a blogpost.

                      Here the key tenet was:

                      Would the content of my blogpost have been helpful to me a week ago? From [0]

                      [0]: https://jvns.ca/blog/2016/05/22/how-do-you-write-blog-posts/...

                      • gessha

                        today at 1:21 AM

                        People are at different stages of their learning journey and a blog post like this one might be a stepping stone for a blog post with some of the work that you’ve done.

                        • selimthegrim

                          today at 12:52 AM

                          Would that be worth a blogpost?

                            • kelseyfrog

                              today at 1:20 AM

                              No. I used o3-mini so it's not interesting but for different reasons. It's also not interesting unless you're into the equations of nuclear kinetics and fluid flow.

                      • fussypart

                        today at 5:26 AM

                        can anyone recommend a good textbook on differential equations?

                          • __rito__

                            today at 5:43 AM

                            Could you tell me a bit about your background, your level in mathematics, and the type of book you prefer? Do you enjoy highly rigorous, proof-heavy texts, or do you prefer books where the author explains concepts in a more conversational and detailed way—still rigorous, of course, but more wordy?

                              • seanhunter

                                today at 6:09 AM

                                I wish more people did this (get the required context) before steaming in with recommendations tbh.

                                That said, I will steam in with some starting points that may be helpful.

                                1) An “anti-recommendation”: Probably don’t get Edwards and Penney unless you are forced to for some course. It’s _fine_ I guess - like you will learn from it, but it is staggeringly overpriced for what it is and there are enough typesetting errors and other little niggles that grate when a book is as expensive as that. The one good part about it is there are tons of problems but for many/most of them it just gives the answer not a full solution, so it’s not very helpful if you are stuck or your solution looks very different from theirs and you don’t know where to go from there.

                                2) If you want a free pdf or online resource, mathematics libretexts has “Differential equations for engineers” by Jiri Lebl, which is at least as good as Edwards and Penney and is free and you get the pdf if you want to download it https://math.libretexts.org/Bookshelves/Differential_Equatio...

                                3) Dover Books publish “Ordinary Differential Equations” by Tennenbaum, Morris and Pollard, which I don’t have personally but a lot of people recommend. It’s a Dover book which means it is cheap and some of the terminology and notation is probably a little bit old-fashioned but it’s going to be a lot cheaper than Edwards and Penney if you want a physical book and as I say a lot of people recommend it.

                                So to complement what the parent said, one approach if you’re not sure what type of book you prefer, is check out Lebl (because it’s online and free so easy to dip into) and then you can explore from there.

                                But don’t get Edwards and Penney. I got a cheap second-hand copy and I still think I probably overpaid.

                                I would add get a CAS. You could use wxmaxima (which is OSS) or get Mathematica or something if you can get a cheap student license. It’s going to help a lot to develop intuition by allowing you to plot direction fields etc much more easily as well as doing some of the heavy lifting of verifying solutions etc (although you really need to do that a bunch yourself so you get good at it).

                                • fussypart

                                  today at 6:05 AM

                                  I have a CS degree but haven't done math in a few years. Last course I completed was an MITx - intro to probability. While I enjoyed its rigor and detail, I am looking for something practical without proofs.