Python Forum
Linear Equations Homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Linear Equations Homework
#3
(Oct-26-2017, 08:19 AM)buran Wrote: in y = x + 1 y is dependent variable and x is independent variable. In z = 2a + 5 z is dependent and a - independent variable. They use different notation to avoid confusion. It is same to write the second one as z = 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?
Reply


Messages In This Thread
Linear Equations Homework - by ngr33n - Oct-26-2017, 02:25 AM
RE: Linear Equations Homework - by buran - Oct-26-2017, 08:19 AM
RE: Linear Equations Homework - by ngr33n - Oct-26-2017, 11:17 PM
RE: Linear Equations Homework - by buran - Oct-27-2017, 07:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  solve linear system of r equations amalalaoui 0 1,972 Feb-20-2020, 03:33 PM
Last Post: amalalaoui

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020