{"id":1718,"date":"2022-12-13T20:49:53","date_gmt":"2022-12-14T04:49:53","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1718"},"modified":"2022-12-13T20:53:38","modified_gmt":"2022-12-14T04:53:38","slug":"204-mandelbrot-fractal-using-python-2-2-2-2-2-2","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2022\/12\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2\/","title":{"rendered":"#325 Finding the intersection points between two functions using python"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom scipy.optimize import fsolve\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\n\r\n# Defining function to simplify intersection solution\r\ndef findIntersection(func1, func2, x0):\r\n    return fsolve(lambda x : func1(x) - func2(x), x0)\r\n\r\n# Defining functions that will intersect\r\nfunky = lambda x : np.cos(x \/ 5) * np.sin(x \/ 2)\r\nline = lambda x : 0.01 * x - 0.5\r\n# Defining range and getting solutions on intersection points\r\nx = np.linspace(0,45,10000)\r\nresult = findIntersection(funky, line, &#x5B;15, 20, 30, 35, 40, 45])\r\n# Printing out results for x and y\r\nprint(result, line(result))\r\nplt.plot(x, funky(x),x,line(x))\r\nplt.scatter(result,line(result),color=&quot;red&quot;)\r\nplt.savefig('ex325.png', dpi=100)\r\nplt.show()\r\n<\/pre>\n<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2022\/12\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2\/ex325\/\" rel=\"attachment wp-att-1719\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=600%2C400&#038;ssl=1\" alt=\"\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-1719\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png 600w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325-480x320.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 600px, 100vw\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>from scipy.optimize import fsolve import numpy as np import matplotlib.pyplot as plt # Defining function to simplify intersection solution def findIntersection(func1, func2, x0): return fsolve(lambda x : func1(x) &#8211; func2(x), x0) # Defining functions that will intersect funky = lambda x : np.cos(x \/ 5) * np.sin(x \/ 2) line = lambda x : 0.01 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2],"tags":[3],"class_list":["post-1718","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-rI","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1153,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/201-polyfit-and-polyval-2-2\/","url_meta":{"origin":1718,"position":0},"title":"#203 Find minimum of Rosenbrock function using scipy.optimize","author":"gantovnik","date":"2021-11-23","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import scipy.optimize as so rosenbrockfunction = lambda x,y: (1-x)**2+100*(y-x**2)**2 X,Y = np.meshgrid(np.linspace(-.5,1.5,100), np.linspace(-1.5,3.,100)) Z = rosenbrockfunction(X,Y) plt.figure(figsize=(4,3), dpi=72) cs=plt.contour(X,Y,Z,np.logspace(-0.5,3.5,20,base=10),cmap='gray',linewidths=1.0) rosen=lambda x: rosenbrockfunction(x[0],x[1]) solution, iterates = so.fmin_powell(rosen,x0=np.array([0,-0.7]),retall=True) x,y=zip(*iterates) plt.plot(x,y,'.',c=\"red\") # plot black bullets plt.plot(x,y,':',c=\"red\",linewidth=1.5) # plot black dotted lines plt.clabel(cs) plt.savefig('ex203.png')\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex203.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1984,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/12\/398-find-points-of-intersection-of-two-circles\/","url_meta":{"origin":1718,"position":1},"title":"#398 Find points of intersection of two circles","author":"gantovnik","date":"2023-12-26","format":false,"excerpt":"- how to find an equation of the common chord of two intersected circles. [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import math def get_intersections(x0, y0, r0, x1, y1, r1): # circle 1: (x0, y0), radius r0 # circle 2: (x1, y1), radius r1 d=math.sqrt((x1-x0)**2 + (y1-y0)**2)\u2026","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":1109,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/193-animation-using-python\/","url_meta":{"origin":1718,"position":2},"title":"#193 Animation using python","author":"gantovnik","date":"2021-11-19","format":false,"excerpt":"[code language=\"python\"] # create an animation import numpy as np import matplotlib.pyplot as plt import matplotlib. Animation as manimation n = 1000 x = np.linspace(0, 6*np.pi, n) y = np.sin(x) # Define the meta data for the movie FFMpegWriter = manimation.writers[\"ffmpeg\"] metadata = dict(title=\"Movie Test\", artist=\"Matplotlib\", comment=\"a red circle following\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2175,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/425-an-animation-of-a-bouncing-ball-using-python\/","url_meta":{"origin":1718,"position":3},"title":"#425 An animation of a bouncing ball using python","author":"gantovnik","date":"2024-05-05","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import matplotlib. Animation as animation # Acceleration due to gravity, m.s-2. g = 9.81 # The maximum x-range of ball's trajectory to plot. XMAX = 5 # The coefficient of restitution for bounces (-v_up\/v_down). cor = 0.65 # The time\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1758,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/335-solution-of-nonlinear-equation-using-brent-method-in-python\/","url_meta":{"origin":1718,"position":4},"title":"#335 Solution of nonlinear equation by Brent&#8217;s method in python","author":"gantovnik","date":"2023-01-06","format":false,"excerpt":"Brent's method is a hybrid root-finding algorithm combining the bisection method, the secant method, and inverse quadratic interpolation. ex335.py [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import scipy.optimize as optimize def f(x): return 2*x - 1 + 2*np.cos(np.pi*x) x = np.linspace(0.0,2.0,201) y=f(x) plt.plot(x,y,label='$2x - 1 + 2\\\\cos(\\\\pi\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex335.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1746,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":1718,"position":5},"title":"#331 Interpolation with Newton&#8217;s polynomial using python","author":"gantovnik","date":"2023-01-04","format":false,"excerpt":"interpolation.py [code language=\"python\"] def interpolation(c,x,x0): # Evaluate Newton's polynomial at x0. # Degree of polynomial n = len(x) - 1 y0 = c[n] for k in range(1,n+1): y0 = c[n-k] + (x0 - x[n-k]) * y0 return y0 def coef(x,y): # Computes the coefficients of Newton's polynomial. # Number of\u2026","rel":"","context":"In &quot;interpolation&quot;","block_context":{"text":"interpolation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/interpolation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex331.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=1718"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1718\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}