0% found this document useful (0 votes)
59 views11 pages

New01 Intro

Uploaded by

xzsjoson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views11 pages

New01 Intro

Uploaded by

xzsjoson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Computer Assignment

Introduction to Mathematica
Ÿ Double click on the brackets on the right to open sections.

Technology Guidelines
TO START WITH A CLEAN SLATE: Restart Mathematica or Quit Kernel (under Evaluation)

TO OPEN ALL CELLS: Highlight bracket on far right and select Grouping under the Cell pull-down
menu and choose Open All Subgroups.

TO STOP AN EXECUTION: Select the Evaluation pull-down menu and click on Abort Evaluation.

ORDER OF EXECUTION:Execute cells in the order given in a notebook.

SAVE YOUR WORK OFTEN

EXPERIENCING MAJOR PROBLEMS: Save if appropriate, then shut down Mathematica and start it up
again. Sometimes, it also helps to turn off your computer and start it up again.

TO PRINT YOUR RESULTS:


1. Select far right bracket, Format pull-down menu, and Word Wrapping - Wrap at Paper Width. If you
want to print only selected cells, highlight the sections you want printed while holding down the Ctrl key,
then select Print Selection from the File pull-down menu.
2. You can also save and print or send your file as a pdf.
3. If you want to print your answers in a Word or text document, copy and paste the contents of the
cells you want included. For graphs, copy as Bitmaps or Metafiles

Purpose and Prerequisites


Purpose: To provide a brief introduction to the Mathematica computing environment and to use Mathe-
matica to solve systems of linear equations.

Prerequisite Reading: Section 1.1

Mathematica Functions:
Assignment (=) to give a name to a quantity, such as a matrix
Clear to remove any previously defined assignments of a variable
MatrixForm to view a matrix in matrix form, instead of as a list of lists
[[ ]] double brackets used to pull parts out of lists
RowReduce quick way to perform elementary row operations
Copyright (c) 2016 Pearson Education, Inc.

Palette Needed: In preparation for this notebook and others as well, select the pull-down menu item,
Palette. Then select Other and choose Basic Math Input. This palette will appear in the upper right
corner of your screen.
Prerequisite Reading: Section 1.1

Mathematica Functions:
2 | new01_Intro.cdf
Assignment (=) to give a name to a quantity, such as a matrix
Clear to remove any previously defined assignments of a variable
MatrixForm to view a matrix in matrix form, instead of as a list of lists
[[ ]] double brackets used to pull parts out of lists
RowReduce quick way to perform elementary row operations

Palette Needed: In preparation for this notebook and others as well, select the pull-down menu item,
Palette. Then select Other and choose Basic Math Input. This palette will appear in the upper right
corner of your screen.

Introduction to Mathematica (8 exercises)

Mathematica Arithmetic
Mathematica can be thought of as a sophisticated calculator, able to perform exact as well as approxi-
mate arithmetic computations. As you read through this file, you will be asked to execute the contents
of input cells (shaded cells like the one immediately below this paragraph). As a first example, you will
add the fractions appearing in the following input cell. Using your mouse, click anywhere inside the
following shaded cell and then press Enter on the numeric keypad or Shift-Enter on the QWERTY
keypad.

552 291 225 555


+
67 158 4 512 874

208 963 959 752


25 256 299 341

The first command you execute may take a little longer to perform due to the fact that on many com-
puter platforms, the bulk of the Mathematica program is not loaded into memory until the first Mathemat-
ica operation is executed.

Also note that the output to our first calculation is an exact answer in rational form (i.e., the output is
expressed as the fraction of two integers).

Exercise 1
1 5
Compute + with Mathematica. To do this, in the open space immediately below this cell and
31 72
before the following cell entitled Arithmetic Operations, click the mouse button and a horizontal line will
appear marking the location of the new cell you are about to create. Next, on the palette, click on the
‚
button containing Enter 1, then press Tab (or click on the denominator with your mouse) and then
ƒ
enter 31. (Another method is to type in 1/31 from the keyboard.) Next, press the right arrow button (®)
on the keyboard to move the cursor to the right of the fraction. Press the plus (+) and then enter the
second fraction. Finally, press Shift+Enter to execute the command.

Arithmetic Operations
The following table summarizes the Mathematica symbols used for addition, subtraction, multiplication,
division and powers.

Copyright (c) 2016 Pearson Education, Inc.


new01_Intro.cdf | 3

The following table summarizes the Mathematica symbols used for addition, subtraction, multiplication,
division and powers.
Operation Mathematica Operation
addition +
subtraction -
multiplication * or at least one space
division  or enter ƒ
ƒ
from the palette
power ^ or enter ƒƒ from the palette
grouping terms HL
Execute the following command to compute 2 to a power of 500. Notice that the answer is so long that
it cannot fit on a single line!

2500

3 273 390 607 896 141 870 013 189 696 827 599 152 216 642 046 043 064 789 483 291 368 096 133 „
796 404 674 554 883 270 092 325 904 157 150 886 684 127 560 071 009 217 256 545 885 393 053 „
328 527 589 376

A space can be used for multiplication. Therefore, to compute 56987 times 23186, at least one space
must be inserted between the two numbers. In newer versions, Mathematica will insert the × symbol to
denote multiplication. Execute the following computation.

56 987 ´ 23 186

1 321 300 582

Parentheses are used to group terms. Never use square brackets [ ] or curly brackets { } (braces) for
grouping in arithmetic or algebraic computations. Always use parenthesis ( ). Bracket and braces are
used for other purposes.For example, execute the following arithmetic operation.

5 H8 - 6L + 25

42

Entering a Matrix
1 3 5 9
Suppose you wish to enter the matrix 2 0 3 7 and assign it to the letter A. One way to create this
-5 4 3 2
is to enter A={{1,3,5,9},{2,0,3,7},{-5,4,3,2}} where each set of inner braces represents a row of A. Exe-
cute the following command to assign A to the given matrix.

A = 881, 3, 5, 9<, 82, 0, 3, 7<, 8- 5, 4, 3, 2<<

881, 3, 5, 9<, 82, 0, 3, 7<, 8- 5, 4, 3, 2<<

To view A in matrix form, execute the following command.

Copyright (c) 2016 Pearson Education, Inc.


4 | new01_Intro.cdf

MatrixForm@AD

1 3 5 9
2 0 3 7
-5 4 3 2

Notice the square brackets used to enclose the argument. Functions in Mathematica always use
square brackets, not parentheses or braces to enclose arguments.

Exercise 2
1 2 3 4 5
Use braces { {...}, {...},..., {...} } to create the matrix B = 6 7 8 9 10 .
11 1 2 13 0

A More Natural Method for Creating a Matrix


A more natural way to enter a matrix is to enter the matrix in matrix form, although it will still be stored
as the list of lists above, Open a new cell and from the Basic Math Input Palette, select the 2x2 blank
ƒ ƒ
matrix . With your cursor anywhere in the matrix, press Ctrl Enter. That will add another row.
ƒ ƒ
Pressing Ctrl followed by a comma will enter another column. Do this twice to get a matrix with three
rows and four columns.

ƒ ƒ ƒ ƒ
ƒ ƒ ƒ ƒ
ƒ ƒ ƒ ƒ

88ƒ, ƒ, ƒ, ƒ<, 8ƒ, ƒ, ƒ, ƒ<, 8ƒ, ƒ, ƒ, ƒ<<

Yet another way to enter the matrix in matrix form is to select the Insert pull - down menu, choose
Table/Matrix and use it to add rows or columns to an existing matrix or create a new matrix with the
specified number of rows and columns.The blank matrix above will appear : The matrix contained in the
following input cell was constructed using this method.Execute the following assignment statement.

1 3 5 9
A= 2 0 3 7
-5 4 3 2

881, 3, 5, 9<, 82, 0, 3, 7<, 8- 5, 4, 3, 2<<

Once again, the output is not in matrix form. You could have asked for MatrixForm in the output at the
time you first defined A, as follows:

MatrixForm@A = 881, 3, 5, 9<, 82, 0, 3, 7<, 8- 5, 4, 3, 2<< D

1 3 5 9
2 0 3 7
-5 4 3 2

One thing that you DO NOT want to do is to define a matrix as one in matrix form:
A = MatrixForm[{{1,2},{3,4}}]
Copyright (c) 2016 Pearson Education, Inc.
If you do this, you will not be able to manipulate the matrix as we will do in what is to follow.
new01_Intro.cdf | 5

One thing that you DO NOT want to do is to define a matrix as one in matrix form:
A = MatrixForm[{{1,2},{3,4}}]
If you do this, you will not be able to manipulate the matrix as we will do in what is to follow.

Exercise 3
-2.5 0 2 11
Assign F to equal 1 2 3 0 using the Insert-Table/Matrix option and ask for it in matrix form.
-9 5 7 -3

Assignment Names
Execute the following assignment statement:

1 2 3
D= 1 0 0
1 0 5

Set::wrsym : Symbol D is Protected. ‡

881, 2, 3<, 81, 0, 0<, 81, 0, 5<<

When the assignment statement is executed, an error message is displayed, indicating that D is a
protected symbol. This is due to the fact that D is actually a Mathematica command used to find the
derivative of a function. All Mathematica commands are protected symbols and all protected symbols
begin with a capital letter. So if you create an assignment statement and you obtain a protected symbol
error, then you will need to choose a new name in your assignment statement. If you wish to use
another capital letter to represent your matrix, avoid using C, D, E, I, N, and O since these are all
protected Mathematica symbols. Assignment of any other upper-case number is allowed.

With Mathematica, you can use a string of letters for assignment names as long as you begin with a
letter of the alphabet and no spaces occur in your assignment statement. An easy way to avoid pro-
tected symbol errors is to use an assignment name which begins with a lower case letter. By following
this rule of thumb, you will never obtain protected symbol errors since all protected symbols begin with
a capital letter. So instead of using the letter D, you can use something like matD. Execute the follow-
ing command.

1 2 3
matD = 1 0 0
1 0 5

881, 2, 3<, 81, 0, 0<, 81, 0, 5<<

Notice that the matrix is displayed again in an output statement. Placing a semicolon at the end of an
assignment statement suppresses the display of the output. Execute the following command:

matD = K O;
1 2
0 1

To include more than one statement in your input, semicolons can be used, but they also suppress
output. A better way to include more than one line of input is to use the Return or Enter key on the
QWERTY pad. That key simply moves your cursor to the next line, thus opening a new line for you to
Copyright
type code. Do not press Shift-Enter (c) 2016
or Enter onPearson Education, keypad
the numeric Inc. to open a new line.
6 | new01_Intro.cdf

To include more than one statement in your input, semicolons can be used, but they also suppress
output. A better way to include more than one line of input is to use the Return or Enter key on the
QWERTY pad. That key simply moves your cursor to the next line, thus opening a new line for you to
type code. Do not press Shift-Enter or Enter on the numeric keypad to open a new line.

A=K O
1 2 3
0 1 -7

B=K O
-1 3 -2
2 5 10

881, 2, 3<, 80, 1, - 7<<

88- 1, 3, - 2<, 82, 5, 10<<

Solving Systems of Linear Equations


Suppose you want to use elementary row operations to solve a system of linear equations:
2 x1 + 4 x2 = 10
6 x1 + 4 x2 = 14
To begin, execute the following command to set up the corresponding augmented matrix:

MatrixForm@M = 882, 4, 10<, 86, 4, 14<<D

K O
2 4 10
6 4 14

Notice that a matrix is now displayed immediately after a statement of the form
Out[n]//MatrixForm=
Determine the specific value of n (called the output number) given in this statement. The matrix appear-
ing in this output statement is automatically assigned the name Out[n] ( %n is shorthand for Out[n]).
For example, if the output number is 19, then the name of the matrix is %19.

There are numerous ways to perform elementary row operations in Mathematica to solve the system.
1 0 ƒ
One way is to perform elementary row operations to convert the matrix M into the form ,
0 1 ƒ
where the blank entries on the right will be the values of x1 and x2 that represent a solution to the sys-
tem.

Since matrices in Mathematica are represented as lists of lists, row operations can be performed by
assigning each row of a matrix a name. We can define each row separately by using the double-
bracket command that separates the parts of the matrix as follows:

r1 = M@@1DD
r2 = M@@2DD

82, 4, 10<

86, 4, 14<

So the matrix M is {r1,r2}.

We can redefine M to be the matrix with rows 1 and 2 reversed. To do this we could reassign the value
Copyright (c) 2016 Pearson Education, Inc.
of M to be this new matrix. For convenience, we can go ahead and rename the rows of the new matrix.
new01_Intro.cdf | 7

So the matrix M is {r1,r2}.

We can redefine M to be the matrix with rows 1 and 2 reversed. To do this we could reassign the value
of M to be this new matrix. For convenience, we can go ahead and rename the rows of the new matrix.

MatrixForm@M = 8r2, r1<D


r1 = M@@1DD;
r2 = M@@2DD;

K O
6 4 14
2 4 10

Now, replace row 1 by 1/6 times itself

MatrixForm@M = 8r1  6, r2<D

2 7
1 3 3
2 4 10

If the output is what you expected, execute the following command to redefine r1 and r2.

r1 = M@@1DD;
r2 = M@@2DD;

Now, replace row 2 by “row 2 - 2 row 1”.

MatrixForm@M = 8r1, r2 - 2 r1<D

2 7
1 3 3
8 16
0 3 3

If the output is what you expected, execute the following command to redefine r1 and r2.

r1 = M@@1DD;
r2 = M@@2DD;

Now, replace row 2 by "3/8 row2". If your output from the next input cell is what you expected, execute
the input cell that follows it to again redefine r1 and r2.

MatrixForm@M = 8r1, 3  8 r2<D

2 7
1 3 3
0 1 2

r1 = M@@1DD;
r2 = M@@2DD;

Finally, replace row 2 by "row 1 - 2/3 row2".

Copyright (c) 2016 Pearson Education, Inc.


8 | new01_Intro.cdf

MatrixForm@M = 8r1 - 2  3 r2, r2<D

K O
1 0 1
0 1 2

1 0 1
The output should be and therefore, translating out of matrix form gives the solution is x1 = 1
0 1 2
and x2 = 2.
The key here is to do each of these steps in order and, each time, redefine your r1 and r2 after you
have examined your output matrix to make sure you have accomplished what you set out to accom-
plish. If you find yourself making mistakes be sure not to redefine r1 and r2 before you correct your
mistakes.
A fast way to accomplish the row reduction to echelon form is with the built-inMathematica RowReduce
command. We will ask for the output in MatrixForm. Again, notice the square brackets to enclose both
arguments.

MatrixForm@RowReduce@MDD

K O
1 0 1
0 1 2

1 0 1
The output should be and therefore, x1 = 1 and x2 = 2.
0 1 2

Exercise 4
-2 x2 = 6
Use matrices to solve the following linear system:
2 x1 + 4 x2 = -8
Follow the instructions below to begin this exercise. Once you have found the solution, fill in the missing
values immediately below (to enter a number, click inside each empty box (ƒ) and type in the appropri-
ate number and then press the TAB key to quickly move to the next box).
x1 = ƒ, x2 = ƒ
Check your solution (by hand) by substituting the values of x1 and x2 into each of the original equations.
Execute the following assignment statement to set up the corresponding augmented matrix and define
the rows of that matrix.

M=K O;
0 -2 6
2 4 -8
r1 = M@@1DD
r2 = M@@2DD

80, - 2, 6<

82, 4, - 8<

The first step is to switch rows, by executing the following command:

Copyright (c) 2016 Pearson Education, Inc.


new01_Intro.cdf | 9

MatrixForm@M = 8r2, r1<D

K O
2 4 -8
0 -2 6

Now continue the process to find a solution.

Exercise 5
3 x1 - 2 x2 + 6 x3 = 33
Solve the following linear system: 6 x1 - 4 x2 + 5 x3 = 31
9 x1 + x3 = 14
using elementary row operations as discussed above.
Begin by executing the assignment statement below to set up the corresponding augmented matrix and
defined its rows. Once you have found the solution, fill in the missing values immediately below:
x1 = ƒ, x2 = ƒ, x3 = ƒ

3 - 2 6 33
M= 6 - 4 5 31 ;
9 0 1 14
r1 = M@@1DD;
r2 = M@@2DD;
r3 = M@@3DD;

Exercise 6
In this exercise, you will use the RowReduce command to solve the following linear system
x1 - 2 x3 = -1
x2 - x4 = 2
-3 x2 + 2 x3 = 0
-4 x1 + 7 x4 = -5
Execute the assignment statement representing the corresponding augmented matrix (output sup-
pressed), followed by the RowReduce command, where we ask for the result in matrix form.

1 0 -2 0 -1
0 1 0 -1 2
M=
0 -3 2 0 0
-4 0 0 7 -5
MatrixForm@RowReduce@MDD

881, 0, - 2, 0, - 1<, 80, 1, 0, - 1, 2<, 80, - 3, 2, 0, 0<, 8- 4, 0, 0, 7, - 5<<

1 0 0 0 -4
0 1 0 0 -1
3
0 0 1 0 -2
0 0 0 1 -3

Read the solution from the output


and fill in the missing values for the

Copyright (c) 2016 Pearson Education, Inc.


10 | new01_Intro.cdf

and fill in the missing values for the variables.


x1 = ƒ, x2 = ƒ, x3 = ƒ, x4 = ƒ
You can check to see if your answers are correct.

Using the Symbolic Power of Mathematica


1 h 2
Suppose you want to determine the values of h such that is the augmented matrix of a
2 -4 5
consistent linear system. Execute the following cell. (The Clear command is used to clear out any
value that may have been assigned to h during the current computer session. It produces no output.)

Clear@hD
M=K O;
1 h 2
2 -4 5
r1 = M@@1DD;
r2 = M@@2DD;

Now execute the following command:

MatrixForm@M = 8r2, r1<D


r1 = M@@1DD;
r2 = M@@2DD;

K O
2 -4 5
1 h 2

Now, replace row 2 by "row 2- 1/2 row1"

MatrixForm@M = 8r1, r2 - 1  2 r1<D


r1 = M@@1DD;
r2 = M@@2DD;

2 -4 5
1
0 2+h - 2

From observing the bottom row, the system is inconsistent if and only if 2 + h = 0. So, the system is
consistent if and only if h ¹ -2.

Exercise 7
Determine the value(s) of h such that the following matrix is the augmented matrix of a consistent
system.

K O
1 4 -2
3 h -6
Begin by executing the following cell and when you complete your work, answer the following question
(click at the end of the question below and begin typing):
For what values of h is the system consistent?

Copyright (c) 2016 Pearson Education, Inc.


new01_Intro.cdf | 11

Clear@hD
MatrixFormBM = K OF
1 4 -2
3 h -6
r1 = M@@1DD;
r2 = M@@2DD;

K O
1 4 -2
3 h -6

Exercise 8
Find an equation involving g, h, and k that makes the following augmented matrix correspond to a
consistent system
2 5 -3 g
4 7 -4 h
-6 -3 1 k
Begin by executing the following cell and when you have found the answer, complete the following
statement by replacing each box with an appropriate number.
The system is consistent if and only if ƒ g + ƒ h + ƒ k = ƒ.

Clear@g, h, kD
2 5 -3 g
M= 4 7 -4 h ;
-6 -3 1 k
r1 = M@@1DD;
r2 = M@@2DD;
r3 = M@@3DD;

Copyright (c) 2016 Pearson Education, Inc.

You might also like