Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [numerical-methods]

Algorithms which solve mathematical problems by means of numerical approximation (as opposed to symbolic computation).

30
votes
8answers
8k views

PI Calculator, Interview Challenge

Greg Beech says here that he asks C# candidates to produce a formula that calculates PI Given that Pi can be estimated using the function \$4 * (1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \dots)\$. ...
25
votes
6answers
24k views

Monte Carlo pi calculation

In order to learn the basics of Monte Carlo I calculated pi with it. I also wrote an explanation of the reasoning behind the code. Down here you can see the circle with random points that I simulated ...
19
votes
9answers
4k views

Approximating constant π² to within error

This function takes as input float error and approximates constant π² to within error by computing this sum, term by term, until the difference between the new and the previous sum is less than error. ...
14
votes
2answers
3k views

Pi by Monte-Carlo

I was inspired by this SO post to investigate a good Java8 way to calculate Pi based on simulation. I used a similar task to learn about parallel programming on both CUDA, and Intel Xeon Phi ...
13
votes
2answers
674 views

Simulating a two-body collision problem to find digits of Pi

I came across a nice video on 3Blue1Brown's channel that highlights a very indirect way to find the digits in Pi. I'd suggest watching the whole video, but briefly: The setup is as above. A "small" ...
12
votes
3answers
2k views

Definite Integral Approximation using the Trapezoidal Method

I wrote a program to calculate the value of Definite Integral of a function from a to b. It used the trapezoidal approximation ...
12
votes
2answers
2k views

Calculate Pi using Monte Carlo

...
12
votes
1answer
180 views

Calculate the closest point to many hyperbolic paraboloids

In this question I asked for a way to compute the closest projected point to a hyperbolic paraboloid using python. Using the iterative approximation answer, I'm able to use the code below to ...
11
votes
6answers
4k views

Approximating the square root using an iterative method

I wrote this code, based on the Newton-Raphson method, to find the square root of a number. I'm wondering how I can optimise this code, as I am out of ideas. ...
11
votes
2answers
4k views

Sqrt (square root) function

As an exercise in learning Scala, I implemented a square root function like this: ...
10
votes
3answers
113 views

Reinventing the Math Functions

Just for practice in the mathematical side of programming, I decided to rewrite the math functions, with the addition of the root() function, which the Math library ...
10
votes
1answer
876 views

Approximating Pi, Monte Carlo integration

I wrote some code that uses Monte Carlo Integration to Approximate pi in Java and Akka. The tl;dr explanation is you can imagine throwing darts at a square with a circle inscribed inside of it. You ...
9
votes
5answers
911 views

Probabilistic matchmaking simulation

The following is a problem I found on this wiki. What can I do to optimize my algorithm, and make this code more C++11? Write a program to discover the answer to this puzzle:"Let's say men and ...
9
votes
1answer
472 views

Gamma function in Rust

The gamma function is one of a couple nice continuous extensions to the traditional factorial function. I used this Python program as a reference, which in turn, uses this Ada program. As the Ada ...
9
votes
1answer
786 views

Generate iCalendar .ics files with events for astrological aspects

I'm relatively new to Python, coming from a deep C++ background. I'm mostly looking for feedback on how to make my code more idiomatic/pythonic, but I would welcome and appreciate any and all other ...
9
votes
1answer
99 views

Discrete Lanczos Derivatives

I have a PR implementing denoising discrete Lanczos derivatives, following this paper. The following code works well, but the design is a train wreck, and I was hoping to get some advice to improve it....
8
votes
4answers
1k views

Buffon's Needle experiment

I was working on a program that simulates the Buffon's Needle experiment. If anyone is not familiar with it, a Wiki link is provided - Buffons's Needle The point of Buffon's experiment is to find ...
8
votes
3answers
797 views

1D shock tube problem written in Fortran

I have written a simple Euler solver for the 1D shock tube problem. Eventually, I plan to extend this code to solve the full 3D compressible Navier-Stokes equations. Therefore, I want to start with ...
8
votes
3answers
448 views

Multithreaded Monte-Carlo Integration

I have a PR implementing multithreaded naive Monte-Carlo integration here. My requirements for the class are the following: It should support progress reporting, ETA, and graceful cancellation. It ...
8
votes
1answer
738 views

Fitting multiple piecewise functions to data and return functions and derivatives as Fortran code

Background For a future workshop I'll have to fit arbitrary functions (independent variable is height z) to data from multiple sources (output of different ...
8
votes
0answers
141 views

Root finding and integration

I am trying to implement the modified Next Reaction Method with time varying propensities as mentioned in sections IV and V of this paper. At one step in the process this expression must be evaluated: ...
7
votes
3answers
437 views

Implementing numerical integration

I have this C++ code that implements a rectangular numerical integration. It evaluates the \$K\$-dimensional integral $$\int_{u_K = 0}^{\gamma}\int_{u_{K-1} = 0}^{\gamma-u_K}\cdots\int_{u_2}^{\...
7
votes
2answers
557 views

Visualizing Newton-Raphson method for finding zeroes of a function

I have created a program to visualize the working of Newton-Raphson method to find the zeroes of a function: newton.m: ...
7
votes
2answers
323 views

Monte Carlo estimation of the Hypergeometric Function

I am trying to implement the algorithm described in the paper Statistical Test for the Comparison of Samples from Mutational Spectra (Adams & Skopek, 1986) DOI: 10.1016/0022-2836(87)90669-3: $$p =...
7
votes
1answer
191 views

Monte Carlo Pi (MASM)

I'm currently trying to brush up on my assembly skills and, being at the FPU section of the tutorial, I implemented a very basic version of a Monte-Carlo-Algorithm to compute pi. I deliberately use ...
7
votes
1answer
173 views

2d linear Partial Differential Equation Solver using finite differences

This is code that solves partial differential equations on a rectangular domain using partial differences. fd_solve takes an equation, a partially filled in output, ...
7
votes
1answer
3k views

Python class that implements the Newton method

Here is a python function I wrote to implement the Newton method for optimization for the case where you are trying to optimize a function that takes a vector input and gives a scalar output. I use ...
7
votes
1answer
143 views

Computing max t such that tanh(pi*sinh(t)/2) <1

I'm looking for a way to quickly determine that maximum value of t such that tanh(pi/2 sinh(t)) is strictly less than 1, for a ...
6
votes
3answers
5k views

Implementation of Brent's Algorithm to find roots of a polynomial

I made a program that contains a root-finding algorithm for polynomials as a function and contains 3 test polynomials. The algorithm is Brent's method and is based entirely off the pseudocode from ...
6
votes
1answer
2k views

Iterative equation solver in Python

In order to solve a equation where the left hand side appears under an integral on the right hand side: $$ B(p^2) = C\int_0^{p^2}f_1\left(B(q^2),q^2\right)\mathrm{d}q^2 + C\int_{p^2}^{\Lambda^2} f_2\...
6
votes
2answers
488 views

Monte-Carlo method to estimate Pi runs slower in multiple threads, than in single thread

I'm learning Scala, and I wrote a program to estimate the value of Pi. When I'm using multiple threads, it takes 5-6 times longer to calculate the same iterations. I wrote a similar program in Java, ...
6
votes
1answer
65 views

Monte Carlo errors estimation routine

I would value your opinion on the following piece of code. I am rather new to both Python and Monte Carlo analysis, so I was wondering whether the routine makes sense to more experienced and ...
6
votes
1answer
226 views

Approximating π via Monte Carlo simulation

Inspired by a tweet linked to me by a friend and a Haskell implementation by her for the same problem, I decided to try my hand at approximating the value of π using everything in the Haskell standard ...
6
votes
1answer
147 views

C++ Pi Calculator - Leibniz Formula

Yesterday I saw a CodeTrain video in where Daniel Shiffman tried to approximate Pi using the Leibniz series. It was interesting so I decided to try it too. I wrote this console application with a ...
6
votes
1answer
2k views

Monte Carlo simulation to approximate the value of PI

This is a C++ implementation of a simple Monte Carlo simulation to approximate the value of pi. The program uses the standard library Mersenne twister engine to generate two random numbers between -1....
6
votes
2answers
666 views

Implementation of alternating direction implicit method

I have written a program that implements the ADI method and Crank-Nicolson method for solving Schrodinger equations. The program is working, but it takes a very long time to run. I am looking for ...
6
votes
2answers
7k views

Newton's method to solve cubic equations

I have used the Newton-Raphson method to solve Cubic equations of the form $$ax^3+bx^2+cx+d=0$$ by first iteratively finding one solution, and then reducing the polynomial to a quadratic $$a1*x^2+b1*x+...
6
votes
1answer
304 views

Calculating pi to 7 significant figures without using Math.PI

I need to calculate pi to 7 significant figures in Java—without using Math.PI. Here is the code I came up with to do that: ...
6
votes
1answer
195 views

Computing the improper integral of several functions

How can I optimize and make prettier the following code? Main.cpp ...
6
votes
1answer
301 views

Double exponential quadrature

I'm trying to lighten the code review load for the maintainers of boost.math, and I was hoping you guys could help me out. I have a pull request which implements tanh-sinh quadrature, which is ...
6
votes
2answers
327 views

Runge-Kutta 4th order using Python numexpr.evaluate()

I am implementing an ODE solver, where the user provides rates and coefficients as a string. ODE solver has to work with vectors. The best implementation I got so far is the following: ...
6
votes
0answers
80 views

Metropolis Monte Carlo Sampler in Rust

the following is an implementation of the standard Metropolis Hastings Monte Carlo sampler. You can read more about it here. At the end I am going to give you a link to the Rust playground, so you ...
5
votes
3answers
4k views

Finding the root of a function by Bisection Method

The program has to look for a root in an interval [a,b]. The root should be declared with a certain accuracy eps. I.e it should ...
5
votes
2answers
4k views

Compute a numerical derivative

Since I could not get numpy.gradient() to compute a derivative successfully, I wrote a script to compute it manually. Running the script below will output a plot of ...
5
votes
2answers
25k views

Trapezoidal rule to approximate the integral of x^2

I've implemented the trapezoidal rule to compute the integral for a function \$x^2\$. I would like to see another style of the same code. It seems Matlab hates for a matrix to be expanded without ...
5
votes
3answers
790 views

Bisection to find roots in C++

I have written a short C/C++ code finding root by bisection. (This is a simple iterative numerical method allowing to find the root of an equation i.e. x such that f(x) = 0). Bisection Method The ...
5
votes
2answers
383 views

Monte Carlo simulation of amoeba population

I wrote a simple Python simulation to answer the "Amoeba" population question posed here: A population of amoebas starts with 1. After 1 period that amoeba can divide into 1, 2, 3, or 0 (it can die)...
5
votes
2answers
88 views

Calculating Maclaurin series for sin(x)

I'm very new to Haskell as was hoping to get some feedback on my code AND I have some specific questions. I've posted code below or you can see it here. I'd welcome ideas on how better to calculate ...
5
votes
3answers
2k views

Numeric double integration

I've made a simple program for numerically aproximating double integral, which accepts that the bounds of the inner integral are functions: ...
5
votes
1answer
1k views

Evaluating a series of Legendre polynomials

The following function represents the electrostatic potential, in spherical coordinates, due to a ring of charge \$q=1\$ and radius \$R=1\$, placed in the plane \$x\$-\$y\$: $$\phi(r,\theta) = \sum_{...