Basic Mathematica Assignment Solutions

Remember--shift-enter or enter on the keypad to send information to the kernel for processing.

Problem 1a

To plot a list, you need to first create the list of ordered pairs, and then create a list of them. An ordered pair is written in Mathematica using the curly brackets, as {0,0}. The list of ordered pairs are separated by commas, and also wrapped with curly brackets, {}. Plotting a list is accomplished using the command ListPlot.

data = {{0, 0}, {1, 2}, {2.5, 4}, {3, 7}, {4, 
          5}}
ListPlot[data]

The graphic may be too small to see. You can enlarge it by clicking on it with the mouse, and dragging one of the small black squares to resize. Try it! You can also increase the size of the points by using the following command:

ListPlot[data, PlotStyle -> PointSize[0.02]]

Problem 1b

Here, we need to input a function. Remember, a function requires an underscore and the square brackets. The exponential function is already defined for you in Mathematica as Exp. Also, the plotting is accomplished using the command Plot.

f[x_]  =  Exp[x]
Plot[f[x], {x, -2, 2}]

Problem 1c

Here we plot two function on the same axis. To do that in Mathematica, we use the command Plot, and create a list of the two functions we want to plot--hence the curly brackets {f[t],g[t]} below.

f[t_]  =  t^10
g[t_]  =  Exp[t]
Plot[{f[t], g[t]}, {t, -2, 2}]

To know which is which, we may want to go back and only plot one at a time, then combine them together. Try it! Alternatively, you can colour the plots differently using the following command, which will make f (t) red and g (t) blue:

Plot[{f[t], g[t]}, {t, -2, 2}, 
      PlotStyle -> {RGBColor[1, 0, 0], RGBColor[0, 0, 1]}]

Problem 2

Mathematica treats things symbolically if it can, since symbolically is more accurate than a decimal answer. There are two ways to get your answer expressed as a decimal. One is to include a decimal in the thing you are working with. Another is to use the command N, which tells Mathematica to get a decimal. Using N or SetPrecision is powerful, since you can get as many decimals as you like!

Sqrt[20]
Sqrt[20. ]
N[Sqrt[20]]
N[Sqrt[20], 100]
SetPrecision[Sqrt[20], 100]

20^(1/3)
20^(1/3. )
N[20^(1/3), 20]
SetPrecision[20^(1/3), 20]

Problem 3

Verifying things graphically can be useful to help you understand a problem, but it doesn't really prove things in the mathematical sense of the word proof. Below we see that he function crosses the x-axis four times, and thus has four roots. We could zoom in on each root graphically, and determine an approximation to those roots graphically. That might take awhile. Mathematica allows us to solve equations symbolically using the command Solve, and numerically using the command NSolve. Notice that an equation in Mathematica uses a double equal sign (==)! We will learn more about what Mathematica is doing when it uses NSolve later when we study Newton's Method in Chapter 4.9. Notice that the symbolic solution can be quite unwieldy! Also notice that the symolic solution seems to involve complex numbers (that's what the i is), but in reality it doesn't..

g[x_] = x^4 - 56*x^2 + x + 100
Plot[g[x], {x, -10, 10}]
Solve[g[x]  == 0, x]
NSolve[g[x]  == 0, x]

If we need more accuracy, we can again use SetPrecision:

SetPrecision[%, 20]

Problem 4a

You should definitely be working the algebraic part out by hand, and not using Mathematica. We can use Mathematica to help us determine if we are correct or not by getting graphs of the functions.

f[x_]  =  x^3 + x^6
Plot[f[x], {x, -10, 10}]

This certainly looks even, but we haven't proven it is even. Let's explore a little more.

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

Well! Now the function does not look even nor odd! Our first graph had too big a scale on it. Let's prove algebraically that the function is neither odd nor even

f (-x) = (((-x))^3) +  (((-x))^6)
=(((-1))^3) (x^3) +  (((-1))^6) 
            (x^6)
=-(x^3) +  (x^6)
\(\(≠\)\(\(-f\) \((x)\)\ nor\ f \((\(-x\))\)\ so\ the\ function\ f \((x)\)\ is\ neither\ odd\ nor\ even\)\)

Problem 4b

g[x_]  =  Cos[x]
Plot[g[x], {x, -5, 5}]

This looks symmetric when flipped about the y-axis, so it looks even. Of course, knowing the trigonometric relation

cos (-x) = cos (x)

we can say that g (-x)  =  g (x), and so the function is even.

Problem 4c

h[x_]  =  1/x
Plot[h[x], {x, -3, 3}]

Here, the graph looks kinda funny. Well, that is because the function is not defined at x = 0! However, it does look like if we rotated the function 180 degrees we would get the same function back. We would guess from the graph that the function is odd. We can easily prove this algebraically.

h (-x) = (1/((-x)))
=- (1/x)
= - 
          h (x) so the function is odd.

Problem 5a

Here we make use of the command Abs.

f[x_]  =  Abs[x]
Plot[f[x], {x, -2, 2}]

Probelm 5b

We won't be using the If command very much, and certainly you can get by without it for anything you are likely to do yourself. But I wanted you to see some of the more advanced things that Mathematica can do. Notice that the graph is not a proper representation of a piecewise defined function! Can you sketch the proper graph by hand, with proper filled dots and open circles? This is an example of a case where you need to interpret the Mathematica ouput. There are others examples, and I will point out the important ones to you. The lesson here? Mathematica is useless without our own mathematical understanding to guide us in its use.

f[x_]  =  If[x  < 2, x^2, 1]
Plot[f[x], {x, -2, 5}]

Problem 5c

Here, the first graph looks very interesting, but are all the interesting features of the function really evident?

f[x_]  =  (2  x^4 - x^2 + 1)/(x^2 - 
            4)
Plot[f[x], {x, -5, 5}]
Plot[f[x], {x, -1, 1}]

By zooming in we notice that there is some extra detail that is not evident from the graph that went from -5 to 5.

Problem 6a

Let's begin by factoring f(x) = (x^3) + 1:

Factor[x^3 + 1]

We see that f(x) has a zero if x = -1, so (((-1))^3)+1=0, or (((-1))^(1/3)) = -1.

Problem 6b

Now, let's evaluate (((-1))^(1/3)).

(-1.0)^(1/3)

Hmmm, that seems strange. Above we showed that this should be -1.

Problem 6c

Let's see what the solution to the equation (x^3) + 1 = 0 is:

Solve[x^3 + 1 == 0, x]

The NSolve command will find decimal solutions, which may be more useful in this case:

NSolve[x^3 + 1 == 0, x]

This just shows that the quadratic we found above when we factored (x^3) + 1has complex valued roots.

OK, so we see that Mathematica can choose any one of three possible roots when it evaluates the quantity (((-1))^(1/3)), and it happens to choose one of the complex valued ones, not the real valued root of -1. This is because Mathematica has been instructed to choose the root with the least positive argument when it has a choice. If we prefer to have Mathematica always choose the real valued root, we need to tell Mathematica that we want it to do that. We can do that by loading the package RealOnly:

<< Miscellaneous`RealOnly`

Now let's again evaluate the cube root of -1:

(-1.0)^(1/3)

Notice that Mathematica now produces a different output for Solve:

Solve[x^3 + 1 == 0, x]

The RealOnly package will mess up some solutions when using Solve, so we won't use it much.

This type of problem will not bother us much in this course, but I want you to see some of the problems that can arise when using Mathematica.