42 votes
Accepted

Gillespie Stochastic Simulation Algorithm

Yes you can. Below is a fairly general, Mathematica-compiled, fast and robust version. Examples 1. Michaelis-Menten kinetics Michaelis-Menten kinetics for enzyme-directed substrate conversion. The ...
István Zachar's user avatar
39 votes
Accepted

Animating wave motion in water

...
Kuba's user avatar
  • 137k
37 votes
Accepted

Modeling the spread of an infection in networked computers

If it is at all an option to represent the grid as a 2D list instead of a list of infected coordinates, I would model this is a cellular automaton. What you've essentially got is an outer totalistic ...
Martin Ender's user avatar
  • 8,754
35 votes
Accepted

Interstellar Image Effects

This was a fun question to answer, even considering that I know nothing about general relativity. It's all a matter of translating the equations presented in this paper by Oliver James, Eugenie von ...
Jason B.'s user avatar
  • 67.9k
32 votes
Accepted

Simulating molecular dynamics efficiently

Okay, here is a way to compute the forces much faster: We create a CompiledFunction (called getForces). It eats a list of points ...
Henrik Schumacher's user avatar
31 votes
Accepted

Computational Bayesian analysis in Mathematica: Any plans to develop MCMC?

Update: 2/7/2019 I have just released a new version of the package: MathematicaStan v2.0 I just have released a beta version of MathematicaStan, a package to interact with CmdStan. https://github....
Picaud Vincent's user avatar
28 votes

Computational Bayesian analysis in Mathematica: Any plans to develop MCMC?

For the sake of completeness let me advertise someone else's code which implements MCMC in mathematica. Josh Burkart has implemented Mathematica Markov Chain Monte Carlo which is available on github....
chris's user avatar
  • 22.8k
28 votes
Accepted

Bouncy Bubbles animation

This is my port of the Processing code that you referenced. It doesn't try to optimize, so I didn't try it either, for example I didn't use Nearest to find ...
C. E.'s user avatar
  • 70.4k
25 votes

Bouncy Bubbles animation

data generates n balls, here: 10 Note that it might be wise to make the box larger, if ...
Feyre's user avatar
  • 8,577
24 votes
Accepted

Simulation of diffusion in a grid

You can use ListConvolve to simulate a single diffusion time step and build a simulation out of that. I'll show a simple example: Let's say we start with simple ...
Thies Heidecke's user avatar
23 votes
Accepted

Obtaining a 3D animation as a drop in a liquid surface

Thanks to J.M. ...
Kuba's user avatar
  • 137k
21 votes

Gravitation simulation and interaction of N (1000) massive objects

And I was somewhat lying in wait for this kind of question... In I have been working on Barnes-Hut-like code for the so-called tangent-point energies in the last couple of years. This is a family of ...
Henrik Schumacher's user avatar
20 votes
Accepted

Multiple reflections of a laser beam in a triangle

Based on some geometric operations such as reflection and line-line intersection (LLI), I wrote up a small code. Hope this could be a starting point to build a more compact ...
Joo-Haeng Lee's user avatar
19 votes
Accepted

Efficiently Mowing Grass with Mathematica

This is just starting ideas for arbitrary regions. Perhaps you can improve it. Version 1 Define concave region with holes, which can be generalizable to arbitrary complexity: ...
Vitaliy Kaurov's user avatar
18 votes
Accepted

Writing compiled functions as fast as Python's Numba

Based on the experience obtained here: ...
xzczd's user avatar
  • 65.4k
17 votes
Accepted

How to make a Cahn–Hilliard model GIF

I am delighted by this problem mostly because I was not aware of the underlying physical model of phase separation (the Cahn–Hilliard equation)! Anyway, here is an approximation of a somewhat similar ...
MarcoB's user avatar
  • 66.6k
16 votes
Accepted

How best to simulate n-body systems in a functional way?

In version 12.0 you can use NBodySimulation for that: ...
user21's user avatar
  • 39.4k
15 votes
Accepted

Beam deformation due to localized force distribution

Here is an example with a force that pushes down between the stretch defined by 2<=x<=3 && y==1 and a fixation at the point ...
user21's user avatar
  • 39.4k
14 votes

How to make a Cahn–Hilliard model GIF

Taken from the Matlab code here http://www.math.utah.edu/~eyre/computing/matlab-intro/ch.txt with slight modifications: ...
shrx's user avatar
  • 7,807
14 votes

Computational Bayesian analysis in Mathematica: Any plans to develop MCMC?

This answer gives explicitly a (parallel) MCMC implementation in mathematica following closely this Wolfram Demonstrations Project. This basically involves only a few lines: ...
chris's user avatar
  • 22.8k
13 votes

How to simulate the true reflective movement of a particle bouncing around in an ellipse?

@Kuba has provided an excellent solution. Here we follow his idea and use another approach like WhenEvent to get the particle tracing. ...
cvgmt's user avatar
  • 69.2k
13 votes
Accepted

Simulating a "Coin Tossing" Game

Let's say that both players start with 10 dollars. We can represent the game state with a list: start = {10, 10}; Create a function which plays one round of the ...
Simon Woods's user avatar
  • 84.9k
13 votes

Two bouncing balls in 1 dimension, issues with two different methods?

General comments on dealing with impacts Your are dealing with a low number of contact points (two). For low number of contact constraints (typically, <10), event driven methods are known to be ...
anderstood's user avatar
  • 14.3k
13 votes
Accepted

Efficiency in calculation on graphs: Compile?

The following should be 150 times faster. Too many micro changes to comment on all of them. Major speedup came from using Ordering to find ...
Henrik Schumacher's user avatar
13 votes

Animating wave motion in water

This is, as J.M. pointed out, a trochoidal wave. I'm going to provide an implementation based on this. This is slightly different compared to what Kuba did. The advantage is that this parametrization ...
C. E.'s user avatar
  • 70.4k
12 votes

Battery charge simulation not clipping properly as it iterates

Your code shows you are somewhat confused about the indices. Here is some code that is more Mathematica idiomatic and which makes keeping the indices straight much easier. I am running the simulation ...
m_goldberg's user avatar
  • 108k
12 votes

Partial Differential Equation in Parallel

The open source Wolfram Research FEMAddOns package has a domain decomposition solver called DecompositionNDSolveValue. You can install the paclet with evaluating: <...
user21's user avatar
  • 39.4k
12 votes

Modeling the spread of an infection in networked computers

Non CellularAutomaton solution, using @MartinEnder's suggestion of FixedPointList as opposed to ...
martin's user avatar
  • 8,668
12 votes

Multiple reflections of a laser beam in a triangle

Instead of thinking too hard, we can let NDSolve take care of it, using WhenEvent to handle the reflections. First, set up 3 ...
Chris K's user avatar
  • 20.1k
12 votes

Simulating molecular dynamics efficiently

To expand on @HenrikSchumacher's comment, compare: ...
Carl Woll's user avatar
  • 130k

Only top scored, non community-wiki answers of a minimum length are eligible