Mathematica Lab 1

MATH 220E Spring 2025

Mathematica Basics

Here are some basic Mathematica examples.  They illustrate the syntax of some useful commands for linear algebra.  You make the commands "happen" by 
hitting the “Shift-Enter” combination (hold down the Shift key, then hit Enter or Return) when the cursor is anywhere the code cell.

Go through this worksheet, making sure you understand each part.  Then, complete the exercises at the end.

Section 1: “Calculator” commands in Mathematica

You can use Mathematica as a calculator simply by typing what you want to compute.

Note that every built-in function starts with a Capital letter and uses square brackets [ ] to surround the input to the function

Click in each code cell. Then press Shift-Enter to run the code and see the output.

(2+3)/7+Cos[Pi/4]

If you want to see the number above in decimal form, use the N[ ] function:

N[%]

The N[ ] function tries to evaluate the thing in the brackets numerically, giving a decimal (or “numerical”) result.  The percent sign represents the most recent output.

Sqrt[25]

N[Sqrt[10]]

Mathematica will do arithmetic with variables, too!  But, make sure you “clear” the variables before you use them again.

a=2;
3a

Clear[a,b];
a+7a+b

Expand[(a+b)^2]

a+7a+b+3(a+b)

The last example above might be a bit unexpected.  Mathematica is treating (a + b) as a single variable.  Look at this example:

a+7a+b+3(a+b)+2(a+b)

If you want Mathematica to distribute, try this:

Expand[a+7a+b+3(a+b)+2(a+b)]

Section 2: Creating functions and basic plotting

You can define a function and manipulate it as you would think. Look carefully at the Mathematica syntax in the example of f(x)=(x^2) below. Note that in the definition of the function, the variable name is followed by an underscore on the left side of the equation. The := symbol tells Mathematica that we want to store this function in memory but not evaluate it for x just yet.

f[x_]:=x^2

Now we can ask Mathematica to evaluate f(x) for values of x that we specify.

f[4]

f[a]

f[a+b]

Expand[f[a+b]]

You can use the Plot function to graph a single variable function in 2 dimensions. The syntax is Plot[function, range of input].

Plot[x,{x,-2,2}]

There are lots of ways to change the appearance of your graphs (different colors, dashes, thickness, etc.). To get information on this either hover your mouse over the word "Plot" above until you see a little "i" (for information) that you can click, or highlight the word "Plot" and press F1 . A help window will open that you can peruse; see especially the "Details and Options."

We can plot more than one function at a time by putting them in curly brackets.

Plot[{x,f[x]},{x,-2,2}]

Section 3: Solving systems of equations (without using matrices)

Here are a few examples, beginning with very simple ones. Notice the double equal signs and use of curly brackets for systems of equations.

Solve[x+3==0,x]

Solve[{x+y==3,2x-y==5},{x,y}]

Solve[{3x1-x2+x3-2x4 == -5, x1+x2-2x3+x4 == 1, 2x1+2x2+x3-x4 == 5, 4x1+4x3-4x4 == -1},{x4,x3,x2,x1}]

The last solution above is Mathematica’s way of saying that x4 is a free variable and the other three variables depend on it.  Thus, there are infinitely many solutions to the system of equations.

Now, we will solve the same system the exact same way, but notice a SLIGHT difference in the code at the end.  Pay close attention to the different-looking output.

Solve[{3x1-x2+x3-2x4 == -5, x1+x2-2x3+x4 == 1, 2x1+2x2+x3-x4 == 5, 4x1+4x3-4x4 == -1},{x1,x2,x3,x4}]

Here is a system with no solutions!

Solve[{x+y==3, x+y==0},{x,y}]

Exercises

Create a new file to do these exercises.

Note that Mathematica has code cells and text cells. Use code cells for commands that you want to run. Use text cells for English sentences to explain your work. You can change the type of cell using the Format menu.

To earn full credit for this assignment, you should turn in work that is not only correct, but also well organized and easy to read.

When you are finished, upload your Mathematica notebook to the Mathematica Lab 1 assignment on Moodle.

Problem 1

Define the function g(t) = (t^3) and evaluate the function at t = 2.

Problem 2

Plot both f(x)=(x^2) and g(t) (from Problem 1) on the same set of axes.

Problem 3

Why do the following code cells return different output?

Solve[{3x1-x2+x3-2x4 == -5, x1+x2-2x3+x4 == 1, 2x1+2x2+x3-x4 == 5, 4x1+4x3-4x4 == -1},{x4,x3,x2,x1}]

Solve[{3x1-x2+x3-2x4 == -5, x1+x2-2x3+x4 == 1, 2x1+2x2+x3-x4 == 5, 4x1+4x3-4x4 == -1},{x1,x2,x3,x4}]

Problem 4

Refer to the linear system given in textbook Section 1.1, Exercise 23. Find the set of solutions to this linear system using Mathematica’s Solve function, as described above under “Solving systems of equations (without using matrices).”

Problem 5

Refer to the linear system given in textbook Section 1.1, Exercise 27. Find the set of solutions to this linear system using Mathematica’s Solve function, as described above under “Solving systems of equations (without using matrices).”