Mathematica Workshop

Basic Mathematica

To start with, let’s try using some of the functions that we’ve already seen. Try changing the parameters to check what happens.

Binomial[15,4]

What happens if you try to only give one argument to the function? Do you notice any styling to indicate that the function needs two arguments?

Try using the Simplify command to simplify (x-3)(x+3)(x^2+9)+80. Type in the command after this  Note that powers are typed using the ‘^’ symbol. If you want the power to be displayed as a superscript, you can type Ctrl-^. This will allow you to type a superscript power, you should then press the right arrow key to get back to the normal level.

Simplify(1/(x+1))+(1/(x-1)) below this text cell.  You can type in a fraction by pressing Ctrl-/. You can also open the ‘Basic Math Assistant’ from the ‘Palettes’ menu to help you enter commands in a nicely displayed form.

Try to simplify ((Sqrt[(x^2) + 2x + 1]))- 1. Did it give what you expected? By default, Mathematica doesn’t assume that the variables we give it are real. We can give another argument to Simplify to tell it what assumptions to make. Try adding x > 0, or Element[x, Reals] as a second argument to Simplify.

What is the tenth coefficient of (((2x-π))^16)? To type π, or other Greek letters, press Escape, then type pi then press Escape again.

Try entering a list by using curly braces. A function we often need when using lists is Length. Try getting the Length of the list you entered.

We can use this to count the number of Divisors of a number. Use Length to count the number of Divisors of 1236712378. How about the number of Divisors of 1987?

What is the definite integral of ((Sec[y])^2) for y between -π/4 and π/3?

What is the indefinite integral of ((x^5)/(Sqrt[(x^3)+1]))with respect to x?

Let’s check that the derivative of the integral of a function is the function we started with. Compute the derivative with respect to x, of the indefinite integral with respect to x, of (5/((x^3)+8)). You might want to simplify your answer. You can chain these functions together in one cell, or use the % function to do each step separately -- try doing both.

Search the documentation for the % function. How do you refer to the output of the last but one computed cell?

Look up the documentation for the Table function. Use this to create a list which contains the eigenvalues for each of the three Pauli matrices separately.

Let matrix1 = (({{1, 0, 0}, {-2, 2, 0}, {-(14/3), -(5/3), 3}}))
and matrix2 = (({{-1, 0, 0}, {-3, 2, 0}, {-3, -3, 5}}))
create a single list which contains all the eigenvalues of both matrices without duplicates.

Find two non-diagonal 4 by 4 matrices which have exactly 2 shared integer eigenvalues.

Plot Log(Cos(x)+x-(1/2))between 0 and 10. Remember that  Mathematica doesn’t use parentheses for its functions.

Plot (1/((2x+3)))between -5 and 5 in red. Look at the documentation for plot, in particular the ‘Highlighting Discrete Function Features’ section, under the ‘Applications’ heading. Add a dashed line to your plot to show highlight the jump discontinuity.

Create a line plot of the first 100 primes.

Create a plot of Tan[(e^x)+y].Look at the documentation for ColorFunction and then colour your plot by height. Can you think of any other interesting colour functions you could add?

Plot 2(x^4)+(x^3)-13 (x^2)+6x for x between -5 and 5. Use the Solve function to find the smallest root of this polynomial.

More Mathematica

This document has a title and two sections. Add some subsections to the work you have done in the first section to separate the questions into a few topics (you can choose how to split them). Change the colour of the question text to a shade of gray so that it is more distinguished from the Mathematica code you’ve written. Add some new text cells in another colour with some comments explaining how some of your code works.

Continue adding subsections and changing the colour of text and adding explaining comments as you work through this section. It will make your document much easier to look back through later.

The first function I defined in the talk was

myFunc[x_,y_]:=x^2+x*y-3

run the cell above which contains the definition, and then try using this new function in a new cell below this text.

Try changing the definition of myFunc. Can you make it into a 3 argument function? What happens if you then try to call it with 2 arguments?

The next functions I defined in the talk were

padToLength24[vec_]:=Join[Table[0,24-Length[vec]],vec]

toBinaryCodeword[n_]:=padToLength24[IntegerDigits[n,2]]

try using these functions to see how they work. Can you define a new function that converts an integer into its base 3 representation, padded to 12 digits long. You might want to define two new functions to do this.

Can you change your padding function to use Join as an infix function rather than as a prefix function?

In the first section you created some 4x4 matrices. Use the function MatrixForm as a postfix function to display these matrices nicely. You could also try repeating the simplifications we did in the first section using Simplify as a postfix function.

When you do your final year project, you will probably write the final report in LATEX. You might want to be aware of the function TeXForm, which can be used as a postfix function. Try calling myFunc[p,3] and use TeXForm to get the LATEX form of the answer.

Define a function call comm which takes 2 matrices A and B and returns their commutator A.B - B.A displayed in matrix form.

Define your own function which computes Cos[(x^(1/2))+(x/16)].Plot your function for  0 ≤ x ≤ 100. Using what you learnt about exclusions in the first section, highlight the maxima of the function in this interval in red.

What is the FullForm of your function comm applied to two matrices?

Use Apply to find the maximum from a list of 10 random primes between 2 and 1000 created using Table.

Define a quadratic polynomial function called quad which takes a single argument x. Use Map to compute a list of the values of your function applied to all the divisors of 24.

Let S1, S2 and S3 be the three Pauli matrices. Form the linear combinations Tp = (((S1 + i S2))/2), Tm = (((S1 - i S2))/2), T3 = (S3/2). Using your comm function from above, define a new function which takes the commutator of a matrix with T3 (where T3 is the second argument to comm). Use Map to compute the commutator of each of Tp,Tm and T3 with T3 in turn. This can be made even nicer using  ‘pure functions’, but I’ll leave this for you to investigate in your own time.

Use a pattern to make your function comm[A,B] only defined when A and B are matrices.

Use the overloading idea to define comm[list,B], taking the commutator of each entry from the list with B in turn. Use this to repeat the question from above, computing the commutator of each of Tp,Tm and T3 with T3 in turn.

Make a dynamic plot of a*(((x-b))^2) for x between -5 and 5, where a and b can be controlled dynamically. Let them have default values of 1.

Make a dynamic plot of the cyclic graph on n vertices for n between 3 and 10. Add a checkbox to make it a directed graph.