Skip to content

Instantly share code, notes, and snippets.

@steven-tey
Last active September 13, 2019 21:51
Show Gist options
  • Save steven-tey/b397c83c6997fe24f8b5a2769e5dad72 to your computer and use it in GitHub Desktop.
Save steven-tey/b397c83c6997fe24f8b5a2769e5dad72 to your computer and use it in GitHub Desktop.
CS146 Assignment 1 Code
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 139,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.49999999999999983\n"
]
}
],
"source": [
"import scipy.integrate as integrate\n",
"from scipy.integrate import quad\n",
"import math as m\n",
"a = 1\n",
"b = 0\n",
"result = integrate.quad(lambda x: (((1/m.sqrt(2*m.pi*a))*m.e**(-((x-b)**2)/(2*a**2)))), 0, m.inf)[0]\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.50059\n"
]
}
],
"source": [
"import random\n",
"import math as m\n",
"import numpy as np\n",
"\n",
"def f(x): #defining the f(x) function\n",
" return (x**3+2*x+1)\n",
"\n",
"l = [] #creating a list to store the values of random numbers from a normal distribution\n",
"\n",
"for j in range (100000): #creating 100,000 random numbers from a normal distribution\n",
" l.append(np.random.normal(0,1))\n",
" \n",
"counter = 0 #creating a counter to count how many times the value of f(x) is larger than 1\n",
"\n",
"for i in range (len(l)): #for loop to check through the values list l, when put into f(x), if the output is >1\n",
" if f(l[i]) > 1:\n",
" counter+=1\n",
" else:\n",
" continue\n",
"\n",
"print(counter/100000)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment