147 lines
3.8 KiB
Python
147 lines
3.8 KiB
Python
import math
|
|
import turtle
|
|
import scipy.integrate as integrate
|
|
|
|
MIN_VALUE = -2
|
|
MAX_VALUE = 2
|
|
ITERATIONS = 100
|
|
|
|
TURTLE_SCALE = 100
|
|
|
|
#### TURTLE STUFF
|
|
cursor = turtle.Turtle()
|
|
win = turtle.Screen()
|
|
cursor.shape()
|
|
cursor.hideturtle()
|
|
cursor.speed(0)
|
|
cursor.goto(-2, 0)
|
|
|
|
def draw_rectangle(length, width):
|
|
arr = [width, length, width, length]
|
|
i = 0
|
|
|
|
while (i < len(arr)):
|
|
cursor.forward(arr[i])
|
|
cursor.left(90)
|
|
i += 1
|
|
|
|
cursor.penup()
|
|
cursor.forward(width)
|
|
cursor.pendown()
|
|
|
|
def draw_trapezoid(a, b, h):
|
|
# Drawing Trapezoid given Area
|
|
width_of_triangle = b - a
|
|
|
|
c = math.sqrt(math.pow(h, 2) + math.pow(width_of_triangle, 2))
|
|
|
|
angle = math.atan2(h, width_of_triangle) * (180 / math.pi)
|
|
|
|
cursor.forward(h)
|
|
cursor.left(90)
|
|
cursor.forward(b)
|
|
|
|
cursor.left(180 - angle)
|
|
cursor.forward(c)
|
|
cursor.left(angle)
|
|
cursor.forward(a)
|
|
cursor.left(90)
|
|
|
|
cursor.penup()
|
|
cursor.forward(h)
|
|
cursor.pendown()
|
|
###
|
|
|
|
# The function that draws the upper half of the Heart
|
|
def f(x):
|
|
return math.sqrt(1 - ((math.fabs(x) - 1) ** 2))
|
|
|
|
# The function that draws the Lower Half the the Heart
|
|
def g(x):
|
|
return -3 * math.sqrt(1 - math.sqrt(math.fabs(x) / 2 ))
|
|
|
|
def percent_error(exper, accept):
|
|
return (math.fabs(exper - accept) / accept) * 100
|
|
|
|
def approx_integral_square(func, a, b, n):
|
|
# n is how many rectangles you want.
|
|
# area is length * width
|
|
area = 0.0
|
|
width = (b - a) / n
|
|
i = 1
|
|
|
|
while (i <= n):
|
|
height = func(a + (i - 1) * width)
|
|
|
|
# draw_rectangle(height * TURTLE_SCALE, width * TURTLE_SCALE)
|
|
|
|
area += width * height
|
|
i += 1
|
|
|
|
return area
|
|
|
|
def approx_integral_trapezoid(func, a, b, n):
|
|
# n is how many trapezoids you want
|
|
width = (b - a) / n
|
|
res = func(a) + func(b)
|
|
i = 1
|
|
|
|
prev_height = func(a)
|
|
|
|
while (i < n):
|
|
height = func(a + (i * width))
|
|
draw_trapezoid(prev_height * TURTLE_SCALE, height * TURTLE_SCALE, width * TURTLE_SCALE)
|
|
res += 2 * height
|
|
|
|
prev_height = height
|
|
i += 1
|
|
|
|
return (width / 2) * res
|
|
|
|
|
|
|
|
|
|
# Compare Results
|
|
upper_area = integrate.quad(lambda x: f(x), MIN_VALUE, MAX_VALUE)
|
|
lower_area = integrate.quad(lambda x: g(x), MIN_VALUE, MAX_VALUE)
|
|
|
|
# Don't really care for the margin of error at the moment
|
|
# Absolute Value of the area because we don't care about signed area at the moment.math.
|
|
upper_area = math.fabs(upper_area[0])
|
|
lower_area = math.fabs(lower_area[0])
|
|
|
|
|
|
print("Using the Square Method:")
|
|
|
|
upper_area_approx = math.fabs(approx_integral_square(lambda x: f(x), MIN_VALUE, MAX_VALUE, ITERATIONS))
|
|
# cursor.goto(-2, 0)
|
|
lower_area_approx = math.fabs(approx_integral_square(lambda x: g(x), MIN_VALUE, MAX_VALUE, ITERATIONS))
|
|
|
|
print("\nApproximated Top Half Area:", upper_area_approx, "Original:", upper_area)
|
|
print("Percent Error: ", percent_error(upper_area_approx, upper_area), "%", sep="")
|
|
|
|
print("\nApproximated Bottom Half Area:", lower_area_approx, "Original:", lower_area)
|
|
print("Percent Error: ", percent_error(lower_area_approx, lower_area), "%\n", sep="")
|
|
|
|
print("------")
|
|
print("\nUsing the Trapezoid Method:")
|
|
|
|
upper_area_approx = math.fabs(approx_integral_trapezoid(lambda x: f(x), MIN_VALUE, MAX_VALUE, ITERATIONS))
|
|
cursor.goto(-2, 0)
|
|
lower_area_approx = math.fabs(approx_integral_trapezoid(lambda x: g(x), MIN_VALUE, MAX_VALUE, ITERATIONS))
|
|
|
|
|
|
print("\nApproximated Top Half Area:", upper_area_approx, "Original:", upper_area)
|
|
print("Percent Error: ", percent_error(upper_area_approx, upper_area), "%", sep="")
|
|
|
|
print("\nApproximated Bottom Half Area:", lower_area_approx, "Original:", lower_area)
|
|
print("Percent Error: ", percent_error(lower_area_approx, lower_area), "%", sep="")
|
|
|
|
|
|
# Contratulations!
|
|
|
|
|
|
|
|
# Figuring out How to draw a Trapezoid in Turtle.
|
|
|
|
win.exitonclick() |