Linear Functions
with Mathematica

by
K. D.  Stroyan
University of Iowa

copyright 2007 by Keith Stroyan - All rights reserved.

Review: Closed and Open Mathematica Cells

The NoteBook MmaNtBkCells introduces you to use of cells.

To open a closed cell, use your mouse to position the cursor over the dark arrowhead and click the button on the mouse twice rapidly.  (This is called "double clicking.")  When you are done looking at a section, you can position the cursor at the far right bracket again and double click to close that section.

Notebook Overview

This NoteBook introduces the uses Mathematica to help you solve the linear function exercises of Chapter 1 of the main text.  The goal of the NoteBook is for you to solve the Exercises below in this NoteBook.  The sample computations help you do this.

Equations for a Linear Function

The basic differential equation of a line is:

dy/dx = m,  a constant.

In the local variables  (dx,dy)  that go through a point on the line.  The constant m is the slope of the line.  We may view this statement as a rule for how  y  changes as  x  changes. When we also know a point where this change begins, we can find the line.

Since the local variables are given by  dx = x - x0  and  dy = y - y0  if the localization point is  (x0,y0), we can convert the (dx,dy)-equation to:

(y - y0)/(x - x0) = m

When we simplify the algebra, can even put this in the familiar slope-intercept form

y = m x  + b

This NoteBook helps you view a linear function these three ways with Mathematica.

Help?

Additional information on numerical computation with Mahtematica can be found in the NoteBook MmaNumerics, graphical computation in MmaGraphics and list computations in MmaLists all in the folder aComputerIntro of Chapter 1.

Point-slope formulas for a line in Local Coordinates

The line through the point (x0,y0) with slope  m  has the (dx,dy)-equation

dy = m dx

where  (dx,dy)  are the parallel coordinates to (x,y) through (x0,y0).  We can work with this directly.  <Enter> the next computation.

Clear[dx,dy,m,scale];
SetOptions[Plot,PlotRange->{-scale,scale},AxesOrigin->{0,0},AspectRatio->Automatic,AxesLabel->{"dx","dy"}];
dy:=m dx;
m=-2;
scale=2;
Plot[dy,{dx,-scale,scale}]
Print["dy = ",m," dx"];

Exercise: Graph in Local Coordinates

Graph a line of slope  -3  using local  (dx,dy)  coordinates through a point  (x0,y0).  We will tell you the value of the (x,y)-point later.  Plot with equal CHANGES of 6 units in  x  and  y.

Copy the computation of the graph above and modify it to plot the line you want.

To copy a group of cells:

FIRST, move the cursor to the line at the right of the window that marks the collection you wish to copy and click once to highlight it.

SECOND, leaving it highlighted, click Edit on the main Mathematica menu and click Copy inside that menu.

THIRD, move the mouse to the empty cell below here and click once to get an insert cursor (or horizontal line, either works).

FOURTH, click Paste from the Edit menu.

Do this cut and past now for the line graph above.  Then edit the typing to compute the example with dy = -3 dx below here.  To edit, move the cursor to the right of something you want to change and click once to get the insert vertical line.  Next, delete the un-wanted symbols and type what you want.

Alternate solution:

Type the necessary commands below where we have typed ??  To do this, move the cursor to the right of something you want to change and click once to get the insert vertical line.  Next, delete the un-wanted symbols and type what you want.

Clear[dx,dy,m,scale];

dy := ??

scale = 6;
Plot[ ??, {??,-scale,scale},
		PlotRange->{-scale,scale},
		AspectRatio -> Automatic,AxesLabel->{"dx","dy"}];

The Point-Slope Formula in Original Coordinates

Since dx = x - x0  and  dy = y - y0, it is easy to convert the equation

dy = m dx

to

y = y0 + m(x - x0)

So we can make the same plot with regular coordinates as follows:

Clear[x,y,m,x0,y0,scale];
SetOptions[Plot,AxesOrigin->{x0,y0},PlotRange->{y0-scale,y0+scale},AspectRatio->Automatic,AxesLabel->{"x","y"}];
y[x_]:=y0+m (x-x0);
Print["y[x] = ",y[x]];
x0=-4;
y0=3;
m=-2;
scale=2;
Plot[y[x],{x,x0-scale,x0+scale}]
Print["y[x] = ",y[x]];

Perhaps you don't like Mathematica's choice of axes.  Try this:

Plot[y[x],{x,x0-scale,x0+scale},PlotRange->{y0-scale,y0+scale},AspectRatio->1,AxesLabel->{"x","y"},AxesOrigin->{0,0}]

And this:

scale=5;
Plot[y[x],{x,x0-scale,x0+scale},PlotRange->{y0-scale,y0+scale},AspectRatio->1,AxesLabel->{"x","y"},AxesOrigin->{0,0}]

Simplify and plot

Mathematica can expand too, if you wish:

Clear[x,y,m,x0,y0,scale];
y[x_]:=Expand[y0+m (x-x0)];
Print["y[x] = ",y[x]];

Plot this one:

x0=-4;
y0=3;
m=-2;
scale=5;
Plot[y[x],{x,x0-scale,x0+scale},PlotRange->{y0-scale,y0+scale},AspectRatio->1,AxesLabel->{"x","y"},AxesOrigin->{0,0}]
Print["y[x] = ",y[x]];

Exercise: Graph in (x,y) Coordinates

Define a linear function  y[x]  that has slope  -3  and goes through the  (x,y)  point  (5,-4).  Plot the function with equal scales on the axes for points that are equivalent to the previous plot, that is so the CHANGES in  x  and  y  go from  (5,-4)  plus and minus  6  units.  (You can use the Edit menu to copy and modify one of the Sample computations above or complete the commands below here.  (It's easier to learn the copy, paste & modify method.)

Clear[x,y];

y[x_] := ??

Plot[???]

Text Example 1.5: Two Points Determine a Line

Two points determine a slope:

Geometrically, two points determine a line.  Practically, we put the ruler at each point and draw.  Here's how to do this symbolically for the line through  (-2,3)  and  (1,4): (See Text Example 1.5)

Clear[x,y,z,m,dy,dx];
m=((4-3)/(1--2));
dy:=m dx;
scale=6;
Plot[dy,{dx,-scale,scale},PlotRange->{-scale,scale},AxesOrigin->{0,0},AspectRatio->1,AxesLabel->{"dx","dy"}]
Print["dy = ",m," dx"];

Or in ordinary coordinates: (See Text Example 1.5)

Clear[y,x];
y[x_]:=4+m (x-1);
scale=6;
Plot[y[x],{x,1-scale,1+scale},PlotRange->{4-scale,4+scale},AxesOrigin->{0,0},AspectRatio->1,AxesLabel->{"x","y"}]
Print["y = ",m,"(x - 1) + ",4];

Original Coordinates Give the Same Line Two Ways:

We can also use  (x,y) coordinates two ways: (See Text Example 1.5)

Clear[x,y,z];
{x0,y0}={-2,3};
y[x_]:=y0+m (x-x0);
Print["Expand from first point:  y[x] = ",y[x]];
Expand[y[x]]
{x1,y1}={1,4};
z[x_]:=y1+m (x-x1)
Print["Expand from second point:  z[x] = ",z[x]];
Expand[z[x]]

But these must actually be the same:

Print["y[x] = ",Simplify[y[x]]];
Print["z[x] = ",Simplify[z[x]]];

Here's a plot:

scale=5;
Plot[y[x],{x,x0-scale,x0+scale},PlotRange->{y0-scale,y0+scale},AspectRatio->1,AxesLabel->{"x","y"},AxesOrigin->{0,0}]

Exercise: A Line from Two Points

Exercise 1.3.3 & 1.3.6

Copy, paste & modify or imitate commands above to do the following computations.

Two points determine a slope:

Calculate the slope of the line through the points  (3,-2)  and  (-4,5).

m=((??)/(??))

Two points determine an  (x,y)  function:

Use the slope from the previous computation and the point  (3,-2)  to give a linear function  y[x]  through both points.  Verify that your  y[-4] = 5. (You can also copy, paste and modify a sample computation above.)

Clear[x,y];
y[x_] := ??
??
??
Print["y[-4]\\\ =\\\ ",y[-4]];
Plot[??]

Plot your function on a graph that shows both points, for example:

scale=8;
Plot[y[x],{x,x0-scale,x0+scale},PlotRange->{y0-scale,y0+scale},AspectRatio->1,AxesLabel->{"x","y"},AxesOrigin->{0,0}]

Two points determine another  (x,y)  function:

Use the slope from the previous computation and the point  (-4,5)  to give a linear function  z[x]  through both points.  Verify that your  z[3] = -2.

Clear[x,z];
z[x_] := ??
??
??
Print["z[3]\\\ =\\\ ",z[3]];

Plot your function on a graph that shows both points, for example:

scale = 8;
Plot[ ??[x]  ,{x,x0-scale, x0+scale},
		PlotRange->{y0-scale,y0+scale},
		AspectRatio -> 1,AxesLabel->{"x","y"},
		AxesOrigin->{0,0}];

The two functions agree:

Show that the two functions are actually equal.

Expand[y[x]]
Expand[z[x]]

Simplify[y[x]]
Simplify[z[x]]

Linear data (Mathematica solution of the linear data Exercise 1.3.4).

The linear data in Table 1.1 of Chapter 1 Section 3 is a list of (x,y) pairs.  The next cell combines the  x  list and  y  list into a list of pairs.  Lists are a little complicated, but <Enter> it now and see what happens.

Clear[xList,yList,pairs];
xList={1,2,5,6.3};
yList={3,1,-5,-7.6};
pairs=Transpose[{xList,yList}];
Print["A set of (x,y) pairs"];
Print[pairs];

We would like to see the plot of points and the line through them, but this too is a little technical.  (You can copy this entire computation any time you need to do this.)  <Enter> it now.

plot1=ListPlot[pairs,Prolog->AbsolutePointSize[6],AxesLabel->{"x","y"},DisplayFunction->Identity];
plot2=ListPlot[pairs,Prolog->AbsolutePointSize[6],AxesLabel->{"x","y"},Joined->True,DisplayFunction->Identity];
Show[plot1,plot2,DisplayFunction->$DisplayFunction]

Two points determine a slope by taking the  y  change over the  x  change.  One of the points together with this slope then gives the point-slope equation for a linear function.

Clear[x,y,m,x0,y0];
y[x_]:=y0+m (x-x0);
x0=xList[[1]];
y0=yList[[1]];
m=((yList[[2]]-yList[[1]])/(xList[[2]]-xList[[1]]));
Print["The first two points say  y = ",Simplify[y[x]]];

The whole set of data truly is linear if the function determined by the first two points matches at all the points.  <Enter> the next computation to check.

Print["Function y[xList]=",y[xList]];
Print["Original yList = ",yList];