Welcome to another arduous, knackering topic of LINEAR ALGEBRA!
So, for this guide, I’ll be teaching you how to find the straight line equation, with the gradient and one point. I’ve also written a Python program that helps do this, but I’ll talk about that first. We’ll start with the theory.
Contents
Equation of a Straight Line
As always, this is the universal equation of a straight line-
![]()
is the gradient, aka the slope, aka the steepness of the line.
is the y-intercept.
Finding the Equation given the Gradient and One Point
For example, the gradient is 4, and one of the points we have is (2,1). We have to find another point to get the y-intercept.
We can calculate the other point by using algebra and our gradient formula. Do you remember it? Here it is-
![]()
So, we obviously have to plug in (2,1) into the formula-
![]()
Since we already know the value of
,
, we can also plug that in-
![]()
That leaves us with
and
, the coordinates of our next point. We can use our algebra skills to solve it now!
![]()
![]()
![]()
![]()
![]()
The Python Program
I’ll explain what I’ve written in subprograms! It’s about the same as the one I wrote for finding the straight line equation with two points.
main()
def main():
print("Welcome! Enter a point and a gradient, and I'll make the straight line equation. i will automatically round up the numbers")
print("")
gradient = int(input("Enter gradient: "))
print("")
xpoint = int(input("Enter x-coordinate: "))
ypoint = int(input("Enter y-coordinate: "))
equation_maker(gradient,xpoint,ypoint)
another_equation()
main()
In line 1, the subprogram main() is defined. To know more about Subprograms, click here! Then, line 2 explains the objective of the program.
Lines 3-8 ask the user for both the gradient and the coordinates of one point.
Lines 10 and 11 call the subprograms that will be used to form the equation. Lastly, outside of main(), line 13 calls the main() subprogram.
equation_maker()
Lets’ take a look in equation_maker()–
def equation_maker(gradient,xpoint,ypoint):
y_inter = ypoint - gradient * xpoint
r_y_inter = round(y_inter)
if r_y_inter < 0:
equation = "y" + "=" + str(gradient) + "x" + str(r_y_inter)
elif r_y_inter == 0:
equation = "y" + "=" + str(gradient) + "x"
else:
equation = "y" + "=" + str(gradient) + "x" + "+" + str(r_y_inter)
print("")
print(equation)
Line 1 defines equation_maker(), and passes in the arguments needed in the subprogram. Line 2 calculates the y-intercept needed, and line 3 rounds it up.
The rest of the code, lines 5-10, decides how the equation is formed, and lines 12 and 13 print the final answer.
another_equation()
Now, we go in another_equation(), that asks if the user wants to form another equation.
def another_equation():
print("")
confirm_another = input("Would you like to make another equation?: ").lower()
print("")
if confirm_another == "yes":
main()
Line 1 defines the subprogram. Line 3 asks if the users wants to form another straight line equation, and sets their answer to lowercase.
Then, in the last two lines, if the user answers ‘yes’, it’ll run the main() subprogram again.
The Whole Code
Finally, this is how the whole code looks like-
def equation_maker(gradient,xpoint,ypoint):
y_inter = ypoint - gradient * xpoint
r_y_inter = round(y_inter)
if r_y_inter < 0:
equation = "y" + "=" + str(gradient) + "x" + str(r_y_inter)
elif r_y_inter == 0:
equation = "y" + "=" + str(gradient) + "x"
else:
equation = "y" + "=" + str(gradient) + "x" + "+" + str(r_y_inter)
print("")
print(equation)
def another_equation():
print("")
confirm_another = input("Would you like to make another equation?: ").lower()
print("")
if confirm_another == "yes":
main()
def main():
print("Welcome! Enter a point and a gradient, and I'll make the straight line equation. i will automatically round up the numbers")
print("")
gradient = int(input("Enter gradient: "))
print("")
xpoint = int(input("Enter x-coordinate: "))
ypoint = int(input("Enter y-coordinate: "))
equation_maker(gradient,xpoint,ypoint)
another_equation()
main()
Conclusion
That’s all for this topic! I hope you could understand it. Linear algebra is HARD!!!!!! At least for me.
For the final guide, I’ll be teaching you how to plot a line in Python. Click here!
