Finding Zeros of Functions In Python (Bisection Method and Scipy)

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024
  • In this video I go over two root finding methods in python. I motivate the Bisection Method on paper before getting into how to write a program to implement it. At the end of the video, I show a quick way to find zeros using the fsolve function from the scipy library.

ความคิดเห็น • 60

  • @cicciobombo7496
    @cicciobombo7496 6 ปีที่แล้ว +39

    If you replace tol with 0 in the if else statement you get a much better accuracy

    • @AndrewDotsonvideos
      @AndrewDotsonvideos  6 ปีที่แล้ว +8

      Good catch! Thanks!

    • @zuzannagembus882
      @zuzannagembus882 4 ปีที่แล้ว +1

      @@AndrewDotsonvideos Where exactly? In 6:47?

    • @pengjieren9601
      @pengjieren9601 3 ปีที่แล้ว

      @@AndrewDotsonvideos lol, i was wondering that

    • @mpperfidy
      @mpperfidy 3 ปีที่แล้ว

      @@AndrewDotsonvideos You are near-criminally underfollowed. Always interesting content.

  • @DragasGaming
    @DragasGaming 6 ปีที่แล้ว +21

    You're doing gods work. Please more python videos.

  • @noellundstrom7447
    @noellundstrom7447 6 ปีที่แล้ว +13

    You really got to the root of my issue Andrew, thank you!

  • @williambrunosalesdepaulali6582
    @williambrunosalesdepaulali6582 3 ปีที่แล้ว

    Here i am discovering your channel again after so many years hahhas. Awesome!

  • @robertjcliu
    @robertjcliu 3 ปีที่แล้ว

    I am watching your tutorial in Taiwan. Thank you.

  • @aliciaroberts3965
    @aliciaroberts3965 2 ปีที่แล้ว

    This really helped me finish my python hw I put off to the last minute, so thank you!

  • @galenseilis5971
    @galenseilis5971 5 ปีที่แล้ว +16

    You can use "elif" instead of nesting "if" inside "else".

  • @muhammedabdulmalik3687
    @muhammedabdulmalik3687 2 ปีที่แล้ว

    Great job! You dissected it very well.

  • @al-washisarkerturan8913
    @al-washisarkerturan8913 5 หลายเดือนก่อน

    Described very well

  • @JoseGonzalez-cb2gw
    @JoseGonzalez-cb2gw 3 ปีที่แล้ว

    Thank you very much, excellent explanation of the procedure

  • @brianbecsi7482
    @brianbecsi7482 4 ปีที่แล้ว +1

    you call me smart... it goes straight to my head

  • @arthurheinrich723
    @arthurheinrich723 ปีที่แล้ว

    In the bissection method, if values "a" and "b" are root, or any "c" value makes f() function returns zero, the routine enter in an infinite loop

  • @janlhesternavasca3160
    @janlhesternavasca3160 3 ปีที่แล้ว +2

    What if you have two roots?

  • @bishwasgnawali
    @bishwasgnawali 5 ปีที่แล้ว +4

    Can you use this method to find a complex root of a complex function?

  • @ferns191
    @ferns191 5 ปีที่แล้ว

    Thank you so much!! You made this so easy to understand

  • @aureeel
    @aureeel 3 ปีที่แล้ว +1

    Please do more python videos, you make it so much easier to understand

  • @jptuser
    @jptuser 4 ปีที่แล้ว +1

    what's the use of the tolerance ??? we did the same code in fortran for computational physics but didn't understand that tolerance

  • @DRNi23
    @DRNi23 4 ปีที่แล้ว

    Useful video! Thank you!

  • @calebwhales
    @calebwhales 6 ปีที่แล้ว +1

    Love the python videos mate

  • @kamehamehaDdragon
    @kamehamehaDdragon 6 ปีที่แล้ว

    I love this kind of videos!

  • @danielriquelme5766
    @danielriquelme5766 4 ปีที่แล้ว

    I like your binary search xD good video

  • @ulilhidayat7467
    @ulilhidayat7467 4 ปีที่แล้ว

    Yuu're doing gods work, thanks

  • @uaenaarmy-enhyFim
    @uaenaarmy-enhyFim 5 ปีที่แล้ว

    Thank u! U saved my life

  • @iamwhitewhale
    @iamwhitewhale ปีที่แล้ว

    do u guys have any idea why my code isn't working? after running the code a star will appear in left side and then nothing, i need to re-run the jupyter.

  • @abhishankpaul
    @abhishankpaul ปีที่แล้ว

    I had tried so much to find roots of Bessel functions of 1st,2nd or 3rd order, everytime the code screwed up things

  • @speedspeed121
    @speedspeed121 6 ปีที่แล้ว +6

    Could you do Newtons method in Python?

  • @aniruddha4672
    @aniruddha4672 5 ปีที่แล้ว

    more python videos pls!!

  • @theflaggeddragon9472
    @theflaggeddragon9472 6 ปีที่แล้ว +1

    If there are multiple roots, the algorithm works regardless, you'll just find one of the roots instead of multiple. You could then search the other intervals by a bisection method to make sure you haven't missed any? (This doesn't work for arbitrary functions since you don't know when to stop, but polynomials, which have at most n roots for degree n, this would work pretty fast I think).

  • @johnchristian5027
    @johnchristian5027 6 ปีที่แล้ว

    Nice!

  • @megabyte3348
    @megabyte3348 3 ปีที่แล้ว +1

    Hello! Is there any book or free course that explain numerical methods in a simple way? What do you recommend?

    • @thisisFaye1
      @thisisFaye1 2 ปีที่แล้ว +2

      I'm using Computational Physics by Mark Newman right now and I find it's my most useful textbook. There are some accompanying materials posted by the author for free online.

    • @muhammedabdulmalik3687
      @muhammedabdulmalik3687 2 ปีที่แล้ว

      How can I get a pdf version please?

  • @phvisser1
    @phvisser1 2 ปีที่แล้ว

    how do you get 2 answers for x with the bisection method?

  • @mqfkkc6621
    @mqfkkc6621 3 ปีที่แล้ว

    hi, may i ask if there is another possible way to get two roots without using fsolve?

  • @kanadkrishanusengupta2578
    @kanadkrishanusengupta2578 4 ปีที่แล้ว

    Which is the best book to learn numerical analysis ?????

  • @GavinCOC
    @GavinCOC 5 ปีที่แล้ว

    thank you!!

  • @nitindogra6195
    @nitindogra6195 ปีที่แล้ว

    hey at 2:18 shouldn't it be () ? or may be i need to re-understand this, thanks

  • @QsHsNation1
    @QsHsNation1 6 ปีที่แล้ว

    Did you learn how to use python for physics mainly on your own or did your courses help you develop those skills? I'm going into my Junior year of physics (Quantum and Thermo this year!) and also starting my minor in comp sci, and I have very little knowledge of coding. Been trying to find resources online for learning to code for math and physics and what not, but i've been unsuccessful lol. We do have a course that works with coding for physics at my uni which I'll probably take next semester or next year. Just want to get ahead of the game though to be valuable during research.

    • @DrHami
      @DrHami 6 ปีที่แล้ว +2

      Computational Physics by Nicholas J. Giordano is a great textbook that may help you "learn to code for math and physics." I suspect that you already know, more or less, how to code a simple program (there's a shitload of resources for that), but you don't have experience in solving physics/math problems numerically. If that's the case, then that textbook might come in handy. (There's many ways to get that book online without paying a dime >_>)

    • @QsHsNation1
      @QsHsNation1 6 ปีที่แล้ว

      Thank you! Yeah, I understand the basics of coding, just don't know how to use the tools of it to solve problems. Alot of the libraries he imported in this video like matplotlib i didn't have so I guess I gotta figure out how to obtain that lol. I'll go check out that book *cough cough* by "purchasing it" *cough cough.

  • @anatolyrozhkov851
    @anatolyrozhkov851 4 ปีที่แล้ว +1

    8:00 How on Earth the first function and the second are connected? How does the bisection function understands that it needs to find a different root when the f(x) is changed to the quadratic????!!!

    • @UmurCanKaya
      @UmurCanKaya 4 ปีที่แล้ว +1

      If the interpreter can't find an object inside the function it starts to look for it in the main code. It would be much safer to give f as an argument of the bisection function.

  • @asifmahmud6331
    @asifmahmud6331 4 ปีที่แล้ว

    If I put "c = (xl + xr) / 2" line outside the while loop (just after xr = b), it takes forever to run. Can you tell me why is that?

    • @johnjohnerd6921
      @johnjohnerd6921 4 ปีที่แล้ว +1

      if you define c before the loop starts it will never get updated and therefore the greater than/less than conditions don't effect c.

  • @young_la_flame
    @young_la_flame 4 ปีที่แล้ว

    I was searching how to do the bisection search.
    how did I ended up here.

  • @alexanderrobertson5530
    @alexanderrobertson5530 6 ปีที่แล้ว

    Hey, Andrew I had an idea and I would like your input on it. I could be completely and utterly wrong, but I can't shake the idea so I want someone who knows some QM and SR to give me some feedback. Quantum field theory describes antimatter as matter moving backwards through time. If you look at a Feynman diagram of a matter-antimatter annihilation event the antimatter is described as moving in the negative time direction well matter goes in the positive time direction while being mediated by force carrying bosons. This is considered a mathematical trick that has no physical significance and leads to wacky joke theories like the one electron universe theory. Well what if antimatter is just regular matter that is tachyonic. Tachyons are a mathematical trick with lorentz transformations. You can't approach the speed of light because it requires infinite energy to get to because when v = c the entire equation is dividing by 0. Well if you approach it from the opposite direction so you start off being faster than the speed of light then it requires infinite energy to slow down for the exact same reason you can't speed up to being faster than light. When you do this you end up dealing with the square root of a negative number so an imaginary number. Well, we all know that travelling faster than the speed of light is equivalent to travelling backwards in time then is it possible that antimatter is simply the tachyonic equivalent of matter. If you trace a tachyonic worldline it reverses the direction of time. In QFT a little mathematical quirk is just considering antimatter to be time reversed matter so it is just regular matter travelling backwards through time just like a tachyon would. Thanks for any input.

  • @supercat908
    @supercat908 2 ปีที่แล้ว

    Ben Affleck doing python tutorials?

  • @jnscollier
    @jnscollier 5 ปีที่แล้ว

    Thanks Ryan Reynolds

  • @PauloVictor-hp9op
    @PauloVictor-hp9op 5 ปีที่แล้ว

    how can i proceed to find all roots of a function in some range (or interval)?

    • @PauloVictor-hp9op
      @PauloVictor-hp9op 5 ปีที่แล้ว

      fsolve can give us roots just in specified point (guesses).

  • @ariusmaximilian8291
    @ariusmaximilian8291 6 ปีที่แล้ว

    first

  • @googlegoogle1610
    @googlegoogle1610 2 ปีที่แล้ว

    there is always one typo hhhhhhhhhhhhhhh at least :)

  • @valentinewiggin9705
    @valentinewiggin9705 4 ปีที่แล้ว

    this code is so bad...