pages tagged mathematics
https://www.vanrenterghem.biz/blog/tags/mathematics/
blogikiwikiSun, 29 Oct 2023 15:36:05 +0800agent based models digital twinshttps://www.vanrenterghem.biz/blog/posts/agent_based_models_digital_twins/
https://www.vanrenterghem.biz/blog/posts/agent_based_models_digital_twins/
mathematicsmodelingSun, 29 Oct 2023 15:15:34 +08002023-10-29T07:36:05Z<p>A new approach to modeling using categories and software, facilitating the build of advanced models like digital twins, is being developed at the moment.</p>
<p>During the 2023 SIAM Conference on Computational Science and Engineering, a group of researchers <a href="https://meetings.siam.org/sess/dsp_talk.cfm?p=124342">presented</a> their</p>
<blockquote><p>diagrammatic representations that provide an intuitive interface for specifying the relationships between variables in a system of equations, a method for composing systems equations into a multiphysics model using an operad of wiring diagrams, and an algorithm for deriving solvers using directed hypergraphs, yielding a method of generating executable systems from these diagrams using the operators of discrete exterior calculus on a simplicial set. The generated solvers produce numerical solutions consistent with state of the art open source tools.</p></blockquote>
<p>As pointed out, mathematics can rarely be isomorphic to its software implementation, yet here the researchers go a long way in enabling that.</p>
<p>Using Julia language, the applied category theorists working on this concept wrote a software (StockFlow) which allows users to build stock-flow diagrams and do all sorts of things with them - from drawing them over to transforming them into other forms like dynamical systems and system structure diagrams, or to solving the underlying differential equations.</p>
<p>The team have also built software (ModelCollab) that hides all the Julia code again, enabling people that aren't educated mathematicians or computer scientists to apply this way of modeling in their work.</p>
<p>This fascinates me, as having a way to write and audit complex systems like digital twins using free and open-source approaches can be transformative in making them accessible for smaller organisations or developed for non-core departments in bigger organisations that up to now are the only ones with enough money or people to develop them for their key operations.</p>
<p>Read more on <a href="https://johncarlosbaez.wordpress.com/2023/10/25/software-for-compositional-modeling-in-epidemiology/">John Baez's blog</a>.</p>
/blog/posts/agent_based_models_digital_twins/#commentsFibonacci golden spiralhttps://www.vanrenterghem.biz/blog/posts/Fibonacci_golden_spiral/
https://www.vanrenterghem.biz/blog/posts/Fibonacci_golden_spiral/
RmathematicsvisualisationMon, 16 Sep 2019 22:03:03 +08002019-09-17T01:51:51Z<h1>What</h1>
<p>After having read the first part of a Rcpp tutorial which compared native R vs C++ implementations of a Fibonacci sequence generator, I resorted to drawing the so-called Golden Spiral using R.</p>
<h1>Details</h1>
<p>Libraries used in this example are the following</p>
<pre><code>library(ggplot2)
library(plotrix)
</code></pre>
<p>In polar coordinates, this special instance of a logarithmic spiral's functional representation can be simplified to r(t) = e<sup>(0.0635*t)</sup>
For every quarter turn, the corresponding point on the spiral is a factor of phi further from the origin (r is this distance), with phi the golden ratio - the same one obtained from dividing any 2 sufficiently big successive numbers on a Fibonacci sequence, which is how the golden ratio, the golden spiral and Fibonacci sequences are linked concepts!</p>
<pre><code>polar_golden_spiral <- function(theta) exp(0.30635*theta)
</code></pre>
<p>Let's do 2 full circles. First, I create a sequence of angle values theta. Since 2 * PI is the equivalent of a circle in polar coordinates, we need to have distances from origin for values between 0 and 4 * PI.</p>
<pre><code>seq_theta <- seq(0,4*pi,by=0.05)
dist_from_origin <- sapply(seq_theta,polar_golden_spiral)
</code></pre>
<p>Plotting the function using coord<sub>polar</sub> in ggplot2 does not work as intended. Unexpectedly, the x axis keeps extending instead of circling back once a full circle is reached. Turns out coord<sub>polar</sub> might not really be intended to plot elements in polar vector format.</p>
<pre><code>ggplot(data.frame(x = seq_theta, y = dist_from_origin), aes(x,y)) +
geom_point() +
coord_polar(theta="x")
</code></pre>
<p><img src="https://www.vanrenterghem.biz/blog/pics/golden_spiral-coord_polar-fail.png" alt="failed attempt plotting golden spiral" /></p>
<p>To ensure what I was trying to do is possible, I employ a specialised plotfunction instead</p>
<pre><code>plotrix::radial.plot(dist_from_origin, seq_theta,rp.type="s", point.col = "blue")
</code></pre>
<p><img src="https://www.vanrenterghem.biz/blog/pics/golden_spiral-plotrix.png" alt="Plotrix golden spiral" /></p>
<p>With that established and the original objective of the exercise achieved, it still would be nice to be able to accomplish this using ggplot2. To do so, the created sequence above needs to be converted to cartesian coordinates.
The rectangular function equivalent of the golden spiral function r(t) defined above is a(t) = (r(t) cos(t), r(t) sin(t))
It's not too hard to come up with a hack to convert one to the other.</p>
<pre><code>cartesian_golden_spiral <- function(theta) {
a <- polar_golden_spiral(theta)*cos(theta)
b <- polar_golden_spiral(theta)*sin(theta)
c(a,b)
}
</code></pre>
<p>Applying that function to the same series of angles from above and stitching the resulting coordinates in a data frame. Note I'm enclosing the first expression in brackets, which prints it immediately, which is useful when the script is run interactively.</p>
<pre><code>(serie <- sapply(seq_theta,cartesian_golden_spiral))
df <- data.frame(t(serie))
</code></pre>
<h1>Result</h1>
<p>With everything now ready in the right coordinate system, it's now only a matter of setting some options to make the output look acceptable.</p>
<pre><code>ggplot(df, aes(x=X1,y=X2)) +
geom_path(color="blue") +
theme(panel.grid.minor = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank()) +
scale_y_continuous(breaks = seq(-20,20,by=10)) +
scale_x_continuous(breaks = seq(-20,50,by=10)) +
coord_fixed() +
labs(title = "Golden spiral",
subtitle = "Another view on the Fibonacci sequence",
caption = "Maths from https://www.intmath.com/blog/mathematics/golden-spiral-6512\nCode errors mine.",
x = "",
y = "")
</code></pre>
<p><img src="https://www.vanrenterghem.biz/blog/pics/golden_spiral-ggplot-coord-fixed.png" alt="ggplot2 version of Golden Spiral" /></p>
<h1>Note on how this post was written.</h1>
<p>After a long hiatus, I set about using emacs, org-mode and ESS together to create this post. All code is part of an .org file, and gets exported to markdown using the orgmode conversion - C-c C-e m m.</p>
/blog/posts/Fibonacci_golden_spiral/#comments