Skip to content

Instantly share code, notes, and snippets.

View AFAgarap's full-sized avatar

Abien Fred Agarap AFAgarap

View GitHub Profile
@AFAgarap
AFAgarap / vending_machine.py
Created April 7, 2016 04:07
Model for a vending machine
# Model for a vending machine that has the following items
# Item #1 15
# Item #2 25
# Item #3 30
import re
def accept_tokens(tokens):
regex = r'[ab]|[[aa|b][a|c]]|[ac]'
regex_eval = re.findall(regex, tokens)
@AFAgarap
AFAgarap / error_analysis.py
Last active May 16, 2016 16:44
Computation for error analysis
# Numerical Analysis, Summer Semester A.Y. 2015-2016, Adamson University
# Author: A.F. Agarap
# Error Analysis
import os
def main():
true_value = float(eval(input("Enter true value: ")))
approximate_value = float(eval(input("Enter approximate value: ")))
float_range = int(input("Enter number of precision values: "))
@AFAgarap
AFAgarap / newton_method.py
Created May 1, 2016 08:31
Finding the root of a function using Newton's method
import math
print("Newton's method\n")
A = int(input("Enter value of Xn: "))
X = 0; E = 1
while (True):
B = 3**(3 * A + 1) - 7 * 5**(2 * A)
C = math.log(3) * 3**(3 * A + 2) - 14 * math.log(5) * 5**(2 * A)
@AFAgarap
AFAgarap / bisection_method.py
Created May 2, 2016 13:51
Bisection method for finding root of f(x)
def main():
A = 0; B = 12
while True:
C = (A + B) / 2
val_a = evaluate_function(A)
val_b = evaluate_function(B)
val_c = evaluate_function(C)
@AFAgarap
AFAgarap / trapezoidal_method.py
Last active May 15, 2016 13:34
Trapezoidal rule, a technique for approximating a definite integral
# Numerical Analysis
# Trapezoidal Method
# Author: A.F. Agarap
# Function = SUM(i = 1, n) h / 2 (f(x[1]) + f(x[n + 1]) + 2(f(x[2]) + f(x[3]) + ... + f(x[n])))
# f(x) = cos(x**2) * exp(x**3)
import math
import os
@AFAgarap
AFAgarap / simpson_method.py
Created May 16, 2016 12:49
Simpson's method for approximating definite integral
# Numerical Analysis
# Simpson's Method
# Author: A.F. Agarap
# Function = (h / 3) (f(x[0]) + 4f(x[1]) + 2f(x[2]) + 4f(x[3]) + 2f(x[4]) + ... + 4f(x[n-1]) + f(x[n]))
# f(x) = cos(x**2) * exp(x**3)
import definite_integral as di
import os
@AFAgarap
AFAgarap / cos-x.py
Created May 20, 2016 05:58
Maclaurin series for cos(x)
def main():
print("\t\t\t10th degree of Maclaurin series, cos(x)")
x = float(input("Enter value of x (in Radians): "))
answer = 1; i = 1; j = 2
while(i < 10 and j <= 10):
if (i % 2 == 0):
answer += (x ** j) / factorial(j)
elif (i % 2 != 0):
answer -= (x ** j) /factorial(j)
i += 1; j += 2
@AFAgarap
AFAgarap / sin-x.py
Created May 20, 2016 05:59
Maclaurin series for sin(x)
def main():
print("\t\t\t10th degree of Maclaurin series, sin(x)")
x = float(input("Enter value of x (in Radians): "))
answer = x; i = 1; j = 1
while(i < 10):
if(i == 1):
answer -= (x ** (j + 2)) / factorial(j + 2)
elif(i % 2 == 0):
answer += (x ** (j + 2)) / factorial(j + 2)
elif(i % 2 != 0):
@AFAgarap
AFAgarap / e-x.py
Created May 20, 2016 06:00
Maclaurin series for e^x
def main():
print("\t\t\t10th degree of Maclaurin Series, e^x")
x = float(input("Enter value of x: "))
answer = 1; i = 1
while(i < 10):
answer += (x ** i) / factorial(i)
i += 1
print(answer)
def factorial(number):
@AFAgarap
AFAgarap / repeated-one.py
Created June 27, 2016 13:54
Mathematical equation producing repeated one's
n = '0'
for i in range(1, 11):
print('{} * 9 + {} = {}'.format(int(n), i, (int(n) * 9 + i)))
n += str(i)