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

Поділитися
Вставка
  • Опубліковано 11 лип 2018
  • 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.
  • Розваги

КОМЕНТАРІ • 61

  • @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 2 роки тому

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

  • @aliciaroberts3965
    @aliciaroberts3965 2 роки тому

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

  • @muhammedabdulmalik3687
    @muhammedabdulmalik3687 2 роки тому

    Great job! You dissected it very well.

  • @ferns191
    @ferns191 4 роки тому

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

  • @kamehamehaDdragon
    @kamehamehaDdragon 5 років тому

    I love this kind of videos!

  • @calebwhales
    @calebwhales 6 років тому +1

    Love the python videos mate

  • @robertjcliu
    @robertjcliu 3 роки тому

    I am watching your tutorial in Taiwan. Thank you.

  • @JoseGonzalez-cb2gw
    @JoseGonzalez-cb2gw 3 роки тому

    Thank you very much, excellent explanation of the procedure

  • @DRNi23
    @DRNi23 4 роки тому

    Useful video! Thank you!

  • @ulilhidayat7467
    @ulilhidayat7467 4 роки тому

    Yuu're doing gods work, thanks

  • @uaenaarmy-enhyFim
    @uaenaarmy-enhyFim 5 років тому

    Thank u! U saved my life

  • @bishwasgnawali
    @bishwasgnawali 5 років тому +3

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

  • @danielriquelme5766
    @danielriquelme5766 4 роки тому

    I like your binary search xD good video

  • @galenseilis5971
    @galenseilis5971 5 років тому +16

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

  • @al-washisarkerturan8913
    @al-washisarkerturan8913 4 місяці тому

    Described very well

  • @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

  • @mqfkkc6621
    @mqfkkc6621 3 роки тому

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

  • @brianbecsi7482
    @brianbecsi7482 4 роки тому +1

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

  • @kanadkrishanusengupta2578
    @kanadkrishanusengupta2578 4 роки тому

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

  • @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.

  • @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

  • @GavinCOC
    @GavinCOC 5 років тому

    thank you!!

  • @johnchristian5027
    @johnchristian5027 6 років тому

    Nice!

  • @phvisser1
    @phvisser1 2 роки тому

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

  • @janlhesternavasca3160
    @janlhesternavasca3160 3 роки тому +2

    What if you have two roots?

  • @aureeel
    @aureeel 3 роки тому +1

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

  • @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.

  • @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.

  • @aniruddha4672
    @aniruddha4672 4 роки тому

    more python videos pls!!

  • @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.

  • @speedspeed121
    @speedspeed121 6 років тому +6

    Could you do Newtons method in Python?

  • @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?

  • @young_la_flame
    @young_la_flame 3 роки тому

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

  • @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

  • @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).

  • @nitindogra6195
    @nitindogra6195 Рік тому

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

  • @PauloVictor-hp9op
    @PauloVictor-hp9op 4 роки тому

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

    • @PauloVictor-hp9op
      @PauloVictor-hp9op 4 роки тому

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

  • @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.

  • @asratmekonnenbelachew6225
    @asratmekonnenbelachew6225 4 роки тому

    Please it does not work for the function 2*x**2 - 5*x + 3 with xl=0 and xr=1.3. In this case, our solution must be x=1
    but using this code, we have the answer
    # Let assume that the root is found between x1=0 and x2=1.3
    x1=0
    x2=1.3
    y1=2*x1**2 - 5*x1+3
    y2=2*x2**2 - 5*x2+3
    if y1*y2>0:
    print("the root is not found in the interval")
    exit
    for bisection in range(1,101):
    xh=(x1 + x2)/2
    yh=2*xh**2 - 5*xh+3
    y1=2*x1**2 - 5*x1+3
    if abs(y1)

  • @jnscollier
    @jnscollier 5 років тому

    Thanks Ryan Reynolds

  • @ariusmaximilian8291
    @ariusmaximilian8291 6 років тому

    first

  • @supercat908
    @supercat908 2 роки тому

    Ben Affleck doing python tutorials?

  • @googlegoogle1610
    @googlegoogle1610 2 роки тому

    there is always one typo hhhhhhhhhhhhhhh at least :)

  • @valentinewiggin9705
    @valentinewiggin9705 4 роки тому

    this code is so bad...