Oct-26-2017, 11:17 PM
(Oct-26-2017, 08:19 AM)buran Wrote: iny = x + 1
y is dependent variable and x is independent variable. Inz = 2a + 5
z is dependent and a - independent variable. They use different notation to avoid confusion. It is same to write the second one asz = 2x + 5
so, it is simple math substitution:
y(x) = x +1 (1)
y(z) = z+1 (2)
z = 2x + 5 (3)
substitute (3) in (2)
y = (2x + 5) + 1 (4)
y = 2x + 6 (5)
In more general form
y = ax+b
z = cx+d
y(z) = a(cx+d) + b
y(z) = acx + ad + b
in your particular example a=b=1, c=2 and d=5
that is why
y(z) = 1*2*x + 1*5 + 1
y(z) = 2x + 6
I don't know why that confused me. I guess the "a" is what threw my off. Thank you.
This is my updated coded by the way. Is there anything I can improve?
class LinearEquations(object): def __init__(self, m, b): # print("in constructor") self.m = float(m) self.b = float(b) def __str__(self): # print("in str") # return "%2fx + %.2f" % (self.m, self.b) # optional method return "{}x + {}".format(self.m, self.b) def __repr__(self): # print("in repr") return self.__str__() def value(self, x): return self.m * x + self.b def compose(self, linear_equation): return LinearEquations(self.m * linear_equation.m, \ self.m * linear_equation.b + self.b) def __add__(self, z): """ Combine like terms by adding two linear equations together. """ return LinearEquations(self.m + z.m, self.b + z.b) # ---------------------------------------------------------------------------- # description example executed try: # create equations equation_y = LinearEquations(1, 1) equation_z = LinearEquations(2, 5) # display equations print("\nLinear Equation 1: y =", equation_y) print("Linear Equation 2: z =", equation_z) # user input for x values y_x = float(input("Enter an x value for Equation 1: ")) z_x = float(input("Enter an x value for Equation 2: ")) # value(x) method print("\ny =", equation_y.value(y_x), "if x =", y_x) # y = 1(x) + 1 print("z =", equation_z.value(z_x), "if x =", z_x) # z = 2(x) + 5 # compose(linear_equation) method print("\ny(z) =", equation_y.compose(equation_z)) print("z(y) =", equation_z.compose(equation_y)) # __add__(z) method print("\ny + z =", equation_y + equation_z) except NameError: print("Sorry, there was a name error.")I wanna say my teacher said something about an equality() method, but maybe I misheard him. There's no need for an equality method is there?