Questions tagged [numerical-methods]

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

Filter by
Sorted by
Tagged with
3
votes
1answer
37 views

Evaluating π using Monte Carlo methods - Serial vs OMP

I wrote this simple code for evaluating the π using Monte Carlo method. This is the serial version: ...
1
vote
0answers
19 views

Trapezoidal rule for set of data

Here is the question from the book of Mark Newman-Computational Physics Exc 5.1 a) Read in the data and, using the trapezoidal rule, calculate from them the approximate distance traveled by the ...
7
votes
1answer
88 views

Parallel Ramanujan's formula for 1/π calculation

I finished my university project for calculating \$1/\pi\$ and I would love to get some feedback. Before you guys jump into this code please keep in mind newcomer to C++ just decided to use it for ...
7
votes
1answer
2k views

Linear Interpolation C++

I have to write a collection of methods for performing linear, bilinear and trilinear interpolation. I have also to write some tests to show that interpolation is exact for polynomials (which should ...
11
votes
0answers
47 views

Continuous Fourier integrals by Ooura's method

I have a PR implementing Ooura and Mori's method for continuous Fourier integrals. I used it here to compute an oscillatory integral that Mathematica got completely wrong, and then I thought "well ...
6
votes
0answers
54 views

Numerical integration in cython

I have a set of nested functions that I need to call multiple times. I know scipy.quad is pretty fast, but I will need to call the integrator recursively and want ...
7
votes
2answers
139 views

Simple function that simulates survey results based on sample size and probability

What is this: This is a simple function, part of a basic Monte Carlo simulation. It takes sample size and probability as parameters. It returns the simulation result (positive answers) plus the input ...
3
votes
1answer
81 views

Calculating pi via collisions

This code calculates pi via collisions; it asks for a user input of N which determines the mass of the second block. It is fully working, it just takes forever to run when N >= 2. I want to be able ...
3
votes
1answer
43 views

Monte Carlo pi animation

I have created a program that calculates pi using a Monte Carlo method. It also animates the process and displays the value as it is updated to show how it gets closer and closer to the actual value ...
6
votes
1answer
220 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
68 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 ...
7
votes
3answers
6k 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 ...
2
votes
1answer
59 views

Java Pi Calculation using an Averaged-Leibniz formula

While trying to discover a way to calculate the digits of Pi faster with the Leibniz formula for Pi, I noticed that, if I took two consecuent numbers in the series, and calculate their average, I ...
11
votes
2answers
2k views

Calculate Pi using Monte Carlo

...
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)\$. ...
0
votes
1answer
38 views

A simple clusterness measure of data in one dimension using Java

Problem definition Given \$X = (x_1, \dots, x_n)\$ such that \$x_1 \leq x_2 \leq \dots \leq x_n \$. Let \$x_{\min} = \min X = x_1\$, \$x_{\max} = \max X = x_n\$ and \$r = x_{\max} - x_{\min}\$. Also, ...
3
votes
1answer
63 views

Polynomial implementation in Golang

I am writing a numerical analysis library in golang for which I need to implement a polynomial struct. Here is the source code: ...
9
votes
1answer
114 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....
13
votes
2answers
1k 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" ...
5
votes
1answer
165 views

Compute PI in Kotlin on a T-shirt

I have written code in Kotlin with the objective of computing Pi in few enough lines so that it looks good on a t-shirt. Can be cut and paste into http://try.kotlinlang.org under "My Programs" and ...
3
votes
3answers
107 views

A collection of vector functionals

I would like some feedback for a collection of what I call "vector functionals", by which I mean maps \$\ell\colon \mathbb{K}^{n} \to \mathbb{K}\$, where the field \$\mathbb{K} = \mathbb{R}\$ or \$\...
4
votes
1answer
83 views

Complex Newton's Method

I'm trying to build a complex Newton's method. I've submitted a PR here, where you can see the documentation and tests, as well as get a compiling example. I'd appreciate y'alls help reducing the ...
3
votes
0answers
336 views

Complex multiplication and integration with CUDA

I want to perform multiplication on two vectors and integrate it in a vector called acc_y. The acc_y variable will update over ...
6
votes
1answer
213 views

Computing the improper integral of several functions

How can I optimize and make prettier the following code? Main.cpp ...
4
votes
2answers
151 views

Computing the double Integral using MonteCarlo techniques using Julia

I decided to try and learn Julia for doing scientific computing, and I decided to tackle the problem of finding $$ \int_{D_{\frac{1}{4}}} x^4 + y^2 dA $$ where \$ D_{\frac{1}{4}} \$ is the part of ...
4
votes
4answers
757 views

Monte Carlo estimation of π

My C program uses the Monte Carlo method to approximate the mathematical constant π, the ratio of a circle's circumference to its diameter (and, importantly for this code, 4 times the ratio of a ...
4
votes
0answers
63 views

Bisection and Newton's method for finding a root of an equation

In an attempt to learn Rust, I've written up implementations of the bisection method and Newton's method for finding roots of an equation. Both methods come in two variants: the first one searches for ...
7
votes
0answers
91 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 ...
4
votes
0answers
181 views

Verilog implementation of trapezoidal integration method

Any and all comments are welcome in this review. Problem I've been doing a lot with numerical integration methods recently and have mostly been programming in Python. But...speedups! And FPGAs are ...
5
votes
0answers
83 views

Implementing numerical integration in Python

I have this Python code that implements a rectangular numerical integration. It evaluates the (K-1)-dimensional integral for arbitrary integer \$K \geq 1\$ $$\int_{u_K = 0}^{\gamma}\int_{u_{K-1} = 0}^...
7
votes
3answers
646 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}^{\...
5
votes
3answers
850 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 ...
7
votes
1answer
223 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, ...
5
votes
2answers
90 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 ...
2
votes
1answer
3k views

ODE45 solver implementation in Python

I have a project where I need ODE solver without dependencies to libraries like Scipy. I decide to implement ODE45. According to tutorials from internet and from what I remember from classes I ...
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 ...
4
votes
0answers
175 views

Numerical integration with Numba

I'm a bit new to working with Numba, but I got the gist of it. I wonder if there any more advanced tricks to make four nested for loops even faster that what I have ...
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. ...
4
votes
2answers
570 views

Discrete random number generator in Python

Description: I am given a list of possible values, each of which have an associated probability of occurrence. How could I improve the algorithm that randomly generates a value based on the given ...
3
votes
1answer
77 views

A single function for implementing Newton forward and backward polynomial interpolation

On various websites, I've come across a lot of realizations of Newton polynomial interpolation that use separate functions for forward and backward interpolation, respectively. Such solutions seem ...
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 ...
4
votes
1answer
214 views

Simple neural-network simulation in C++ (Round 2)

Intro Yesterday I posted this question. Since then, I've updated my code to incorporate these suggestions. I've also removed the dependence on C++11. Finally, I've made the following changes that ...
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 ...
3
votes
2answers
244 views

SICP - exercise 1.7 better end test for square root approximation

Here is exercise 1.7 from SICP: Exercise 1.7 The good-enough? test used in computing square roots will not be very effective for finding the square roots ...
1
vote
1answer
76 views

Runge-Kutta Fourth Order in C

I've found that the Runge-Kutta (4th order) calculations in some software I wrote are the bottleneck. Is there anything obvious I can do to improve efficiency here? Note that Compiler optimizations ...
4
votes
1answer
265 views

Computing an approximate value of Pi via Monte Carlo method in Java with streams

I have this short program that attempts to compute an approximate value of \$\pi\$: ...
5
votes
3answers
3k 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
2answers
293 views

Python numerical integration

Could the time complexity of this definite integral algorithm be improved? ...
5
votes
0answers
346 views

Quasi-Random Number Generators

I would like to communicate a piece of code which I hope will soon be broadly useful for everyone who programs in C++: A set of quasi-random number generators proposed for addition to boost.random. To ...
3
votes
0answers
1k views

Numerical Differentiation by Finite Differences

Numerical differentiation is known to be ill-conditioned unless using a Chebyshev series, but this requires global information about the function and a priori knowledge of a compact domain on which ...