{"id":2471,"date":"2024-07-14T16:12:16","date_gmt":"2024-07-14T23:12:16","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=2471"},"modified":"2024-07-14T16:32:30","modified_gmt":"2024-07-14T23:32:30","slug":"435-thomaes-function-using-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/435-thomaes-function-using-python\/","title":{"rendered":"#435 Thomae\u2019s function using python"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/435-thomaes-function-using-python\/ex435\/\" rel=\"attachment wp-att-2472\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex435.png?resize=600%2C400&#038;ssl=1\" alt=\"\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-2472\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex435.png 600w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex435-480x320.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 600px, 100vw\" \/><\/a><\/p>\n<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/435-thomaes-function-using-python\/ex435_2\/\" rel=\"attachment wp-att-2475\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex435_2.png?resize=839%2C186&#038;ssl=1\" alt=\"\" width=\"839\" height=\"186\" class=\"alignnone size-full wp-image-2475\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex435_2.png 839w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex435_2-480x106.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 839px, 100vw\" \/><\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nimport matplotlib.pyplot as plt\nxlist = &#x5B;]\nylist = &#x5B;]\nfor q in range(2,1000):\n    for p in range(1,q):\n        if np.gcd(p,q) == 1:\n            xlist.append(p\/q)\n            ylist.append(1\/q)\n\nplt.plot(xlist, ylist, &#039;.r&#039;, ms=3)\nplt.xlabel(&#039;x&#039;)\nplt.ylabel(&#039;y&#039;)\nplt.xlim(0,1)\nplt.grid(&#039;on&#039;)\nplt.title(&quot;Thomae&#039;s function&quot;)\nplt.savefig(&quot;ex435.png&quot;, dpi=100)\nplt.show()\nlim = 0.001\nnum = sum(y &gt; lim for y in ylist)\nprint(f&#039;Found {num} points above y={lim}&#039;)\nlim = 0.01\nnum = sum(y &gt; lim for y in ylist)\nprint(f&#039;Found {num} points above y={lim}&#039;)\nlim = 0.1\nnum = sum(y &gt; lim for y in ylist)\nprint(f&#039;Found {num} points above y={lim}&#039;)\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\nFound 303791 points above y=0.001\nFound 3003 points above y=0.01\nFound 27 points above y=0.1\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import numpy as np import matplotlib.pyplot as plt xlist = &#x5B;] ylist = &#x5B;] for q in range(2,1000): for p in range(1,q): if np.gcd(p,q) == 1: xlist.append(p\/q) ylist.append(1\/q) plt.plot(xlist, ylist, &#039;.r&#039;, ms=3) plt.xlabel(&#039;x&#039;) plt.ylabel(&#039;y&#039;) plt.xlim(0,1) plt.grid(&#039;on&#039;) plt.title(&quot;Thomae&#039;s function&quot;) plt.savefig(&quot;ex435.png&quot;, dpi=100) plt.show() lim = 0.001 num = sum(y &gt; lim for y in ylist) print(f&#039;Found {num} [&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_post_was_ever_published":false,"_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":""},"categories":[69,2],"tags":[126],"class_list":["post-2471","post","type-post","status-publish","format-standard","hentry","category-matplotlib","category-python","tag-thomaes-function"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-DR","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1188,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/204-mandelbrot-fractal-using-python-2-2-2-2\/","url_meta":{"origin":2471,"position":0},"title":"#208 Scatter plot with histograms using python","author":"gantovnik","date":"2021-11-27","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt Fixing random state for reproducibility np.random.seed(19680801) some random data x = np.random.randn(1000) y = np.random.randn(1000) def scatter_hist(x, y, ax, ax_histx, ax_histy): # no labels ax_histx.tick_params(axis=\"x\", labelbottom=False) ax_histy.tick_params(axis=\"y\", labelleft=False) # the scatter plot: ax. Scatter(x, y,s=3) # now determine nice limits\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\/ex208.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex208.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex208.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1718,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/12\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2\/","url_meta":{"origin":2471,"position":1},"title":"#325 Finding the intersection points between two functions using python","author":"gantovnik","date":"2022-12-13","format":false,"excerpt":"[code language=\"python\"] 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) - func2(x), x0) # Defining functions that will intersect funky = lambda x : np.cos(x \/ 5) * np.sin(x \/\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\/2022\/12\/ex325.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1742,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":2471,"position":2},"title":"#330 Gradient method using python","author":"gantovnik","date":"2023-01-03","format":false,"excerpt":"golden.py [code language=\"python\"] import math as mt def golden(f,a,b,tol=1.0e-10): # Golden section method for determining x # that minimizes the scalar function f(x). # The minimum must be bracketed in (a,b). c1 = (mt.sqrt(5.0)-1.0)\/2.0 c2 = 1.0 - c1 nIt = int(mt.ceil(mt.log(tol\/abs(a-b))\/mt.log(c1))) # first step x1 = c1*a + c2*b\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\/ex330.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":845,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/01\/149-optimization-in-python\/","url_meta":{"origin":2471,"position":3},"title":"#149 Optimization in python","author":"gantovnik","date":"2021-01-24","format":false,"excerpt":"#149 Optimization in python [code language=\"python\"] import os import numpy as np from scipy.optimize import minimize os.chdir(r'D:\\projects\\wordpress\\ex149') os.getcwd() def obj(x): return (1-x[0])**2 + 100*(x[1]-x[0]**2)**2 def con(x): g=np.zeros(2) g[0]=1-x[0]**2-x[1]**2 g[1]=5-x[0]-3*x[1] return g x0=[5.0,5.0] constraints = {'type': 'ineq','fun': con} options={'disp':True} res = minimize(obj,x0,constraints=constraints,options=options) print('x =',res.x) print('f =',res.fun) print(res.success) [\/code] Output: [code language=\"python\"]\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2634,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/436-non-convex-univariate-function-optimization-using-brents-method-in-python\/","url_meta":{"origin":2471,"position":4},"title":"#436 Non-convex univariate function optimization using Brent&#8217;s method in python","author":"gantovnik","date":"2024-07-18","format":false,"excerpt":"Brent\u2019s method is an optimization algorithm that combines a bisecting algorithm (Dekker\u2019s method) and inverse quadratic interpolation. It can be used for constrained and unconstrained univariate function optimization. The Brent-Dekker method is an extension of the bisection method. It is a root-finding algorithm that combines elements of the secant method\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\/2024\/07\/ex436.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex436.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex436.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3022,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/442-the-partial-sums-of-the-fourier-series-for-the-square-wave-function-using-python\/","url_meta":{"origin":2471,"position":5},"title":"#442 The partial sums of the Fourier series for the square-wave function using python","author":"gantovnik","date":"2024-07-25","format":false,"excerpt":"import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider nmax = 5 pi = np.pi x = np.linspace(-2*pi, 2*pi, 1001) def f(xarray): y = np.zeros_like(xarray) for ind, x in enumerate(xarray): xmod = x%(2*pi) if xmod<pi: y[ind] = 1 if x%pi==0: y[ind]= np.nan return y def Fourier(x, nmax):\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\/07\/ex442_3.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_3.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_3.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2471","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=2471"}],"version-history":[{"count":3,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2471\/revisions"}],"predecessor-version":[{"id":2476,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2471\/revisions\/2476"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=2471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=2471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=2471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}