Makerspace at the Fulton School
This program was started in my math class but was quickly transferred to the programming elective that I am in. In the elective, I was able to not only complete the programming but also test different theories by running the code (this can be seen below my print statements with a “#” to symbolize notes without affecting the code). After the initial outline was taught, I had free reign to finish and organize the code how I pleased.
This type of programming is object-oriented. For this reason, I was able to create a class that had multiple functions to solve basic quadratic math. Since all the functions are within a class, in future cases, all one has to do is “call” the class and will then have access to the functions. An example of calling a class is demonstrated at the top of my second file where I called the “ProgrammingElective class” that I had just created. This made it possible for me to print direct functions without having to rewrite all of the code.
The way that I tested each function was by creating an additional file that held all the “print” statements so that I could clearly decipher which function I was calling and what exactly it was calculating without having to read in between code.
The first sheet of code is the class itself.
from vpython import*
class quadratic:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def zeros(self):
d = (self.b**2)-4*self.a*self.c
if d >0:
z1 = (-self.b + d**0.5)/2*self.a
z2 = z2 = (-self.b - d**0.5)/2*self.a
else:
z1 = None
z2 = None
return (z1, z2)
def min0max(self):
x = -self.b/(2*self.a)
y = self.a*(x**2) + self.b*x + self.c
return (x,y)
def min0maxidentity(self):
if self.a < 0:
return ("minimum")
else:
return ("maximum")
def slope(self, x):
y = 2*self.a*x + self.b
return y
def y(self, x):
y = self.a*(x)**2 + self.b*x + self.c
return y
def corddistance(self, x1, x2):
y1 = self.y(x1)
y2 = self.y(x2)
cd = distance(x1, y1, x2, y2)
return cd
def linedistance(self, x1, x2, x3, x4, x5):
cd1 = self.corddistance(x1, x2)
cd2 = self.corddistance(x2, x3)
cd3 = self.corddistance(x3, x4)
cd4 = self.corddistance(x4, x5)
ld = cd1 + cd2 + cd3 + cd4
return ld
def linedistance2(self, x1, x2, dx):
ldt = 0
for i in arange(x1, x2, dx):
x1 = i
x2 = i + dx
ld2 = self.corddistance(x1, x2)
ldt = ld2 + ldt
return ldt
def slopecord(self, x1, x2):
y1 = self.y(x1)
y2 = self.y(x2)
sc = (y2-y1)/(x2-x1)
return sc
def distance(x1, y1, x2, y2):
y = ((x2-x1)**2 + (y2-y1)**2)**0.5
return y
This next sheet contains the print statements as well as the notes.
from ProgrammingElective import*
from vpython import*
q = quadratic(1, -2, -3)
print("zeros of equation =", q.zeros())
print("min and max of equation =", q.min0max())
print("min or max identity =", q.min0maxidentity())
#used calculas to make quation for function
print("slope of equation =", q.slope(4))
print("distance between points =", q.corddistance(-2,3))
print("line length of curve =", q.linedistance(-2, -1, 0, 1, 2))
print("total length =", q.linedistance2(-2, 2, .00001))
#did not use calculas
print("slope of cord =", q.slopecord(3.999, 4))
#the following are results for the "q.linedistance" function
#11.089724298507356 - 1 (dx)
#11.224464159058982 - 0.1 (dx)
#11.226015942633229 - 0.01 (dx)
#11.226031459417593 - 0.001 (dx)
#11.226031614584913 - 0.0001 (dx)
#11.226031616156195 - 0.00001 (dx)
#the following are results for the "q.slopecord" function
#3 (x) - 5
#3.5 (x) - 5.5
#3.75 (x) - 5.75
#3.9 (x) - 5.900000000000002
#3.99 (x) - 5.990000000000023
#3.999 (x) - 5.999000000000748
Recent Comments