DiffEq 2 Mathematica crash course

Numbers

Numbers will be represented symbolically when possible

BasicExamples_1.gif

BasicExamples_2.gif

A floating point approximation can be computed using the N function.

BasicExamples_3.gif

BasicExamples_4.gif

You can go to arbitrary precision

BasicExamples_5.gif

BasicExamples_6.gif

BasicExamples_7.gif

BasicExamples_8.gif

You can apply the N function after an operation

BasicExamples_9.gif

BasicExamples_10.gif

Arithmetic

Addition, subtraction, multiplication, and division are defined as usual. Multiplication is implicit for symbols separated by whitespace

BasicExamples_11.gif

BasicExamples_12.gif

BasicExamples_13.gif

BasicExamples_14.gif

Warning: a missing whitespace can cause a subtle error.

BasicExamples_15.gif

BasicExamples_16.gif

BasicExamples_17.gif

BasicExamples_18.gif

This is a common beginner’s mistake. If you look carefully you can see that u v and uv are rendered in different fonts.

To be on the safe side, you can always use the “*” operator for multiplication

BasicExamples_19.gif

BasicExamples_20.gif

Math functions

The usual mathematical functions are defined. All built-in function names are capitalized. Function arguments are put in square brackets not parentheses.

BasicExamples_21.gif

BasicExamples_22.gif

BasicExamples_23.gif

BasicExamples_24.gif

BasicExamples_25.gif

BasicExamples_26.gif

Warning: See what happens if you forget to capitalize:

BasicExamples_27.gif

BasicExamples_28.gif

This error can be hard to detect. Notice that the word “sin” is rendered in a colored font (indicating that it hasn’t been defined yet). Notice also that Mathematica can’t recognize that sin[Pi] is zero, because the “sin” function (as opposed to the “Sin” function) isn’t defined.

Warning: See what happens if you use parentheses instead of square brackets:

BasicExamples_29.gif

BasicExamples_30.gif

All of your familiar friends are available as built-in functions:

BasicExamples_31.gif

BasicExamples_32.gif

Here are some new friends you’ll meet later this semester

BasicExamples_33.gif

BasicExamples_34.gif

BasicExamples_35.gif

BasicExamples_36.gif

Plotting functions of one variable

Plot a function

BasicExamples_37.gif

BasicExamples_38.gif

Plot two functions together

BasicExamples_39.gif

BasicExamples_40.gif

Make the plot a little prettier. See the documentation for lots of plotting options.

BasicExamples_41.gif

BasicExamples_42.gif

Defining your own functions

You can define your own functions very easily, as long as you pay attention to one small detail: put an underscore after the variable name.

The following defines a function f of a variable x

BasicExamples_43.gif

BasicExamples_44.gif

When I evaluate f at different arguments it does the right thing

BasicExamples_45.gif

BasicExamples_46.gif

Warning: the following defines the value of a function g at a specific argument value x.

BasicExamples_47.gif

BasicExamples_48.gif

When I try to evaluate g at different arguments it doesn’t know how to compute g except at the specific input x. This is probably not what you want.

BasicExamples_49.gif

BasicExamples_50.gif

Most of the time you’ll want to put an underscore after argument names when defining functions. See the documentation for further information about function arguments; if you want to get fancy you can define optional arguments, handle different argument types differently, set default values, and other neat tricks, but you’ll not need to do that for any problems in this course.

You can also make multivariable functions

BasicExamples_51.gif

BasicExamples_52.gif

and you can plot them in several ways:

BasicExamples_53.gif

BasicExamples_54.gif

BasicExamples_55.gif

BasicExamples_56.gif

Lists

I’ve been using lists enclosed in curly braces already. Here’s a little more information on them.

You can nest lists inside of lists:

BasicExamples_57.gif

BasicExamples_58.gif

You can access list elements by index

BasicExamples_59.gif

BasicExamples_60.gif

You can access elements by index relative to the end

BasicExamples_61.gif

BasicExamples_62.gif

Ranges of indices are specified with the double semicolon operator (analogous to Matlab’s colon operator)

BasicExamples_63.gif

BasicExamples_64.gif

You can index into more than one level

BasicExamples_65.gif

BasicExamples_66.gif

You can “flatten” a structured list into a simple list

BasicExamples_67.gif

BasicExamples_68.gif

Many operations “thread” over lists

BasicExamples_69.gif

BasicExamples_70.gif

BasicExamples_71.gif

BasicExamples_72.gif

BasicExamples_73.gif

BasicExamples_74.gif

Table: generating lists from a formula for the elements

We’ll often be given a formula for the i-th entry in a list, such as BasicExamples_75.gif

BasicExamples_76.gif

BasicExamples_77.gif

We can make 2D tables by iterating over two indices. The first index is for the rows, the second for the columns

BasicExamples_78.gif

BasicExamples_79.gif

Tables don’t have to be rectangular

BasicExamples_80.gif

BasicExamples_81.gif

Vectors

A vector is just a list

BasicExamples_82.gif

BasicExamples_83.gif

Access vector elements just like you do with a list: use x[[1]] to access the first element of a vector.

BasicExamples_84.gif

BasicExamples_85.gif

BasicExamples_86.gif

BasicExamples_87.gif

Vector addition and subtraction are done using the + and - operators

BasicExamples_88.gif

BasicExamples_89.gif

BasicExamples_90.gif

BasicExamples_91.gif

Dot product is done with the period (“.”) operator or the Dot function

BasicExamples_92.gif

BasicExamples_93.gif

BasicExamples_94.gif

BasicExamples_95.gif

The 3D cross product is done with the Cross function

BasicExamples_96.gif

BasicExamples_97.gif

What does the multiplication operator do?

BasicExamples_98.gif

BasicExamples_99.gif

How about the division operator?

BasicExamples_100.gif

BasicExamples_101.gif

BasicExamples_102.gif

BasicExamples_103.gif

BasicExamples_104.gif

BasicExamples_105.gif

Warning: Mathematica makes no distinction between row vectors and column vectors. This is a significant difference from MATLAB, which carefully distinguishes between the two. It’s convenient when you don’t care about the difference, but can sometimes cause trouble.  

Matrices

Matrices are just rectangular lists.

BasicExamples_106.gif

BasicExamples_107.gif

BasicExamples_108.gif

BasicExamples_109.gif

The usual matrix-matrix operations are defined:

BasicExamples_110.gif

BasicExamples_111.gif

BasicExamples_112.gif

BasicExamples_113.gif

Matrix-matrix multiplication uses the dot (“period”, “.”) operator, just like the dot product

BasicExamples_114.gif

BasicExamples_115.gif

Warning: be sure to use the dot, not * or implicit multiplication!

BasicExamples_116.gif

BasicExamples_117.gif

BasicExamples_118.gif

BasicExamples_119.gif

Matrix-vector multiplication also uses the dot operator

BasicExamples_120.gif

BasicExamples_121.gif

BasicExamples_122.gif

BasicExamples_123.gif

Warning: don’t forget to use the dot!

BasicExamples_124.gif

BasicExamples_125.gif

Linear algebra

You can solve A x=b

Choose a vector for the answer x

BasicExamples_126.gif

BasicExamples_127.gif

Multiply our answer by A to produce a RHS

BasicExamples_128.gif

BasicExamples_129.gif

Solve the system using the LinearSolve function

BasicExamples_130.gif

BasicExamples_131.gif

You can also solve the system numerically. This will be more efficient for large systems.

BasicExamples_132.gif

BasicExamples_133.gif

You can compute the inverse

BasicExamples_134.gif

BasicExamples_135.gif

and then use it to solve the system: BasicExamples_136.gif

BasicExamples_137.gif

BasicExamples_138.gif

I’ll explain in a few days why that’s usually not a good idea. For now, I’ll just tell you: you should always solve linear systems of equations using LinearSolve rather than multiplying by an inverse. And never, never, never use Cramer’s Rule.

Finding eigenvalues and eigenvectors

You can find eigenvalues and eigenvectors symbolically for simple enough matrices.

BasicExamples_139.gif

BasicExamples_140.gif

BasicExamples_141.gif

BasicExamples_142.gif

The Eigensystem function finds both together.

BasicExamples_143.gif

BasicExamples_144.gif

You’ll soon learn that it’s generally impossible to find exact eigenvalues and eigenvectors for matrices above size four-by-four, and even for three-by-three the exact eigenvalues might be messy to compute.

BasicExamples_145.gif

BasicExamples_146.gif

BasicExamples_147.gif

BasicExamples_148.gif

However, there are efficient algorithms for computing approximate eigenvalues numerically. Just give a floating point matrix to the Eigenvalues function and a good algorithm will be used.

BasicExamples_149.gif

BasicExamples_150.gif

(Unfortunately, at the level of this course I can’t explain to you the wonderfully ingenious algorithms used to compute approximate eigenvalues. Practical eigenvalue algorithms are nothing like the methods you’ll learn soon for simple hand calculations. But you can trust that Mathematica, MATLAB, and similar programs have good “black box” algorithms for finding approximate eigenvalues for matrices of dimension up to a few thousand rows.)

Polynomials

BasicExamples_151.gif

BasicExamples_152.gif

You can factor any polynomial that has integer roots

BasicExamples_153.gif

BasicExamples_154.gif

BasicExamples_155.gif

BasicExamples_156.gif

You can solve polynomial equations exactly (up to degree four, and in special cases higher degree):

BasicExamples_157.gif

BasicExamples_158.gif

BasicExamples_159.gif

BasicExamples_160.gif

Most polynomials of degree 5 or above can’t be solved exactly

BasicExamples_161.gif

BasicExamples_162.gif

BasicExamples_163.gif

BasicExamples_164.gif

In such cases, use NSolve to find a numerical solution

BasicExamples_165.gif

BasicExamples_166.gif

The Solve function will find complex roots:

BasicExamples_167.gif

BasicExamples_168.gif

Manipulating expressions

BasicExamples_169.gif

BasicExamples_170.gif

BasicExamples_171.gif

BasicExamples_172.gif

BasicExamples_173.gif

BasicExamples_174.gif

BasicExamples_175.gif

BasicExamples_176.gif

Calculus

BasicExamples_177.gif

BasicExamples_178.gif

Limits

BasicExamples_179.gif

BasicExamples_180.gif

BasicExamples_181.gif

BasicExamples_182.gif

Derivatives

BasicExamples_183.gif

BasicExamples_184.gif

BasicExamples_185.gif

BasicExamples_186.gif

BasicExamples_187.gif

BasicExamples_188.gif

Compute derivatives of BasicExamples_189.gif through order 10

BasicExamples_190.gif

BasicExamples_191.gif

Partial derivatives

BasicExamples_192.gif

BasicExamples_193.gif

Partial wrt x

BasicExamples_194.gif

BasicExamples_195.gif

Partial wrt y

BasicExamples_196.gif

BasicExamples_197.gif

Second partial wrt x

BasicExamples_198.gif

BasicExamples_199.gif

Laplacian: BasicExamples_200.gif

BasicExamples_201.gif

BasicExamples_202.gif

Indefinite integrals

Mathematica can do elementary integrals

BasicExamples_203.gif

BasicExamples_204.gif

BasicExamples_205.gif

BasicExamples_206.gif

Some may return results in terms of functions that probably aren’t familiar to you:

BasicExamples_207.gif

BasicExamples_208.gif

Some can’t be done exactly at all in terms of any known functions. If the result returned is a integral, that means Mathematica’s integration algorithms weren’t able to compute it.

BasicExamples_209.gif

BasicExamples_210.gif

Definite integrals

In this course we will be doing very many definite integrals (for reasons that will become clear later). So get used to working with the Integrate function!

You can do simple definite integrals easily with Mathematica

BasicExamples_211.gif

BasicExamples_212.gif

BasicExamples_213.gif

BasicExamples_214.gif

BasicExamples_215.gif

BasicExamples_216.gif

Complicated integrals might return unfamiliar functions

BasicExamples_217.gif

BasicExamples_218.gif

BasicExamples_219.gif

BasicExamples_220.gif

You can do improper integrals

BasicExamples_221.gif

BasicExamples_222.gif

Some integrals are too hard even for Mathematica. If the result is returned as an “undone” integral, that means Mathematica couldn’t find an exact result with its algorithms.

BasicExamples_223.gif

BasicExamples_224.gif

In such cases, numerical integration can be used.

BasicExamples_225.gif

BasicExamples_226.gif

Finally, if you try to integrate a function that hasn’t yet been defined, Mathematica won’t know what to do and will simply return an “undone” integral.

BasicExamples_227.gif

BasicExamples_228.gif

BasicExamples_229.gif

Laplace transforms

BasicExamples_230.gif

BasicExamples_231.gif

BasicExamples_232.gif

BasicExamples_233.gif

BasicExamples_234.gif

BasicExamples_235.gif

BasicExamples_236.gif

BasicExamples_237.gif

BasicExamples_238.gif

BasicExamples_239.gif

Differential equations

General soln

BasicExamples_240.gif

BasicExamples_241.gif

Initial value problem

BasicExamples_242.gif

BasicExamples_243.gif

Boundary value problem

BasicExamples_244.gif

BasicExamples_245.gif

The result is returned as a substitution rule. The expression

BasicExamples_246.gif

is a rule that, when executed, replaces the symbol y(t) by the stuff after the arrow.

Notice also that the result is a list of substitution rules. This is because, in general, differential equations can have more than one solution.

Digression: substitution rules

Here's a simple example or using a subst rule:

BasicExamples_247.gif

BasicExamples_248.gif

Here’s a more complicated example:

BasicExamples_249.gif

BasicExamples_250.gif

BasicExamples_251.gif

BasicExamples_252.gif

Working with DSolve solutions

Capture the result of DSolve into a variable. Select the first list element (since in this course we’ll always have unique solutions).

BasicExamples_253.gif

BasicExamples_254.gif

Now create a function using the symbol y(t) acted upon by the rule:

BasicExamples_255.gif

BasicExamples_256.gif

Warning: I always use different names to distinguish between the symbolic unknown y(t) and the solution ySoln(t). The reason is that once you define a value for y(t), then if you use that symbol in another call to DSolve Mathematica will evaluate all expressions involving it, including the equality tests such as y[0]==1. That evaluation will return True or False depending on the equation and the value of y[t]. DSolve won’t know what to do with that input, and will give you a strange error message. About all you can do at that point is restart Mathematica.

Long story short: never do something like
y[t_]=y[t] /. solnRule. Always use a variable name for your evaluatable solution function different from the name you used for the unknown.  

Warning #2: Be very, very careful to use double-equals operators (“==”) instead of single-equals (“=”) in defining your equations. Double equals tests for equality, single equals does an assignment (if you know C, C++, Python, Java, etc this will be familiar). If you use single equals the input to DSolve will confuse it. I know of no way to recover from this error except to restart Mathematica.

Spikey Created with Wolfram Mathematica 9.0