Commit 997ed0c7 authored by rworreby's avatar rworreby

Add exercise 13

parent 33ad4a08
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz
-----------------------
| 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']
['b']
['c']
['d']
['e']
['f']
['g: ', 'grape']
['h']
['i']
['j']
['k']
['l: ', 'lemon']
['m']
['n']
['o: ', 'olive']
['p']
['q']
['r']
['s']
['t']
['u']
['v']
['w']
['x']
['y']
['z']
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.
pass
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.
pass
def main():
np.random.seed(42)
samples = get_samples()
pi_estimate = calculate_pi(samples)
print('Pi ~= {}'.format(pi_estimate))
create_scatter_plot(samples)
create_contour_plot(samples)
if __name__ == '__main__':
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