Commit 997ed0c7 authored by rworreby's avatar rworreby

Add exercise 13

parent 33ad4a08
| A a B b C c D d E e |
| F f G g H h I i J j |
| K k L l M m N n O o |
| P p Q q R r S s T t |
| U u V v W w X x Y y |
| Z z |
['a: ', 'apple']
['g: ', 'grape']
['l: ', 'lemon']
['o: ', 'olive']
apple begins with a, grape begins with g, lemon begins with l, olive begins with o
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Programming Techniques for Scientific Simulations, HS 2020
# Exercise 13.2
from __future__ import (division, print_function)
import numpy as np
import scipy.stats as st
import scipy.linalg as la
import matplotlib.pyplot as plt
def get_samples(n=500):
return np.random.rand(n, 2) * 2 - 1
def get_r(samples):
return la.norm(samples, axis=1)
def calculate_pi(samples):
num_inside = np.sum(get_r(samples) < 1.)
num_total = len(samples)
return 4 * num_inside / num_total
def create_scatter_plot(samples):
# TODO: Create a scatter plot of the samples.
def create_contour_plot(samples):
density_func = st.gaussian_kde(samples.T, bw_method=0.2)
# TODO: Create a contour plot showing the interpolated density.
def main():
samples = get_samples()
pi_estimate = calculate_pi(samples)
print('Pi ~= {}'.format(pi_estimate))
if __name__ == '__main__':
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment