Applying these ideas---finding the line through two given points
You and your partner should read through this section, evaluate the inputs, and work together at understanding and being able to explain what is going on. This is not an easy example. Spend some time with it, and ask questions about things that you don't understand.
We know that any non-vertical line has an equation of the form
. Let's find and plot the line that passes through two specific points, (3, 5) and (6, 4). We'll do a
restart
so that all variables start unassigned:
>
restart;
GeneralFormula := y = a*x + b;
point1 := [3,5]; point2 := [6,4];
eq1 := subs( {x = point1[1], y = point1[2]}, GeneralFormula );
eq2 := subs( {x = point2[1], y = point2[2]}, GeneralFormula );
Note that copying and pasting made entering the code for the above cell easier. I copied the first line, pasted, and made a few changes to get the second line.
>
sol := solve( {eq1, eq2}, {a, b} );
Now we know the line through the two points. We could type in the formula for the line directly, but why not use the commands we've learned to do it more easily? We want to take the General Formula
and substitute in
,
as dictated by the variable
sol
:
>
line1 := subs( sol, GeneralFormula);
This gives us the EQUATION of the line. To plot it, we need the EXPRESSION on the right hand side.
(Do you see why
plot( line1, x = -10..10);
won't work?)
But it's easy to obtain the expression from the equation, using
rhs
(which just gets the
right-hand-side
of an equation) and then plot:
>
rhs( line1 );
plot( rhs(line1), x = -10..10 );
>
Look at what we have done!
We have really written a "program" here. If we want to find the formula for a line through two different points, all we have to do is plug in the new numbers into the definitions of
point1
and
point2
and re-evaluate the inputs (in order).