<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kazi Abu Rousan on Medium]]></title>
        <description><![CDATA[Stories by Kazi Abu Rousan on Medium]]></description>
        <link>https://medium.com/@kaziaburousan?source=rss-f2f201eed185------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZkQ-KWGd77RC_aYbpj7fIQ.jpeg</url>
            <title>Stories by Kazi Abu Rousan on Medium</title>
            <link>https://medium.com/@kaziaburousan?source=rss-f2f201eed185------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 06:34:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kaziaburousan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Time Dilation as Coordinate Transformation : Geometry of Time Dilation]]></title>
            <link>https://medium.com/swlh/time-dilation-as-coordinate-transformation-geometry-of-time-dilation-78577ad4dedb?source=rss-f2f201eed185------2</link>
            <guid isPermaLink="false">https://medium.com/p/78577ad4dedb</guid>
            <category><![CDATA[linear-algebra]]></category>
            <category><![CDATA[theory-of-relativity]]></category>
            <category><![CDATA[physics]]></category>
            <category><![CDATA[visualization]]></category>
            <dc:creator><![CDATA[Kazi Abu Rousan]]></dc:creator>
            <pubDate>Tue, 12 Sep 2023 19:34:20 GMT</pubDate>
            <atom:updated>2023-09-12T19:34:20.915Z</atom:updated>
            <content:encoded><![CDATA[<h3>Time Dilation as Coordinate Transformation : Geometry of Time Dilation</h3><p><strong>Special Relativity</strong> was originally proposed by <strong>Albert Einstein</strong> in a paper published on 26 September 1905 titled “<a href="https://www.fourmilab.ch/etexts/einstein/specrel/www/">On the Electrodynamics of Moving Bodies</a>”.<br>All started because of the incompatibility of <strong>Newtonian Mechanics</strong> with <strong>Maxwell’s Equations of EM theory</strong>. Einstein’s developed special relativity, which corrects mechanics to handle situations involving all motions and especially those at a speed close to that of light.</p><p>The history of the development of the theory is very interesting but today we are not going to discuss that. Today we are here to understand one of the key feature of <strong>STR</strong>, which is called <strong>Time Dilation</strong>. But our way of understanding this will be a bit different and I will only recommend this blog if you have,</p><ul><li>A very basic knowledge of Linear Algebra.</li><li>A previous exposure to <strong>Special Theory of Relativity (STR)</strong>.</li></ul><p>So, let’s start our discussion.</p><blockquote><em>A Theory which has so much might…<br>It creates ideas so bright…<br>But still it’s results block our sight…<br>Because we can’t see what imagination and what reality! right?<br>–&gt; By Me</em></blockquote><h3>A Brief Introduction to Space-Time Diagrams</h3><p><strong>A spacetime diagram is a graphical illustration of the properties of space and time in the special theory of relativity.</strong><br><em>Spacetime diagrams allow a qualitative understanding of the corresponding phenomena like time dilation and length contraction without mathematical equations.</em> In space-time diagram,</p><ul><li>The y-axis represent ct,i.e., the vertical axis correspond to <strong>time</strong>.</li><li>The x-axis represent distance along the x axis, as we normally measure distance.</li></ul><p>Keeping these points in our mind, we can draw the <strong>space-time</strong> diagram of some object.<br>Let’s suppose, you are in your room sitting in a chair at <em>rest</em>.<br>Your position is on changing. You are only going forward in time. So, you will be represented by ct-axis or a line parallel to ct-axis depending on your position. As shown in the figure below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*92sO-jvxN-Vdzvxd4Js-Dw.jpeg" /><figcaption>The lines which represent the motion (position over time) are called <strong>World Line</strong> of the object which we are studying. Hence, <em>Red</em> and <em>Blue</em> lines represent your world line.</figcaption></figure><p>Let’s see two example.</p><h4>Example-1:</h4><p><strong>What is the world line of Light?</strong><br>As, we know the speed of light is constant, which is written as <em>c</em>. Hence, It’s equation is,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*N8FpMd1P4fArUFLr99rHfw.png" /></figure><p>The axis are represented by <em>ct</em> and <em>x</em>. Hence, Light’s world line will be a line which makes 45 degree angle with the <em>x</em> and <em>ct</em> axis and which passes through the origin.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*ZGFl3_VrPmEyoFUe8n8d7g.jpeg" /></figure><h4>Example-2:</h4><p>Suppose, we have <strong>flash</strong> running around with a velocity 0.5<em>c</em>, i.e., half of the speed of light. What is the <strong>world line</strong> of flash?<br>The equation relating flash’s path and time is,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*8ZCAyxUbgw7pp6vINRS3Tg.png" /></figure><p>The axis are represented by <em>ct</em> and <em>x</em>. Hence, Flash’s world line is a line with slope of 0.5, which passes through origin.<br>Note: <em>Here slope must be measured with respect to ct</em>−<em> axis.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*9E4-fbl6zjgW8ti-27ZxEg.jpeg" /></figure><p>There is a very important point which we must remember.</p><ul><li><strong>Speed of light is the highest possible speed</strong>, as a result <strong>any world line must exist above </strong><em>x</em>=<em>ct</em><strong> line.</strong></li></ul><p>To know more about this <strong>world line</strong> and <strong>Space-Time Diagrams</strong> watch this amazing:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F1rLWVZVWfdY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D1rLWVZVWfdY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F1rLWVZVWfdY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/61349444d74eb61fb7fa374c01b2315d/href">https://medium.com/media/61349444d74eb61fb7fa374c01b2315d/href</a></iframe><h3>Idea of Basis and Space-Time Separation Vectors</h3><p>In space-time diagram we have two axis in the plane(<em>Minkowski</em>). As a result (I am leaving the details, we will discuss those in some other blog), we can take two <strong>basis vectors</strong> along each of those axis.<br>We will represent the basis vectors by <em>e_{x}</em>​​ and <em>e_{ct}</em>​​. Keeping these in our mind let’s understand <strong>Event Vector</strong>/<strong>Space-Time Separation Vectors</strong>.</p><p>Let’s suppose, You are a lazy-ass person and you are simply sitting in your room. One of your friend was in that room too, But suddenly he climed on a space ship and started to move with a velocty comparable to the speed of light.</p><p>Let’s say after moving some distance, he emits a signal. This is represented by the blue point in the diagram below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*i301gC69l7KJaB0Gq0i04Q.jpeg" /></figure><p>This event of <strong>emitting a signal</strong> can be represented by <strong>a point</strong> on the plane. Hence, It can also be written as a vector using the <strong>basis vectors</strong>.</p><p>Any event which happens, can be represented by a vector called <strong>Space-Time Separation Vector</strong> and can be represented by,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*n1HQtlakYDF1izMMlgX2qQ.png" /></figure><p>Here, the event1 is the beginning of the motion of your friend and event2 is the instant when he emits the signal.</p><p>Let’s see another example to make it more clear.</p><p>Suppose, <strong>Einstein</strong> and <strong>Feynman</strong> both were in a room. When <strong>Einstein</strong> sneezed, <strong>Feynman</strong> being the <em>finest man</em> just started to run far from him.<br>Einstein noticed that after 3sec of his sneeze, Feynman is 4m away from him and at that very moment Feynman snapped.</p><p>So, <strong>Space-Time Separation Vector</strong> for the event1(sneeze) and event2(snap) is,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*HMdxph-wgmcZDfXo5AkSbg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/829/1*Nw0DFjx5PeA1H04DMjNcIw.png" /></figure><h3>A remark on Lorentz Transformation and It’s effect on Basis</h3><p>I think this blog is already too long and also to discuss <strong>Lorentz Transformation</strong> here in it’s full glory, It will take huge amount of time. So, I am just going to mention a point here. If you had a course on <strong>STR</strong> before, then it should make sense. But if it’s not clear, then also don’t worry. I will discuss this in some later blog.</p><p>Suppose, We have two frames,</p><ul><li>One at rest.</li><li>Another moving with a constant velocity <em>v</em>.</li></ul><p>If we want to go from the rest frame to the moving frame, we use <strong>Lorentz Transformation</strong>. The transformation can be written in terms of matrix,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*txgUApdlO5JxgCteATJ7Rg.png" /></figure><p>Now, when we go from rest to the moving frame <strong>the basis vectors transform</strong>. They transform in a way such that <strong>they make same angle with the world-line of light</strong> and also both the basis vectors <strong>tilt inward</strong>. It will be clear if someone knows how to visualize linear transformations by looking into the matrix.<br>I have made one animation which will help you guys understand.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*UHgnh70N9glQV2TA" /></figure><p>Hope It is clear.</p><p>Another Important point which we should always remember is that, when we go to any moving frame, in that frame, the <em>ct</em> axis lies along it’s <strong>world line</strong>.</p><p>Keeping all of these in our mind, we are ready to understand the main topic.</p><h4>Time Dilation due to Transformation of Basis</h4><p>Let’s start with the example of <strong>Einstein</strong> and <strong>Feynman</strong>. In the <strong>Einstein’s Frame</strong>, he sees that <strong>Feynman</strong> snaps after 3<em>s</em> of his sneeze.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*7RAQI1FQOMFcZt9AIhQFoQ.jpeg" /></figure><p>This can be very easily shown by drawing the projection of the <strong>Space Time Separation Vector</strong>. Also, it is clear from equation-4.</p><p>But now what about <strong>Feynman’s Frame</strong>?,i.e., what is the <strong>time component</strong> of the <strong>red arrow</strong> in feynman’s frame.</p><p>To see what happens in his frame, we apply a <strong>Lorentz Transformation</strong>. The transformation property tells us that <strong>Feynman’s basis will be a bit tilted</strong> as shown.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*U5S2x7qvbQaBtpCWUDW7DQ.jpeg" /></figure><p>Here, the <strong>pink</strong> basis vector is the <strong>time basis</strong> in Feynman&#39;s frame and <strong>blue</strong> basis is the <strong>space basis</strong>. I have shifted the <strong>time component of the event vector to Feynman&#39;s location</strong>.<br>Now, our job is to simply find the component of the time (3<em>s</em> in Einstein&#39;s frame) along the <strong>pink</strong> basis.</p><p>Because, the vector is not along the time axis, our intuition is already telling us that <strong>Einstein</strong> will see that in <strong>Feynman’s</strong> moving frame duration has increased. For detail show the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*enuT3rm1qfHgnPdVW7H1-w.jpeg" /></figure><p>Here, the left one is the diagram which <strong>Einstein</strong> will see in his own frame and right one is the diagram which <strong>Einstein</strong> will see for <strong>Feynman’s</strong> frame.</p><p>This is how we can intuitively understand <strong>Time Dilation</strong> using coordinate transformation.</p><p>I tried my best to explain this, although I am not sure how much you guys have understood but I hope, at least you have found of insight on this topic.</p><h4>A question for you the Reader</h4><p>Try explaining the Time Dilation using this method for the case of Mueon particle.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78577ad4dedb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/time-dilation-as-coordinate-transformation-geometry-of-time-dilation-78577ad4dedb">Time Dilation as Coordinate Transformation : Geometry of Time Dilation</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A programmer’s way to check Irrational or Rational numbers]]></title>
            <link>https://www.cantorsparadise.com/a-programmers-way-to-check-irrational-or-rational-numbers-9c800ef251fe?source=rss-f2f201eed185------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c800ef251fe</guid>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[book-writing]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[irrational-number]]></category>
            <category><![CDATA[number-theory]]></category>
            <dc:creator><![CDATA[Kazi Abu Rousan]]></dc:creator>
            <pubDate>Mon, 08 Aug 2022 19:18:03 GMT</pubDate>
            <atom:updated>2023-07-26T15:24:58.940Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Programming is one of the most difficult branches of applied mathematics; the poorer mathematicians had better remain pure mathematicians. —<a href="https://en.wikipedia.org/wiki/Edsger_W._Dijkstra"> <strong>Edsger Wybe Dijkstra</strong></a></blockquote><p>This quote actually trigger me a bit as I myself love pure maths but also as a physics student I can’t totally disagree. Today we will see something which is actually in some sense supporting this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*ZxRSQ-32Me8ekZ00eIhxgw.png" /><figcaption>Pie chart for the irrational number</figcaption></figure><h3>Introduction</h3><p>We all have heard about Rational and Irrational Numbers. Most of us have learnt about those in early days if our school but still let’s just quickly see what they are:</p><p><strong>Rational Numbers</strong>: A number of form p/q, where p,q are both integers with q not equals to 0, is called a Rational number. Some examples can be 2/5, 7/6, etc. Notice if q=1, then we get <strong>Integers</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/100/1*fByQj5viD0o-XZZ38y32cw.png" /><figcaption>Pie chart for a rational number</figcaption></figure><p><strong>Irrational Numbers: </strong>Numbers<strong> </strong>which cannot be written as p/q are called Irrational Numbers. Some examples can be sqrt(2) or 25^(1/65), etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*eoDJuUhcDCIh4rs9WwJD8w.png" /><figcaption>sqrt(3) visualisation using graph</figcaption></figure><p>Now, we are getting close to the real topic. <strong>How can you find if a given number is rational or irrational?,</strong> well there are many ways to find that out. As an example see this example, which most of you have seen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/743/1*mYC9go54vf91-XDl19s_fg.png" /><figcaption>Irrationality of sqrt(2) (Taken from my book)</figcaption></figure><p>Nice!!… But can we write a programme which will tell us if a number is irrational or rational?, Well I have tried to find this all over web but have not find anything. Now, I am a python user as most of my readers know.</p><p>So, We will write a function in python which is tell us <strong>if any given number is irrational or not</strong> (<em>Note: It will only tell if it is a irrational or not, try to write a code which will also include rational numbers</em>).</p><p>Before writing the code, we must know something which is called “Integral Root Theorem”, which is a special case of “Rational Root Theorem”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*bu-o9R8vvJ7XgOEFmheoQw.png" /><figcaption>Integral Root Theorem (IRT)</figcaption></figure><p>Let’s see an example on how you can find the solution of a polynomial using this method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*Pcq5gLZOUyalZHkO2iiztQ.png" /></figure><p>You may be thinking, wtf! why I have to just <strong><em>play the guessing game</em></strong>. It is <strong><em>not efficient</em></strong> at all. But think carefully, you can take equations of power 7 or 8 and using this you will have at-least few numbers among which your solution exist. It is really helpful of you don’t have the solution formula or any computer beside you like in the old times.</p><p>Now, let’s see how you can proof that sqrt(2) is irrational using <strong>IRT</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*T5MX2kNymrwA2JE3rEQqtg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*pGUfRjIcHjwP9xCjeu2ZqQ.png" /><figcaption>Application of IRT</figcaption></figure><p>It’s really easy. But how to find the polynomial?, well for that you have to know something called “<strong>Minimal Polynomial</strong>”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*KrG3g0BT1jzef8ZMwS3TKQ.png" /><figcaption>Definition of Minimal Polynomial</figcaption></figure><p>One thing should be noted that all the things we have discussed so far are only applicable to <strong><em>Algebraic Numbers</em></strong>.</p><p>Let’s see an example of minimal polynomial,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*J1XC8mQGhoOVWT8nznygzw.png" /><figcaption>Minimal Polynomial example</figcaption></figure><p>But why do we need this?, the <strong>IRT</strong> works for any polynomial. Then why we need this new concept?, well as you know minimal polynomial is the smallest one and in case of programming, we always want the code to be small and efficient.</p><p>Now let’s write the code for finding if a number is irrational or not. I will be using <strong>sympy library of python</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*ztLNS3XgvOzvFMdCGxvbIg.png" /></figure><p>As you can see, I am using <strong>minimal_polynomial</strong>, <strong>Poly</strong>, <strong>root</strong> and <strong>divisors</strong> functions. Here root(n, m) returns n^(1/m).</p><p>In this case, <em>you have to use this particular functions to calculate the roots otherwise the programme will not work</em>. This due to the algorithm that this particular function use. <strong>P = minimal_polynomial(n)</strong>, returns the minimal polynomial of number n. This is shown for both n = sqrt(2) and n = sqrt(2 + \sqrt(3)).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*I05h79XtqGGq7Sekn4PWMw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*88qgerm5f093rcX6CWrAqA.png" /></figure><p>I hope up to this point everything is clear. So, Finally we have the function as :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dfa6742505f58a82d3207e4672d1fc19/href">https://medium.com/media/dfa6742505f58a82d3207e4672d1fc19/href</a></iframe><p>This will give the output as,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*TLBp2HcicIGHaxsrfjtNqA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*dwYskOZKVfKv0N6Ngetpdg.png" /></figure><p>What will happen if I write pi there ?, well It will show as an error saying “<strong>NotAlgebraic: pi doesn’t seem to be an algebraic element</strong>”. So, It can even check <strong><em>transcendental numbers</em></strong>!!</p><h3>Ending</h3><p>So, now you have a programme for checking irrationality. Why not try to write a programme to differentiate between Rational and Irrational Numbers?, try it and share your solutions.</p><p>I hope you enjoyed learning something new.</p><h3>Copyrights</h3><p>All the programs, images and gif used here are all created by me using different <em>python library </em>and<em> mathematica </em>. The images which are used are taken from my upcoming book on <strong>Number Theory using Python</strong>. You can read the free draft if you like.</p><p><a href="https://www.researchgate.net/profile/Kazi-Abu-Rousan/project/Number-Theory-with-Python/attachment/62bde55c0c295f1f9ac93822/AS:1172730670002177@1656612188470/download/mybook_gcd+updated.pdf?context=ProjectUpdatesLog">https://www.researchgate.net/profile/Kazi-Abu-Rousan/project/Number-Theory-with-Python/attachment/62bde55c0c295f1f9ac93822/AS:1172730670002177@1656612188470/download/mybook_gcd+updated.pdf?context=ProjectUpdatesLog</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c800ef251fe" width="1" height="1" alt=""><hr><p><a href="https://www.cantorsparadise.com/a-programmers-way-to-check-irrational-or-rational-numbers-9c800ef251fe">A programmer’s way to check Irrational or Rational numbers</a> was originally published in <a href="https://www.cantorsparadise.com/">Cantor’s Paradise</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Invisibility-Harry’s Cloak using python(OpenCv)]]></title>
            <link>https://medium.com/swlh/invisibility-harrys-cloak-using-python-opencv-80dedf9d46c4?source=rss-f2f201eed185------2</link>
            <guid isPermaLink="false">https://medium.com/p/80dedf9d46c4</guid>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[programmin]]></category>
            <category><![CDATA[opencv-python]]></category>
            <category><![CDATA[github]]></category>
            <dc:creator><![CDATA[Kazi Abu Rousan]]></dc:creator>
            <pubDate>Fri, 24 Jul 2020 15:01:22 GMT</pubDate>
            <atom:updated>2020-07-25T07:02:43.223Z</atom:updated>
            <content:encoded><![CDATA[<p>Few days ago I was making an animation for teaching complex analysis. In that animation I was using <a href="https://opencv.org/"><strong><em>OpenCv</em></strong></a><strong><em> </em></strong>to transform any image or live video according to <strong><em>Complex Analytic functions</em></strong> or <strong><em>Matrices</em></strong>. To do so, I first created an array of pixels and then move them around. Then the pixels with are lost inside a certain ranges were replaced using a <strong><em>mask.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bQ_IhpwQNm6QDEy4qQHL1w.jpeg" /><figcaption>Inversion is used on my beautiful face using OpenCv</figcaption></figure><blockquote>This “<strong>Mask</strong>” make me think, <strong>Can we do something like mask a portion of live video and then replace it with a previously captured frame? Something like what Mr.India did or Something Harry Potter did?</strong></blockquote><p>It was obvious, there are many people who have already done it and as I am a lazy bum, Here I will just explain those <a href="https://www.geeksforgeeks.org/invisible-cloak-using-opencv-python-project/"><strong><em>codes already available</em></strong></a> and will make them a little small and compact.</p><h3>The power to invisibility</h3><p>With all this talk, Let’s begin out main goal.</p><h4>Requirements:</h4><p>You will need some python libraries for this.</p><ol><li><a href="https://numpy.org/install/"><strong><em>numpy</em></strong></a>(This is the saviour of physics and maths programmers).</li><li><strong><em>time</em></strong>(We don’t actually need this one. It is just used to give our machine a little rest).</li><li><a href="https://pypi.org/project/opencv-python/#:~:text=%20Installation%20and%20Usage%20%201%20If%20you,run%20pip%20install%20opencv-contrib-python%20if%20you...%20More%20"><strong><em>OpenCv</em></strong></a>(Obviously, from the title you should have expected this 🙄)</li></ol><p>The method of installing those libraries are linked, just click on them. Apart from those libraries , the most important things are a <strong><em>Homosapien</em></strong>, a <strong><em>Laptop</em></strong>, <strong><em>Internet</em></strong>(so you can upload the result).</p><h4>Writing and understanding the code:</h4><p>You can use any text editor like <strong><em>Sublime text</em></strong> or something like <strong><em>Visual Studio</em></strong>, <strong><em>Jupiter Notebook</em></strong>, <strong><em>Pycharm</em></strong> etc..</p><p>First we include all our necessary libraries</p><pre>import numpy as np<br>import cv2<br>import time</pre><p>Now after this, we begin the main programming by starting to record frames.</p><pre>cap = cv2.VideoCapture(0)<br>fourcc = cv2.VideoWriter_fourcc(*&#39;XVID&#39;)<br>output = cv2.VideoWriter(&#39;Invisible_man.avi&#39;, fourcc, 30, (640, 480))</pre><p>In this block what we have done is:</p><ol><li>First created a capture using the webcam. Live stream is captured using this function. There, the <strong><em>0(zero — UltraMan) </em></strong>represent our primary webcam. If you have additional ones, just replace the zero with one to use those.</li><li>Then to save your craziness, we will use <strong><em>VideoWriter</em></strong>. There the XVID codec is used.</li><li>To specify your wished video-format and name , again we have used the <strong><em>VideoWriter</em></strong>(this is a function of cv2). The first one is the name of the output video file. The second one takes the codec(previously defined). Then comes the frame-rate. And finally our width and height(I mean video’s. Here is a catch, due to our webcam and OpenCv’s limitation higher width and height value will not work). The output will be saved at the same location as the program file.</li></ol><pre>time.sleep(3)<br>still_frame = 0<br>for i in range(60):<br>    ret, still_frame = cap.read()<br>    still_frame = np.flip(still_frame, axis=1)</pre><p>Now, Here comes our only use of time library.</p><ol><li>time.sleep(3) let our webcam wait a little(3 sec), before working. This is done for many reason . One of them is that, after opening the webcam doesn’t work well. It needs some time to adjust with the light.</li><li>After this , we define a variable named <strong><em>still_frame </em></strong>and assign it a value.</li><li>Now we load the image of all things before camera(just our background).This will be used as our reference image. cap.read() does this job for us. It returns a <strong><em>boolean True or False value depending upon whether our camera is one or off</em></strong>. This also returns an image which is saved as the variable still_frame.</li><li>Finally the last line flip the image along <strong><em>y-axis, </em></strong>as previously saved still frame was reversed due to camera’s recording.</li><li><strong><em>step-3</em></strong> and <strong><em>step-4</em></strong> are written inside the<strong><em> for loop</em></strong> which is running <strong>6<em>0 times, </em></strong>because we want to capture the background i.e., our surrounding as clearly as possible. You can just load the still_frame a single time(but i have the word single!!), but it will make our result a little worse. So, I will suggest run it for at <strong>least 50 times</strong>.</li></ol><h4>Idea behind doing these steps</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*eHs7NVTWMW8CSEzXhDXgHw.png" /></figure><p>Suppose your room is like this. So, Until now , what we have done is scan this image and stored it inside the variable <strong><em>still_frame</em></strong>.</p><p>This is our background i.e., a still frame(remember we have taken the image 80 times to make it better).</p><p>So, After running the program (few seconds later),I come into the picture. That means our current frame is changed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/794/1*DPI9jxhctEHiZdzmGVC8Sg.png" /></figure><p>I come inside the frame wearing a blue T-Shirt. This blue shirt is our <strong>Invisibility Cloak</strong>. So, <strong><em>To create the illusion of being invisible</em></strong>, what we can do is, just <strong><em>remove the blue pixels from the new image</em></strong>(current saved frame)<strong><em> </em></strong>and<strong><em> replace those frames with the frame of the image saved as variable still_frame. </em></strong>For this purpose first I have created a variable for <strong><em>storing our background data</em></strong>. I hope all the previous steps are cleared.</p><pre>while True:<br>    ret, img = cap.read()<br>    if ret == True:<br>        img = np.flip(img, axis=1)<br>        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)</pre><p>Now, Let’s see what we have done in this block.</p><ol><li>Here the line while True: keeps the command inside it run continuously. It is done as we want to be live(Live capturing|onak dabi).</li><li>Now as I have discussed above, we need a current frame, were you will be standing with a sheet of specific colour(I will use<strong><em> Blue colour</em></strong>). So, to capture live (get a frame each instance) we use cap.read(). Remember <strong>cap </strong>is already defined before.</li><li>In the next step, what we do is first see if the camera is one. It is checked by the if ret==True: command. As the <strong><em>ret </em></strong>is only<strong><em> </em></strong>true if the camera is on. So, all<strong><em> under if command </em></strong>will only work if the new frame is captured.</li><li>In the last 2 lines i have just flipped it as before and converted the image in <strong><em>HSV</em></strong>(<strong>Hue- Saturation-Value</strong>) colour format.</li></ol><h4>Why HSV ?</h4><p>You may ask why HSV ?, Why not RGB ?. The answer is simple. Machines are not that great in understanding RGB. I mean It needs more complex programs to do that. And also there all things related to <strong><em>visibility depends upon all three of them</em></strong> and because of it, This RGB is very sensitive and also hard to separate different low or high brightness colour.</p><p>But in <strong>HSV only Hue </strong>(just a single parameter) <strong>defines colour. </strong>For this reason it is used as single variables are mostly easy to use.</p><p>In <strong><em>HSV </em></strong>colouring method<strong>, hue ranges from 0 to 360. </strong>But OpenCv’s limitation is 2⁸(from 0 to 255). For this reason, the ranges of colour in OpenCv and original colouring system are a little different.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*JM6sE-3JOKjoF4pv4mlwfQ.jpeg" /><figcaption>HSV colour wheel</figcaption></figure><p>For our purpose, I will choose a Blue. So, <strong>to find the value of hue of blue</strong> what we will do is first notice <strong><em>where blue colour is in the original wheel</em></strong>. It is in the range <strong>180 to 240. </strong>So, in <strong><em>opencv</em></strong> the range of blue is <strong>almost 90 to 120(</strong>i will take it as 130 for more better result<strong>). If you want to use red, then It is in the range 0 to 10 and also 170 to 180. </strong>Notice It is the case, as it is on both side of zero. So, We have 2 different ranges.</p><p>The <strong>Saturation</strong> in <strong>HSV </strong>in<strong> ranges from 0 to 100 . </strong>Saturation describes amount of <strong>gray</strong> in a particular colour. Here maximum of saturation is <strong>255. </strong>When It is<strong> 0 , </strong>It means gray. For low values of saturation, many colours(like red, pink) can’t be separated by computer, so we will use a range of 5<strong>0 to 255 </strong>for our program. Similarly <strong>Value</strong>(represent <strong>Brightness</strong>) in <strong>HSV </strong>will be taken in the range of 5<strong>0 to 255</strong>.</p><pre>lower_blue = np.array([90, 50, 50])<br>upper_blue = np.array([130, 255,255])<br>mask1 = cv2.inRange(hsv, lower_blue, upper_blue)</pre><p>In this block of code, first two line represent <strong>blue colour </strong>using 2 array. 1st value is <strong>HUE</strong>, 2nd one is<strong> SATURATION</strong> and last one is <strong>VALUE</strong>.</p><p>cv2.inRange is used to create a mask. This takes hsv image as argument(i.e., already saved current image) and then it finds all parts with blue colour(of given range) in the frame current frame and then create separate out the blue sheet covered area(it will treat any blue colour equally). Here only one range is used. But when <strong>you are using Red colour sheet, at that moment we have to define another mask </strong>(<em>as red is inside 2 different ranges</em>) and then we have to add them using OR logic i.e., mask1 = mask1 + mask2,where, mask2 is another mask for other range.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*4l8fq6p2Ury4dS-AxCxdyA.png" /></figure><p>As you can see, if we apply this to our new frame, It will be like this. See, Our machine is only selecting the blue shirt(ignore those noise i.e., those white dots).</p><p>All other colours are removed. Now we just have to remove the selected pixels(this white ones) from our current image and replace this frame’s pixel with our background frame’s pixels.</p><p>Before this, we have to <strong>apply Morphological Transformation</strong> on <strong>mask1. This is done to remove noises i.e.,those white dots. </strong>You can even run the program without it but the result will be shitty.</p><pre>mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8),iterations=2)<br>mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, np.ones((3, 3), np.uint8),iterations=2)<br>mask2 = cv2.bitwise_not(mask1)</pre><pre>result1 = cv2.bitwise_and(img, img, mask=mask2)<br>result2 = cv2.bitwise_and(still_frame, still_frame, mask=mask1)<br>finalOutput = cv2.addWeighted(result1, 1, result2, 1, 0)</pre><ol><li>In this block, the first 2 line removes noises using a <strong>matrix transformation</strong>. So, We will increase the smoothness.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/783/1*By7URfq0jwfvyKJ633KowA.png" /></figure><p>2.Now in the next line, we are using the bitwise_not operation. The main purpose of this is to create a mask of all the remaining things, which are not selected by mask1. As you can see in the image, here all other things are selected and only my T-shirt is not selected.</p><p>3. Now we just use the bitwise_and to take the region common to our new frame and mask2 i.e., <strong>all region except our blue cover(my T-shirt) </strong>and then we take <strong>the region common to both mask1 and our still_frame i.e., the pixels of the places where my T-shirt is (but the image of the background) .</strong></p><p>4. Finally we add those up by using addWeighted .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zwk5Epj-zoFPccOjZJU4YQ.png" /></figure><p>And this does the trick. Here is my output. Due to low quality light and webcam, the output is not too great. You can change the blue colour according to your used colour. <strong>Like in this range</strong><strong> lower_blue = np.array([100, 60, 60]) upper_blue = np.array([140, 255,255]).</strong></p><p>The whole code is given below with a output video of one of my friend who have used it. Like her if you use correct background and light , then you can even beat Harry-Potter’s original stunt.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57f55bcb2314eccd0a15959d10874554/href">https://medium.com/media/57f55bcb2314eccd0a15959d10874554/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*Luj4Yxwg4wGIBzM9u7xr-A.gif" /><figcaption>Done by <a href="https://www.instagram.com/pixie_dust_1330/">Syeda Spandita Zaman</a></figcaption></figure><p>Here is a perfectly done example.</p><h3>Ending</h3><p>As you can see,how beautifully we can use, how simple concepts and ideas to create these wonderful things.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rbZ4kJFpqrZBJbg9xVdMVg.jpeg" /></figure><p>OpenCv is very powerful tool to create many creative things. You can use it in many ways. Here i have only shown some elementary things.</p><p>I hope you enjoyed learning something new.</p><h3>Copyrights</h3><p>For the main code<em>, </em><strong><em>I have taken the idea from </em></strong><a href="https://www.geeksforgeeks.org/invisible-cloak-using-opencv-python-project/"><strong><em>GreeksforGreeks</em></strong></a><strong><em> website, linked here and also in the beginning of the post.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80dedf9d46c4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/invisibility-harrys-cloak-using-python-opencv-80dedf9d46c4">Invisibility-Harry’s Cloak using python(OpenCv)</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Trapezoidal Rule: A Method of Numerical Integration]]></title>
            <link>https://www.cantorsparadise.com/trapezoidal-rule-a-method-of-numerical-integration-5772838657b3?source=rss-f2f201eed185------2</link>
            <guid isPermaLink="false">https://medium.com/p/5772838657b3</guid>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[integration]]></category>
            <category><![CDATA[numerical-analysis]]></category>
            <category><![CDATA[science]]></category>
            <category><![CDATA[math]]></category>
            <dc:creator><![CDATA[Kazi Abu Rousan]]></dc:creator>
            <pubDate>Thu, 02 Jul 2020 08:25:00 GMT</pubDate>
            <atom:updated>2020-07-02T12:15:16.092Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>The knowledge of which geometry aims is the knowledge of the eternal. — Plato</blockquote><p>In physics, most of the time we need to apply <em>integration</em>. But there are some integrations which can’t be solved using normally known methods. For them we have some special tricks such as Feynman’s Integral Trick, the Fourier Transform, Laplace’s Transform, the Residue Theorem and so on.</p><p>But there are times when even those can’t help<em>. </em>Even if we leave those integrations, in modern world when we deal with integrations using our computers, we see that machines can’t solve integrations analytically. To overcome both of those problems we use some methods called Numerical Integration. The heart of these method is our geometry<em> . </em>As the legends like <em>Plato</em>, <em>Archimedes</em>, <em>Gauss</em> and others said, the universe talks with us in the language of geometry, in this case there’s no exception.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2jcjgSAw_qoimpVm_2JNrg.png" /></figure><p>Using a simple<strong><em> </em></strong><em>trapezoid</em> we can solve integrations (best part is we can even let computers use this method and with some restrictions, we can also some some of the usually unsolved integrations) numerically with a desired level of accuracy.</p><h3>Why Trapezoid for Integration?</h3><p>We know that if we plot a given function <em>its integration is equal to the area closed by the function’s curve with the independent variable axis.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*w2moPvEeRa1fr8uLMTpmkQ.gif" /><figcaption>Fig-1: Our function f(x) plotted in the coordinate plane with horizontal axis as x axis</figcaption></figure><p>Suppose we have a function <strong><em>f(x)</em></strong><em> </em>and <strong><em>x</em></strong><em> </em>is the<em> independent variable. </em>Let’s plot our function. After plotting f(x) vs x, if we take two points like <strong><em>x = a</em></strong> and <strong><em>x = b</em></strong> . Then, <em>what is the value of the integration between in the limit of a and b</em> ?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*v8ZKaoURuF9dk8iOwEkoeA.gif" /><figcaption>Fig-2: First pointer is at x= a and 2nd one is x=b. The pointer takes on all values of x as it goes.</figcaption></figure><p>As you can see, I have taken a two random point on x axis(x=a and x = b), corresponding to those x values we will get two y-values ,which are<strong><em> y = f(a)</em></strong> and <strong><em>y = f(b)</em></strong>. We will get the y values for all x values between the linear region. Now what we can do is draw <strong><em>rectangles</em></strong> using <strong><em>consecutive points</em></strong> on x axis and points on the graph (As we do to <a href="https://drive.google.com/file/d/13wM5oli8gy1O8Hb9uIciPoYBYKG4R3wG/view?usp=drivesdk"><em>define the integrations itself</em></a>).</p><p>Usually we use <em>rectangular boxes</em> <em>to approximate area under the curve</em> (making width of each rectangle = <strong><em>dx</em></strong> as small as possible to get result as accurate as possible) which works fine . But in this case we’ve to use many terms(rectangles) which is not that efficient. .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fbLcLw0PTOAvhb13d4Y3Gg.png" /><figcaption>Fig-3: Left side represent area approximation using rectangle and Right side shows the area approximation using trapezoid (here a = 0)</figcaption></figure><p>As you can see, I’ve used same number of rectangles and trapezoids in both cases with same width . Now it’s clear that trapezoids approximates the area better than rectangles which is the main idea behind using<em> Trapezoids </em>instead of <em>Rectangles.</em></p><h3>Calculating Area</h3><p>Suppose we have a function <strong><em>f(x)</em></strong> and we are interested in the integration in the linear range of x = a and x = b. Then the value of integration is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xpx433IVVEBFGy2J427cNQ.png" /><figcaption>See figure 2</figcaption></figure><p>We have divide the x axis <strong><em>uniformly into small lengths ,</em></strong>each having a width of<strong><em> h = (b-a)/n</em></strong> . Here <strong><em>n</em></strong>(no. of trapezoid) is a huge number. The <strong><em>larger n will give us better result</em></strong>.</p><p>As the length is equally divided into n equal parts , hence we can write any point on x axis as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G8171bii__Srz-xlo1zOoQ.png" /><figcaption>Remember we have created n equal lengths that means we have n + 1 points. As an example see your hands. You have 5 fingers(If you have 6 then error🤣) but It contains only 4 gaps.</figcaption></figure><p>Corresponding to each of those x values , If we draw <em>lines parallel</em> to <em>y axis</em> , then the <em>points at which they intersect the curve are the corresponding function values</em> and hence <em>the length of each dotted lines</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ACjoVV3fBgoV3Cev4NlSUg.png" /><figcaption>Hence each<strong> purple dot</strong> on the <strong>red graph</strong> represent <strong>(x_r,f(x_r)) </strong>and here <strong>x_0 = a</strong> is used.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ehVZ9BN6sS1LKG1G7znsdA.png" /><figcaption>Fig-4: Here h = 2 unit. As you can see when we move along the arrows from 1st x to 2nd x, we add a h.</figcaption></figure><p>You may ask why <strong>x_r</strong> has such a value ? You can see in the figure-4, As we move from <strong><em>x_0=a</em></strong> to the <strong><em>end </em></strong><em>of the</em><strong><em> first trapezoid</em></strong>, we add it’s <em>width</em> to our <em>x value</em>. Hence, <em>each time we </em><strong><em>cross</em></strong><em> a trapezoid , we have to add a </em><strong><em>h</em></strong><em> with the </em><strong><em>a</em></strong>. So, to reach the <strong><em>r-th</em></strong> point on x axis(<strong>r-th x value</strong>) we have to add <strong>h</strong> , <strong><em>r-times </em></strong>with our<strong><em> x_0</em></strong>.</p><p>As you can see each shape is a trapezoid. Hence <em>we just need to find the area of each trapezoid</em> and <em>adding them will give us total area under the curve</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*EK2majk1Ge7-C6we9UGPGw.gif" /><figcaption>Fig-5: <strong>r-th</strong> trapezoid of the area approximation, which parallel sides of length f(x_r) and f(x_(r+1))</figcaption></figure><p>Now you see we take two point <strong><em>x_r</em></strong> and <strong><em>x_(r+1)</em></strong>. For those two points we have two lines parallel two y axis, whose lengths are <strong><em>f(x_r)</em></strong><em> </em>and <strong><em>f(x_(r+1))</em></strong> . You can see clearly it is indeed a trapezoid with a height of <strong>h(x_(r+1)-x_r) </strong>and <strong><em>the length of 2 parallel sides are the length of the lines which are parallel to y axis</em></strong>. Using just normal geometric area formula of trapezoid, we get,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y-dVwstbZ1uYPRbg9uK5Jw.png" /><figcaption>Here 1st term represents sum of two parallel sides and h represents distance between 2 parallel sides.</figcaption></figure><p>So , for a single trapezoid the area is as shown. Now for the <em>total are</em>a we just sum over all. The <strong><em>index(r)</em></strong> will run from <strong><em>r = 0</em></strong> to<strong> <em>r = (n-1)</em></strong>, where <strong><em>n</em></strong> is the <em>no. of trapezoid</em> or <em>no. of division between </em><strong><em>x= a</em></strong><em> and </em><strong><em>x = b</em></strong>. Hence the <strong>total area</strong> of the region or the value of integration is (with a few error):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cHjyQ2hM1VWCsBjmtfk67Q.png" /></figure><p>Using the summation notation, we obtain:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6qSL8g6np8msiW2v4oS5xg.png" /><figcaption>In the 1st line, we have summed up to <strong>n-1</strong>, because at r = n-1, f(a+(r+1)h)=f(b), which should be. For the 2nd line, as the middle terms (f(a+h),f(a+2h),..etc exist in 2 consecutive trapezoids) occur twice, we multiply them with 2 and add each term once.</figcaption></figure><p>The upper formula works fine but for better approximation, we have to make n bigger, which makes h much smaller, giving us a <em>result as perfect as we desire</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*eXjZQAETZqEMXrwzBj3lgg.gif" /><figcaption>Fig-6: Decreasing <strong>h</strong> value and it’s corresponding trapezoids</figcaption></figure><p>As you can see in the video above, as we <strong><em>decrease</em></strong> the value of <strong>h</strong>, i.e., <strong><em>increase</em></strong> the value of <strong>n</strong>, the area approximation(<em>the value of integration</em>) goes to it’s <strong><em>true value </em></strong>(i.e., <strong><em>reducing the value of</em></strong> <strong><em>error</em></strong>). Now, let’s see how you can use a computer to solve any integration numerically (we will use python):</p><h3>Integration using Trapezoidal in Python</h3><p><strong>Algorithm</strong></p><ol><li>Define <strong>f(x)</strong>, the function which we want to integrate.</li><li>Get the values of <strong>a</strong> , <strong>b</strong>, tolerance(The error value, i.e., |real value- calculated value|).</li><li>Assign the values of <strong>fa</strong> = f(a), <strong>fb</strong> = f(b), <strong>n</strong> .</li><li>Use a loop to perform summation.</li><li>Return or print the value.</li></ol><h4>Code</h4><p>I have written it as a function so that we can use it at any time(it just uses <strong><em>numpy</em></strong>, although the program will work fine without it, just remove the pi value in the 2nd line with it’s true value):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/248f867e51a8df665a262df13aa0c107/href">https://medium.com/media/248f867e51a8df665a262df13aa0c107/href</a></iframe><p>This is a simple program. You can make it a little more user friendly by supplying the value of n, when calling the function.</p><p>Here is another(it just use <strong>numpy</strong> and <strong>matplotlib</strong>):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83ee919e90ccb8d30e94e994b41ee32f/href">https://medium.com/media/83ee919e90ccb8d30e94e994b41ee32f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*9iPk9BzEZaE4ynvTpshUXw.png" /><figcaption>A plot using Matplotlib to show how introducing more trapezoids help in better approximation.</figcaption></figure><p>This code not only gives you the freedom to give any value of n but also shows how trapezoids cover the area under the curve. But as you can see i have removed the tolerance as it’s meaning-less when you are trying to learn how it converges. Although you can add it yourself like I did in the previous one.</p><p>You can see that there is some error in both cases(i have included the error in output). The amount of error in this method is proportional to the product of the second order derivative of our function<strong> f(x)</strong> and <strong>h²</strong>.</p><p>If we include the error term, then the final expression will be,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZtVxHU-R8smWDQb-hLWUHg.png" /><figcaption>This can be proven using<strong> intermediate value theorem</strong></figcaption></figure><p>You can see clearly from the above expression that as we decrease h (i.e., increase n), the error term decreases.</p><h3>Ending</h3><p>As you can see, how beautifully we can use <em>simple elementary geometry</em> of trapezoids to <em>approximate</em> and <em>compute integrations</em> numerically. This is indeed powerful and much better than the <em>classical rectangular approximation of area</em>. Try It yourself, Solve some integration using this Trapezoidal Rule.</p><p>I hope you enjoyed learning something new.</p><h3>Copyrights</h3><p>All the programs, images and gif used here are all created by me using different <em>python library </em>and<em> </em>all equation are written using Tex. The pdf which is linked is taken from the book “<strong><em>What is Mathematics?</em></strong>” by <em>Richard Courant</em> and <em>Herbert Robbins</em> .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5772838657b3" width="1" height="1" alt=""><hr><p><a href="https://www.cantorsparadise.com/trapezoidal-rule-a-method-of-numerical-integration-5772838657b3">Trapezoidal Rule: A Method of Numerical Integration</a> was originally published in <a href="https://www.cantorsparadise.com/">Cantor’s Paradise</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is Fourier Series? and It’s beauty]]></title>
            <link>https://medium.com/swlh/what-is-fourier-series-and-its-beauty-ce2410012329?source=rss-f2f201eed185------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce2410012329</guid>
            <category><![CDATA[differential-equations]]></category>
            <category><![CDATA[physics]]></category>
            <category><![CDATA[fourier-series]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[mathematics]]></category>
            <dc:creator><![CDATA[Kazi Abu Rousan]]></dc:creator>
            <pubDate>Sun, 07 Jun 2020 16:01:30 GMT</pubDate>
            <atom:updated>2020-06-10T20:32:05.684Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Fourier Series? and It’s Beauty</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YkraGeZW9la2P7sDetBpFQ.png" /><figcaption><a href="https://youtu.be/0y8UsIFcvPs">Joaquin Phoenix Joker using 6000 epicycles(For full video click )</a></figcaption></figure><blockquote>Regarding the research of d’Alembert and Euler could one not add that if they knew this expansion, they made but a very imperfect use of it. They were both persuaded that an arbitrary and discontinuous function could never be resolved in series of this kind, and it does not even seem that anyone had developed a constant in cosines of multiple arcs, that first problem which I had to solve in the theory of heat. —J. Fourier</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*1UhjvvdjKCAcfCBCKY87Wg.gif" /><figcaption><strong>“What is Fourier Series?” using 3000 epicycles</strong></figcaption></figure><h4>The Begining</h4><p>It all starts when <strong>Joseph Fourier </strong>started to analyze the <strong>Heat Equation </strong>which looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VCcwAQmPJqH7u3fdK8uSyg.png" /><figcaption>Here T represent Temperature at any position x at time t</figcaption></figure><p>This equation is what we call <strong><em>A Linear Partial Differential Equation , </em></strong>what it means is ,</p><p>If we have 2 solutions(Φ1 and Φ2) which are different and satisfies a Boundary condition then we can write:</p><p>A new solution=Y = (a ×Φ1 )+ (b ×Φ2)</p><p>where <strong>a</strong> and <strong>b</strong> are 2 <strong>arbitrary constants</strong> which depends upon the Initial Conditions. But how it is so powerful . As we know the solution will give us some combination of <strong><em>sine and cosine function</em></strong>. So what?</p><p>To understand what I mean , let’s take a simple example. We will consider an <strong>imaginary perfect metal rod, </strong>which have 2 different temperatures on two different sides with respect to it’s midpoint. Let’s see what I mean graphically:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p82HJsiIeNkudnbMVLjyeA.png" /><figcaption>The rod has 2 different temperatures on 2 different side w.r.t it’s midpoint.</figcaption></figure><p>So, If we mark temperature on <strong>Y-axis </strong>and position on <strong>X-axis, </strong>then as you can see, we get what we call a <strong>Step Function.</strong></p><p>But wait a minute, I have told you the <strong>solution</strong> we get from the <strong>Heat Equation</strong> are <a href="https://en.wikipedia.org/wiki/Linear_combination"><strong>Linear combination</strong></a> of cosine or sine function.</p><p>But our assumed system of Rod should also be s<strong>olvable with the same method</strong> and should give us A Linear combination of Sine and Cosine(i.e., we will get <strong><em>Step function using Periodic sine and cosine function?? w.t.f</em></strong>)</p><p>This is where <strong><em>Fourier </em></strong>make a move , what we call <strong><em>a pro gamer move </em></strong>and become immortal in the world of Science.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*IFLcadZMQW6UVrHfKuqSmA.gif" /><figcaption>Step function using Combination of sine and cosine</figcaption></figure><p>As you can see in the video, As we keep adding the cosine or sine or their combination (depends on boundary condition) with <strong>appropriate coefficients</strong>(in our discussion similar to a and b),we are getting something close to our desired function. But our function is discontinuous and even that can’t stop us!!, Now you understand how powerful it is? You may say, well It looks like our function but it is not exactly that, I will say yes you are right. What we have is just an approximation but as we add more and more solutions of the <strong><em>Differential Equation shown above , </em></strong>We will get more and more close to our function. Here I have given you a <a href="https://editor.p5js.org/K.A.Rousan/embed/kAYmowr6J"><strong><em>playing thing</em></strong></a>, Try increasing number of terms and see how you approaches to that function(More general square wave).</p><p>Now we understand the idea behind this beautiful method. So, Now let’s see how to calculate one.</p><h3>Fourier Series of a Function:</h3><p><strong>If a function f(x) of some real variable x, defined and integrable in the interval (c,c+2L), satisfies <em>Dirichlet’s Conditions</em> i.e.,</strong></p><ol><li>f(x) is single valued and finite,</li><li>f(x) is <strong>piecewise continuous </strong>having at most <em>finite number of finite discontinuities</em>,</li><li>f(x) has at most finite number of maxima and minima within the interval,</li></ol><p>then f(x) can be expanded in this interval into the following series, called <strong><em>Fourier Series</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Eoh548f8Wa8LEbaYoCUlA.png" /></figure><p>Notice, I have not written that f(x) is periodic as we can write f(x) as a series even if the function is not periodic.</p><p>If we consider the <strong><em>function of time with a period of T </em></strong>i.e., in the interval <em>(c,c+T)</em>, Then we can write</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fH0RkpnoTfyJVTRqGOY0bQ.png" /></figure><p>Where ω=2π/T and T = 2L.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*g-MdAnOAn2bVhDvjsLQUXQ.gif" /><figcaption>Triangular Wave formation using just pure sine waves</figcaption></figure><p><strong>The GIF where the Square wave(Step Function for a particular case) or Triangular wave is formed as an example of this series. As you can see we are just adding pure waves with certain frequency and amplitude. They are just waves and not have any circles in it.</strong></p><p>In these 2 equations a0, an and bn are called Fourier Constants and are given by</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4rZuDoiOc9V6CxzU8SXi1w.png" /></figure><p>The value of (a0/2) is found by putting n = 0 , so I have not written it.</p><p>If you want the proof, just use the <a href="https://tutorial.math.lamar.edu/Classes/DE/PeriodicOrthogonal.aspx"><strong>orthogonality of sine and cosine</strong></a> .</p><p>So, We have seen that we can expand any function(which satisfies certain conditions) into a series of sine and cosine. But <strong>where are the circles which you see in the Joker or What is Fourier series gif? </strong>To understand this we have to go back to the equation</p><p><strong><em>exp(iθ) = cos(θ)+i sin(θ) ; </em></strong>Where <strong><em>i</em></strong> is the square root of -1</p><p>This equation tells us that <strong><em>exp(iθ)</em></strong> represent a circle as <strong><em>cos(θ) </em></strong>and<strong><em> sin(θ) </em></strong>also does the same. <strong>Using this simple but powerful identity we can write the f(x) in terms of exponential functions and in 2D plane they are represented by circles.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*Efi0dMDVuctDZepZEbKGlg.gif" /></figure><p>Like you see in this video, We have <strong>drawn a perpendicular from it’s tip to the Imaginary axis.</strong> The curve created because of it is our good old <strong>sine curve</strong>.</p><p>Similarly we can get a cosine curve if we draw a perpendicular from the tip to real axis.</p><p>So, using the upper identity we can write f(x) as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HP1vXfsN2Q4z81Hc2RxJIw.png" /></figure><p>Now you see from where those circles come from.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*0S7seo2sak4UvjEUUXnkbA.gif" /><figcaption>Adding each exponent term of the <strong>complex Fourier expansion</strong> for <strong>square wave</strong></figcaption></figure><p>In this series <strong><em>each exponent term actually represent a rotating circle</em></strong> with a <strong>certain frequency</strong>(given by the term inside bracket) and a <strong>certain radius</strong>(given by cn). Those circles join together(<strong><em>one after another to their radius’s end like shown in the GIF file</em></strong>) to give you the curve or function as you have seen before.</p><p>Here <strong>cn</strong> is equivalent to <strong>(an-<em>i</em>bn)/2 </strong>and <strong>c-n </strong>is<strong> </strong>equivalent to <strong>(an+<em>i</em>bn)/2. </strong>Those constants are given by,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6eVsPOdY1ZC0g_823siuPg.png" /></figure><p>The problems concerning the possibility of expressing a function by Fourier Series are cumbersome. Fortunately almost all functions associated with physical problems are covered by the Dirichlet’s Conditions. <strong><em>Fourier’s Theorem </em></strong>is a set of conditions <em>sufficient to imply the convergence of The Fourier Series to some function closely related to the given function</em>.</p><h4>Convergence of The Series:</h4><p><strong><em>If f(x) and f`(x) are sectionally continuous in -L to L and f(x) satisfies Dirichlet conditions then according to Fourier Theorem we can say:</em></strong></p><p><strong><em>The Fourier series of f(x) converges to (f(x+)+f(x-))/2</em></strong></p><p>If<strong> f(x) is discontinuous at a point x0</strong>, then the Fourier series of f(x) converges to <strong><em>(f(x0+</em></strong>ɛ<strong><em>)+f(x0-</em></strong>ɛ<strong><em>))/2 as </em>ɛ→0.</strong></p><h4>Odd-Even Function Trick:</h4><p>When we expand any function we have an equation with a <strong>Left hand side</strong> and <strong>Right hand side</strong> . As both sides must be equal, <strong><em>If one side has even function , then another one must be even</em></strong> and <strong><em>if one is odd then other must be odd also</em></strong>. This simple idea is very far reaching and it reduces the complexity of calculation.</p><p>As we know <strong><em>cosine is an even function</em></strong> so if<strong> f(x)=f(-x)</strong>, then <strong>Expansion of f(x) only contains terms with cosines </strong>and if <strong>f(x)=-f(-x)</strong>, then<strong> Expansion of f(x) only contains terms with Sine </strong>as<strong> <em>sine is a odd function</em>.</strong></p><h4><strong>Calculation:</strong></h4><p>Let’s calculate the fourier series of the square wave as an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NlwR8BFvXEw4FC7wSwR-6w.png" /></figure><p>As you can see the Square wave is defined as shown here.</p><p>As you can notice <strong><em>f(t)=-f(-t), </em></strong>this means we must have odd terms only when we expand this function. This means we will only have the <strong>sine terms i.e., the terms whose coefficient are bn. </strong>This insight reduced our half work and we will now only consider sine terms and not cosines.</p><p>To solve this problem as i have told before , we just need <strong><em>bn </em></strong>due to the symmetry as shown in the figure. To understand it’s intuition see the p5.js sketch hyperlinked with the text <strong><em>“Playing Thing”</em></strong> before the discussion <strong><em>Fourier Series of a Function.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RX9oBoerTJ8COvtF6EFmjg.png" /><figcaption>This is the Square wave where T = 3</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*jTwKK7umXudyNTrxzv2FFg.png" /><figcaption>This is the solution.</figcaption></figure><p>As you can see here I have used the <strong>series of sine and cosine expansion</strong>(with an=0), but you can also expand the Square wave using <strong>exponent terms</strong> , <strong>which is signified by the fact that you can create that using Rotating Phasors(vectors) as shown before</strong>.</p><h3>End:</h3><p>The Fourier Series is a way of representing certain functions(mostly all periodic functions). From classical physics to Quantum Mechanics, even in data processing, It is used . It is one of the most powerful tool ever created by Maths.</p><p>We have discussed so much but even now <strong><em>there are many topics left.</em></strong> This post is already long so <strong><em>we will discuss the remaining things later</em></strong>. I hope this article will help you in building intuition and will help you in solving many basic problems. With this let’s end our discussion here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*RzAmd-A8XqvWUtBN0nH23Q.gif" /></figure><p>If you want to make any drawing using <strong>epicycles</strong>, here is my<a href="https://github.com/Rousan99/Fourier_Epycicle"> <strong>github link</strong> </a>(click upon the github word and you just need python nothing more). Just download that <strong>repository </strong>and use it for creating beautiful animations. But remember the best result will come when the used images will be <strong>single-line drawing(As an example see this white background gif)</strong>.</p><p>OR you can visit here, I have written this <a href="https://editor.p5js.org/K.A.Rousan/embed/pjZLu-5m6"><strong>S<em>ketch</em></strong></a> try it out to draw circular Fourier Series(As an example see the screenshot)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gumBTrTiEpgn6pWf8QYg0A.jpeg" /><figcaption>Upper one is the Drawing and Lower one is the Series Breakdown to circle</figcaption></figure><h3>Copyright stuff:</h3><p>All the images and gif used here are all created using a <strong><em>python library</em></strong> called <strong><em>Manim , developed by Grant Sanderson </em></strong>and <strong><em>p5.js</em></strong> developed by <strong><em>Lauren McCarthy</em></strong> . All equations and calculations used are written by me using Latex.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce2410012329" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/what-is-fourier-series-and-its-beauty-ce2410012329">What is Fourier Series? and It’s beauty</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>