matlab and mathematica for scientific computing
TRANSCRIPT
1
MATLAB AND MATHEMATICA FOR SCIENTIFIC COMPUTING version 1.0 August 2014 Alexandru NICOLIN, CS II ([email protected]) Mihaela Carina RAPORTARU, CS III ([email protected]) Horia Hulubei National Institute for Physics and Nuclear Engineering (IFIN-‐HH), Department of Computational Physics and Information Technologies (DFCTI), Magurele
2
Contents Introduction ................................................................................................................................................. 3 MATLAB ......................................................................................................................................................... 4 Command Window ............................................................................................................................... 4 M files ......................................................................................................................................................... 6 Basic programming .............................................................................................................................. 8 Matrices ..................................................................................................................................................... 9 Operations on arrays ........................................................................................................................ 11 Symbolic computations ................................................................................................................... 13 Plotting ................................................................................................................................................... 15 References ............................................................................................................................................. 19
MATHEMATICA ....................................................................................................................................... 21 Algebraic and differential equations ......................................................................................... 21 Numerical and analytic integration ............................................................................................ 23 Integral transforms ........................................................................................................................... 24 Matrices .................................................................................................................................................. 25 Special functions ................................................................................................................................. 28 Plotting ................................................................................................................................................... 29 Basic programming ........................................................................................................................... 32 References ............................................................................................................................................. 32
3
Introduction MATLAB and MATHEMATICA are two of the most widely used computing platforms in the scientific community. The computational kernel of these two platforms covers standard algebra, differential and integral calculus, as well as numerous other topics in advanced mathematics, both symbolically and numerically. Originally developed for scientific computing, they are now both versatile research instruments and efficient educational tools for budding scientists. These notes have been written for undergraduate students entering scientific research with the goal of offering them a brief introduction into the standard functions of MATLAB and MATHEMATICA. In writing these notes we have used our experience with the students who have worked in the Department of Computational Physics and Information Technologies of Horia Hulubei National Institute for Physics and Nuclear Engineering. The notes cover a broad set of subjects which go from linear algebra and basic programming in one end to differential equations and plotting on the other end. The selection of topics reflects, of course, our own interests in MATLAB and MATHEMATICA, but also the widely accepted strong points of these two computing platforms. The scope of these notes is not to provide a thorough presentation of a given subject, but rather to convey the fundamentals and show the extent to which MATHEMATICA and MATLAB can be used for scientific computing. The commented bibliographies at the end of each chapter inform the readers of the literature on MATLAB and MATHEMATICA and are an excellent starting point for those looking for detailed information. Interested readers can send their criticism and suggestions on these notes to the email address on one of the authors. Alexandru NICOLIN Mihaela Carina RAPORTARU Magurele, August 2014
4
MATLAB The official MATLAB (http://www.mathworks.com) description states that it is “a high-level language and interactive environment for numerical computation, visualization, and programming”. Another useful description of MATLAB is that from Ref. [4] where it is described as “a powerful, comprehensive, user-friendly, and interactive software package that is gaining acceptance as the ideal computational choice for scientists and engineers and is becoming an industrial standard, used to solve a wide range of problems in other diverse areas such as economics, business, technology, engineering, science, and education.” It covers both sequential and parallel computing and can be used with or without a computing infrastructure. The parallel computing toolbox takes advantage of multicore computers, graphical processing units (GPUs), computer clusters and grids, as well as cloud computing services. For users working dealing with computationally intensive tasks MATLAB offers the Distributed Computer Server which support batch jobs, parallele computations and distributed large data. It has its own built-in job scheduler but it also support the standard third-party schedulers. In addition to its own toolboxes which cover a wide range of topics that goes from aerospace science and bioinformatics on one end, to econo-physics and wavelets on the other end, one can also use third-party toolbox like the NAG Toolbox for MATLAB (http://www.nag.co.uk/numeric/MB/start.asp). The NAG toolbox “is the largest and most comprehensive single numerical toolkit that both complements and enhances MATLAB. The NAG Toolbox for MATLAB contains over 1,500 functions that provide solutions to a vast range of mathematical and statistical problems.” Unlike the majority of MATLAB functions, the NAG routines are already pre-compiled in Fortran so they are substantially faster than MATLAB’s own routines. In addition to third-party toolboxes MATLAB integrates well with COMSOL Multiphysics (http://www.comsol.com) and MATHEMATICA. The integration between MATLAB and COMSOL Multyphysics is done through LiveLink (http://www.comsol.com/livelink-for-matlab#overview), while that with MATHEMATICA is done through MATLink (http://matlink.org). There are two basic ways of using MATLAB to perform numerical computations: one isthrough the “Command Window”, the other through the M-file generator.
Command Window The Command Window is a line-by-line workspace where you can performsimple tasks such as elementary numerical manipulations, numerical (andsymbolic, if you have the right toolbox on your system) integration/differentiation,one- and two-dimensional plotting, matrix computations, etc. Most of these tasksrely on a wide-collection of pre-compiled (typically Fortran) functions that arebeing executed in the background. Let’s take for instance the following three-by-threematrix defined, row after row, as
5
mat=[1 2 3; 3 4 5; 6 7 8]. To compute the eigenvalues of this matrix one simply writes
eig(mat), while
[V,D]=eig(mat) returns V, a three-by-three matrix containing the eigenvectors of mat, and D,also a three-by-three matrix with zero off-diagonal elements whose diagonalelements are the eigenvalues of mat. To integrate a function f(x) one can write
quad(@(x) f(x), a, b), where a and b are numerical constants and f(x) depends solely on x. There are afew other numerical-integration functions in MATLAB but quad usuallydelivers good results quickly. To plot a function g(x) for x between a and b onefirst writes
x=a:h:b, where h is the size of the grid (smaller values of h resulting in finer plots), then ifg(x) is x2sin(x) we write
y=x.*x.*sin(x) where the . have been added because the operation applies to arrays. To displayto plot one writes
plot(x,y)
which will make a a graphic window pop up. Finally let us add that any MATLAB command-line (be it in the CommandWindow or in an M-file) can be followed by ; which inhibits the result of thatfunction from being displayed on screen. If, for instance, x=1:0.1:2 is notfollowed by ; then, in addition to x-array being created, it is also shown onscreen.
6
M files In order to take advantage of all that MATLAB has to offer one has to shift toworking with M-files. These files are composed of commands identical to thoseused in the Command Window that can now be grouped to form functions. Inaddition to these, MATLAB has a wide collection of programming functions(such as if, while, for, etc.). To understand the basic structure of an M-filelet us take this simple example:
function f=function_test a=1; for j=1:5 x(j)=j; y(j)=j*j+a; end plot(x,y)
In the example above we have constructed a function (named function_test)with the goal of plotting a parabola. In the above program a is a numerical variable equal to 1, j is an integer counter going from 1 to 5 while x and yare two arrays created element-by-element in the for-loop. M-files are run withF5. Let us now take a more complex example:
function f=function_test a=-‐0.4; time=0; h=0.01; x(1)=a; t(1)=0; j=1; while time<3 j=j+1; x(j)=x(j-‐1)+h*integral(x(j-‐1)); t(j)=t(j-‐1)+h; time=time+h; end plot(t,x); %-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ function f=integral(x) f=sin(x); %-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
The short script presented above solve a simple ordinary differential equation, x'(t)=f(x)=sin(x(t)) that is, using the so-called Euler method. Please notice that f(x) is given in the integral function while the main substance of the program isplaced infunction_test. In function_test a gives the initial condition and is
7
equal to -0.4, time keeps track of time, h is the (constant) integration time-step (here set to 0.01), x and t are two arrays constructed element-by-elementin which we keep the numerical solution of the equation, while j is merely acounter. One important comment is in order: if needed, MATLAB can be used as aprogramming language (for it has expressions and functions very similar to thosefound in languages such as Fortran, C, Java, etc.) and one can always write codesfrom scratch. If, however, one desires efficiency then one should take advantageof the considerable library of built-in functions (covering linear algebra,numerical integration and differentiation, special functions, interpolation and bestfitting theory, nonlinear or transcendental equation, dynamical systems, etc.) Putotherwise, there is no need to write, for instance, a complicate function for solvingtranscendental equations because this function already exists and it should betaken full advantage of! Of course, in the case of the explicit Euler method implemented above, there is no much difference between our code and MATLAB’s function, which looks like
function f=function_test [t,Y]=ode45(@integral,[0 10],-‐0.4); plot(t,Y); %-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ function dy=integral(t,y) dy(1)=sin(y); %-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
For more intricate problems this, however, becomes crucial! Let us take forexample Lorenz’ famous system of ordinary differential equations whose solutionis computer numerically by the following program:
function f=function_lorenz options = odeset('RelTol',1e-‐6,'AbsTol',1e-‐6); [t,Y]=ode45(@lorenz,[0 80],[-‐3 -‐44 17],options); figure(1); plot(t,Y(:,1)); figure(2); plot3(Y(:,1),Y(:,2),Y(:,3)); %-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ function dy=lorenz(t,y) sigma=10; r=28; b=8/3; dy=zeros(3,1); dy(1)=sigma*(y(2)-‐y(1)); dy(2)=-‐y(1)*y(3)+r*y(1)-‐y(2); dy(3)=y(1)*y(2)-‐b*y(3);
8
%-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
Please notice the “options” function which sets the relative and absolute error constraints. In our program both RelTol (the relative tolerance) and AbsTol(the absolute tolerance) were set to 10-‐6.
Basic programming MATLAB has all the functions that are standard in a programming language and one can write fairly complex codes with them. In the previous sections we have seen while and for loops and will now detail some of the other building blocks of a program. The if statement has the following general form
if expression statements elseif expression statements else statements end
which allows fairly complex evaluations. We however discourage the use of ifclauses in complex programs because they are very time consuming. Two other extremely important programming blocks are the break and continue functions. They are both useful in loops such as for and while and allow the user the stop the execution of the functions in the loop either permanently or for only during the current iteration. To understand the differences between these two functions let us consider the following two examples:
function break_test=void s=0; for j=1:10 if j==5 break else s=s+j; end end s
and
9
function continue_test=void s=0; for j=1:10 if j==5 continue else s=s+j; end end s
Both examples above attempt to compute the sum 1+ 2+⋯+ 10. In the first example the if clause breaks the sum after adding 1, 2, 3, and 4 due to the break function (i.e., all integers from 5 to 10 are excluded from the sum), while in the second one only 5 is excluded from the sum.
Matrices The mat in MATLAB does not come from mathematics, but from matrix. As MATLAB codes are not compiled (like a C, C++ or Fortran codes) a standard computation contained in an M file is very slow. One way to speed up the computation is to structure the computations in matrix form and take advantage of the internal matrix optimizations. To get an idea of how substantial the speed up is let us look at a series of simple operations. In the short program below we compute the product of two matrices of size 𝑁 each consisting only of elements equal to 1. The result will be an 𝑁 by 𝑁 matrix consisting only of elements equal to 𝑁.
function matrices=void N=1000; for i=1:N for j=1:N m(i,j)=1; end end tic for i=1:N for j=1:N s=0; for k=1:N
10
s=s+m(i,k)*m(k,j); end r(i,j)=s; end end toc tic rM=m*m; toc
In the first loop of the code we create the initial matrix, in the second loop we compute the product explicitly, while in the last section of the code we compute the product using MATLAB’s product function specifically designed for matrices. To time the distinct sections of the codes we use the ticstatements toc function which returns the time needed to execute the statements in seconds. For a matrix of size 10 the element-by-element computation of the product takes1 0.001748 seconds, while MATLAB’s function takes 0.000019 seconds. For a matrix of size 100 the first computation takes 0.028421 while the second one takes 0.000283 seconds. Finally, for a matrix of size 1000 the first computation takes 32.5 seconds, while the second one takes 0.186 seconds. These numbers may vary from one system to the other, but the main result is quite robust: MATLAB’s internal functions for matrices are orders of magnitude faster than the ones written by the users. Our code takes as example the multiplication of matrices, but the same holds for eigenvectors and eigenvalues, Schur decomposition, Hessenberg form of matrices, etc. One particular set of very efficient functions are those operating on sparse matrices. To illustrate their efficiency we present below a short code which computes the eigenvalue of a sparse matrix
function sparse_mat=void N=10000; p=0.025; for i=1:N for j=1:N temp=rand; if temp<p m(i,j)=1; else
1 All computations were performed on a laptop with an Intel i7 CPU, with 4 GB of RAM.
11
m(i,j)=0; end end end m; mS=sparse(m); tic eig(m); toc tic eigs(mS); toc
N is the size of the matrix, p shows the probability to have non-zero elements and sparse is the function which converts a regular matrix into a sparse one. For values of p close to 1 the two eigenvalues functions yield similar computing time. When, however, p is very small and N is very large the eigs function is substantially faster than eig. For example, when 𝑝 = 0.0025 and 𝑁 = 2500 the function designed for regular matrices returns the results after 15.23 seconds, while the one designed for sparse ones returns the result after 1.3 seconds2. More importantly, when eig is applied to large sparse matrices it has problems with the convergence of some of the eigenvalues, while eigs works flawlessly.
Operations on arrays As detailed above, MATLAB is optimized for working with matrices. In addition to the highly-optimized functions for eigenvalues and alike, there are also specialized function for sorting arrays, finding maxima and minima in un-sorted arrays and finding elements in un-sorted arrays. The sorting function sort arranges elements in ascending or descending order. The default order is ascending. To take a very simple example, let us consider the commands
A = [78 23 10 100 45 5 6] and
sort(A)
2 All computations were performed on a laptop with an Intel i7 CPU, with 4 GB of RAM.
12
which outputs
5 6 10 23 45 78 100 In its most general form the sorting function takes the form
B = sort(A, dim, mode)
where the sorting is done along the dimension of A specified by the scalar dim, and mode can takes the value 'ascend' or 'descend'. The function min finds the smallest elements in a given array. In its general form the function is called through
[C,I] = min(A,[],dim) where C stores the minimal elements, I stores the indices of the minimal values of A, and dim indicates the direction along which the minima are searched. For example, considering the array
x=[1 2 3; 4 5 2; 8 1 4; 7 0 9]
min(x,[],1)
returns
1 0 2 In this example dim was equal to 1, which imposed that the minimal values are searched column by column. The max functions works the same way. The find function returns the indices and values of nonzero elements which respects some constraints. In its simplest form, given the array
x=[1 2 3 0 0 1 2 3 8 0 9]
find(x)
returns the indices of nonzero elements, namely
1 2 3 6 7 8 9 11
For more refined searches one may impose additional conditions. For example,
13
find(x>2)
returns the indices of all elements bigger than 2, namely
3 8 9 11 while
find(x==7) returns the index of the element equal to 7, namely 5.
Symbolic computations Symbolic computing is not intrinsic to MATLAB but one can perform a series of standard analytic computations, such as differentiation, integration, simplification, and Fourier transforms, as well as to solve analytically algebraic and differential equations using the Symbolic Math Toolbox. The basic idea is that one first defines a series of symbolic variables using the syms command and then uses the symbolic functions that yield analytic results. To take a very simple example, let us solve analytically the algebraic equation 𝑥! + 𝑥! −𝑥 = 1. To this end we first define x as a symbolic variables through
syms x
and then solve the equation using the solve function
xsol = solve(x^4 + x^3 -‐ x == 1, x)
The resulting solution is xsol = 1 -‐1 (3^(1/2)*i)/2 -‐ 1/2 -‐ (3^(1/2)*i)/2 -‐ 1/2
One advantage of the symbolic computations is that one can integrate the results in Latex documents without the need of typesetting it manually. For instance, if one is interested in the first six terms of the series expansion of
14
sin (𝑥)3+ cos(𝑥)
one uses the taylor function through the command line
taylor(sin(x)/(3+cos(x)),6) which outputs
-‐ (7*x^5)/3840 -‐ x^3/96 + x/4 For more complex results one can cast the results in a more readable form using the pretty function. Let us take as an example the function
f=(x^2+x^3)/(sin(x)+2+x^5)
for which we compute the derivative using the diff function
diff(f)
As the initial result is rather complex and hard to read
(3*x^2 + 2*x)/(sin(x) + x^5 + 2) -‐ ((x^3 + x^2)*(cos(x) +
5*x^4))/(sin(x) + x^5 + 2)^2 we use the pretty function
pretty(diff(f))
which outputs
To integrate this effortlessly in a Latex document one can use the function latex latex(taylor(sin(x)/(3+cos(x)),6))
which outputs
-‐ \frac{7\, x^5}{3840} -‐ \frac{x^3}{96} + \frac{x}{4}
15
Finally, let us mention that in addition to symbolic function one can also convert numbers to symbolic objects. To do so, one has to use the sym command as
a = sym('2')
All computations which involve symbolic numbers result in symbolic numbers. For instance
sqrt(a) results in
2^(1/2)
Plotting MATLAB has one of the most versatile sets of plotting functions which include one-, two-, and three-dimensional plots. In the previous sections we have used the simple plot function which is illustrated below in full regalia.
function matrices=void x=0:0.1:5; plot(x,sin(x),'*',x,sin(2*x),'-‐',x,sin(4*x),'o')
The function depicts three plots on the same figures, using three distinct markers. For two-dimensional plots the most used function is surf which takes as input a matrix and two vectors. The matrix gives the amplitude of each point in the surface, while the two vectors define the axes. In the following example we show how to plot sin(k*x) as a function of both k and x
function matrices=void x=0:0.1:8; k=1:0.1:10; surf(k,x,sin(x'*k)) xlabel('k'); ylabel('x');
16
To label the axes we have used the the xlabel and ylabel functions. On can include mathematical symbols Greek letters and TeX characters on the labels of the axes through MATLAB’s TeX and LaTeX interpretor. To give just one example, if one want to write on a label
𝐴𝑒!!" sin𝛽𝑡 then one should include on the label
'{\itAe}^{-‐\alpha\itt}sin\beta{\itt} \alpha'
There is a very useful set of extensions of the surf function which includes the functions contour, contourf, and contour3. All these functions produce isolines of a given matrix and plot them in various ways, either in a plane or in a three-dimensional fashion. In the example below we produce the isolines of the matrix associated with the function sin(k*x) and show explicitly the isolines for 0, 0.25, 0.5, 0.75 and 1.
function matrices=void x=0:0.1:3; k=1:0.1:2; [C,h]=contour(k,x,sin(x'*k),[0,0.25,0.5,0.75,1]); set(h,'ShowText','on') xlabel('k') ylabel('x')
17
Finally, let us analyze the following short program which plots the surface generated by sin(k*x) as a function of k and x, and the surface normals. The surface normals are computed through the function surfnorm and the actual plotting is done through the function quiver3. Please notice that the plotting is done in two steps: first the surface is plotted and then the surface normals are plotted on top of the surface. To this end, we use the hold on command which makes it possible to plot one graph/surface on top of the other. Also, as the vectors generated by the quiver3 command are depicted in red by default we have chose to plot the surface in shades of gray, using the colormap function. Finally, let us add that one can make similar two-dimensional plots using the quiver 2 function.
function matrices=void [k,x]=meshgrid(-‐2:0.4:2,-‐1:0.25:1); Z=cos(k.*x); surf(k,x,z); hold on [U,V,W] = surfnorm(k,x,z); quiver3(k,x,z,U,V,W,'r'); xlabel('k'); ylabel('x'); zlabel('sin(kx)'); colormap gray
18
To plot symbolic functions in MATLAB one uses the symbolic counterparts of the standard plotting functions. For regular plotting one can use the ezplot function
ezplot(fun) ezplot(fun,[xmin,xmax])
which works with or without the explicit range. To take one simple examplelet us plot
sin (𝑥)3+ cos(𝑥)
through the command line
ezplot(sin(x)/(3+cos(x))) which outputs the plot below. Please notice that the ezplot automatically labels the x axis and produces a title for the plot with the plotted function. For other types of plots one can use ezsurf, ezsurfc and ezcontour.
19
References
1. Numerical methods in engineering with MATLAB, J. Kiusalaas, 2005, Cambridge University Press: This book is dedicated to engineering students interested in scientific computing for engineering problems. The selection of the numerical methods was done based on their scientific relevance and implementation efficiency in MATLAB. The book is structured in 10 chapters, dedicated to algebraic equations, interpolation and curve fitting, roots of equations, numerical differentiation and integration, matrix theory and optimization.
2. Basics of Matlab and beyond, A. Knight, 2000, CRC Press: The book originated from the author’s notes delivered to the Australian Defence Science and Technology Organisation. The two parts of the book correspond to the two modules of the course, each extending over two days. The first part (roughly 70 pages long) is ideally suited to the students who have not experience MATLAB before, with commands and functions briefly discussed as they are introduced. The second part is almost twice as long as the first one and addresses students who have already been exposed to MATLAB. It discusses more specialized topics such as sparse arrays, data manipulation, graphical user interfaces, irregular grids, programming, etc.
3. A guide to MATLAB: for beginners and experience users, B. R. Hunt, R. L. Lipsman, and J. Rosenberg, 2001, Cambridge University Press: The book covers most basic aspects of MATLAB as well as a series of more advanced ones in 11 chapters and three sets of applications. The book is clearly written and extremely well illustrated, which makes it perfect for unexperienced readers who are looking for a step-by-step introduction to MATLAB.
4. Practical Matlab basics for engineers, M. Kalechman, 2009, CRC Press: This bulky volume covers the basic and the advanced features of MATLAB which are relevant for engineers. The first chapter covers a detailed discussion on MATLAB
20
and the status of the market on scientific computing, while the following ones are dedicated to matrices and sets, trigonometric, exponential, logarithmic and special functions, complex numbers, polynomials and calculus (including a brief discussion on symbolic computing), programming elements, etc.
5. A guide to MATLAB object-‐oriented programming, A. H. Register, CRC Press: This is a book for the advanced user of MATLAB who is interested in object-‐oriented programming, a combination which is extremely unusual. The volume is full of line-‐by-‐line examples and contains a thorough discussion on the fundamental object-‐oriented programming concepts. All in all, this is a book unlike anything else on the market.
21
MATHEMATICA The following brief sections deal with the fundamentals of numerical and analytical computing in MATHEMATICA. Throughout the next few pages we will look at specific functions carefully designed to solve (either analytically or numerically) algebraic, transcendental and differential equations, and to compute one-, two- and three-dimensional (possibly singular) integrals. Let us start by noticing that MATHEMATICA belongs to the general class of Computer Algebra Systems, the so-called CAS, a class which includes well-known computing environments such as MATLAB (see the previous section), a package specifically designed for numerical (matrix) computations, Maple (see http://www.maplesoft.com), a popular software specialized in analytical computations, MathCad (see http://www.mathcad.com), Macsyma, Ocatve, MuPad and many more lesser-known ones. MATHEMATICA (see http://www.wolfram.com) is able to perform both analytical and numerical computations, the latter in arbitrary precision, provided, of course, one has enough CPU-time on his hands. Its strongest point, however, is analytical computing.
Algebraic and differential equations The basic function for solving equation is Solve. It comes in many flavors depending on the problem in hand. We have the common Solve function able to solve analytically simple algebraic equations, NSolve, a function designed for the numerical solution of complex algebraic equation and DSolve, a function designed for the analytical solution of simple dynamical systems. More complex dynamical systems can be tackled with using NDSolve, a function carefully designed for the numerical solution of more intricate (possibly stiff) systems. A related function is FindRoot – this function is specialized in finding the numerical solution of intricate transcendental equations and should be used whenever NSolve fails. Let us look at every one of these functions in detail. Given a function f(x) the Solve function works in the following way
Solve[f(x)==0,x]
wheref may be a function of other variables as well. If one for instance writes
Solve[x^2-‐a==0,x]
or in an equivalent notation
Solve[x2-‐a==0,x]
22
he/she finds the two roots of the equation given as a function ofa. Naturally, the NSolve function accepts no other parameters, therefore f should be only a function of x.
Solve[x5-‐1==0,x]
for instance, returns the five roots of the quintic equation above. If the equation becomes transcendental, say we have a function like
Sin[x]-‐x*Exp[x]
neither Solve nor NSolve will be able to solve it due to its intrinsic transcendental nature. In such cases one has to employ FindRoot. To do this, one has to provide the solver with some sort of guess, to be used internally by the numerical root-finder as starting point for its iterations. This implies that – unlike Solve and NSolve – the FindRoot routine is able to find only one root of f at a time! The general syntax of the routine is
FindRoot[f(x)==0,{x, xguess}]
In order to solve the previously mentioned equation one should therefore write something like
FindRoot[Sin[x]-‐x*Exp[x]==0,{x, -‐3}]
which gives us
x→-‐2.99074
Of course, other initial guesses will give us other solutions, for this equation has an infinite number of roots on the negative side of the x-axis. Finally, let us now analyze dynamical systems. But first, some notations: x is our variable, y is our function, the first derivative is represented by y'[x], the second by y''[x], etc. To solve a very simple differential equation like
y'[x]-‐x=0
one writes
DSolve[{y' [x]-‐x==0}, y, x]
where, again, the equal sign appears two time. Initial conditions can be specified as follows
DSolve[{y' [x]-‐x==0, y[0]==1}, y, x].
23
The differential equation previously discussed is, of course, only a very simple one. For more challenging dynamical systems analytical solutions are simply impossible and one has to employ NDSolve. Let us say that the differential equation in focus is given by
y' [x]-‐Sech[x2]=0
and that we also have the initial condition
y[0]=1.
Provided we are interested in the numerical solution over the interval [0,1] the NDSolve routine reads
NDSolve[{y' [x]-‐Sech[x2]==0, y[0]==1}, y,{x,0,1}].
In order to plot the result one should write
S=NDSolve[{y' [x]-‐Sech[x2]==0, y[0]==1}, y,{x,0,1}]
Plot[y[x]/.S, {x,0,1}]
Numerical and analytic integration
For analytical integration MATHEMATICA offers the Integrate function, which is able to compute one-, two- and three-dimensional integrals. For one-dimensional integrals the extra argument PrincipalValue-‐>True allows one to compute the principal value part, if that exists. Numerical computations are performed using the NIntegrate function which is identical to Integrate, except for the principal value part which in this case requires more work (there is, in fact, a special package for it) and we will not go into it. The interested reader can (and should!) consult the extensive Help files. Let us now look at a few examples. Simple one-dimensional integrals can be computed analytically writing either
Integrate[f(x), x]
or
Integrate[f(x), {x, a, b}].
The routine covers multi-dimensional integrals as well, in which case it reads
Integrate[f(x1,x2,...xn), {x, a1, b1},{x, a2, b2}...{x, an, bn}].
24
Whenever the Integrate function fails to give results in a reasonable time – and the function to be integrated depends solely on x– one can us the NIntegrate routine which is used precisely as one uses the Integrate function, of course, with an additional N in front.
Singular functions can also be integrated in a principal value sense by adding an additional PrincipalValue→True. Let us now take a few examples:
Integrate[x, x]
Integrate[x, {x, 0, 10}]
Integrate[x*Sin[y], {x, 0, 10}, {y, 0, 7}]
Integrate[Exp[x]/(1-‐x),{x, -‐3, 3}, PrincipalValue→True]
Other useful functions are Plot and Limit. The Plot is used as
Plot[f(x),{x, a, b}]
where f depends only on x, while a and b are numerical constants. Let us take a simple example:
Plot[Sin[x],{x,0,10}] The Limit function is used as
Limit[f(x), x→x*]
where, again, f depends solely on x and x* is some numerical value. While MATHEMATICA has one of the most advance engine for symbolic computations there are many cases in which executing a given line may return the same thing. On most cases this can be taken as a sign that there is no analytic result, but there are also cases in which breaking the computation into small pieces may yield the desired analytic result. This is particularly relevant for the Integrate function which has been seen to fail in the case of long functions3.
Integral transforms MATHEMATICA has an excellent set of functions for integral transform, the important of which are Fourier and Laplace transforms. For continuous Fourier transform one can use
3 Observation of the authors.
25
FourierTransform[expression, t, ω]
where t is time, ω is the frequency. For discrete Fourier transform one can use
Fourier[{u1,u2,…,un}]
and for inverse discrete Fourier transform
InverseFourier[{v1,v2,…,vn}]
The Laplace transform is obtained with
LaplaceTransform[expression, t, s]
and the multidimensional Laplace transform
LaplaceTransform[expression,{t1,t2,…},{s1,s2,…}]
Matrices
Matrices can be easily defined in MATHEMATICA using either the Basic Input panel or the lengthy notation
matrix={{1,2},{3,4}}
where {1,2} and {3,4} are the rows of matrix, a 2 by 2 real matrix. Similarly one can define a 3 by 3 matrix writing
matrix={{1,2,3},{3,4,5},{4,5,7}}
To compute the eigenvalues and/or the eigenvectors of a given matrix (named matrix from now on) one can choose between three distinct functions: Eigenvalues, Eigenvectors and Eigensystem. To call the first function one should write
Eigenvalues[matrix]
which would then return all eigenvalues of the matrix. If the matrix is extremely large it may come handy to find only the firstk largest eigenvalues, in which case one should write
Eigenvalues[matrix, k]
26
where k is a numerical constant. If one is interest in the first k smallest eigenvalues then k should be replaced by –k, that is
Eigenvalues[matrix,-‐k]
The Eigenvectors and Eigensystem functions work in a similar fashion.
Eigenvectors[matrix] returns all eigenvectors of the matrix while
Eigenvectors[matrix,k]
gives the eigenvectors corresponding the first k biggest eigenvalues. Naturally,
Eigensystem[matrix] returns all eigenvectors and eigenvalues of the matrix while
Eigensystem[matrix,k]
returns the first k biggest eigenvalues and their corresponding eigenvectors. As a simple example let us consider the three-by-three matrix
1 2 33 4 56 7 12
for which the eigenvectors are computed through
Eigenvectors[{{1,2,3},{3,4,5},{6,7,12}}] This yields a very surprising result, namely
27
{{−10951 +
12451 Root[8+ 5#1− 17#1! + #1!&,3]
−751Root[8+ 5#1− 17#1
! + #1!&,3]!,217−
3317Root[8+ 5#1
− 17#1! + #1!&,3]
+217Root[8+ 5#1− 17#1
! + #1!&,3]!, 1}, {−10951 +
12451 Root[8
+ 5#1− 17#1! + #1!&,2]
−751Root[8+ 5#1− 17#1
! + #1!&,2]!,217−
3317Root[8+ 5#1
− 17#1! + #1!&,2]
+217Root[8+ 5#1− 17#1
! + #1!&,2]!, 1}, {−10951 +
12451 Root[8
+ 5#1− 17#1! + #1!&,1]
−751Root[8+ 5#1− 17#1
! + #1!&,1]!,217−
3317Root[8+ 5#1
− 17#1! + #1!&,1]+217Root[8+ 5#1− 17#1
! + #1!&,1]!, 1}} This surprising result is due to the fact MATHEMATICA attempts to perform all computations symbolically by default. To change this, one can simply change the previous command to
Eigenvectors[{{1,2.0,3},{3,4,5},{6,7,12}}]
in which case the .0 in 2.0 (marked in red above) requires the result to be performed numerically. Similarly, if one searches for the largest eigenvector or the first two largest eigenvectors and uses the commands
Eigenvectors[{{1,2,3},{3,4,5},{6,7,12}},1] and
Eigenvectors[{{1,2,3},{3,4,5},{6,7,12}},2] will notice the following results:
{{−10951 +
12451 Root[8+ 5#1− 17#1! + #1!&,3]
−751Root[8+ 5#1− 17#1
! + #1!&,3]!,217−
3317Root[8+ 5#1
− 17#1! + #1!&,3]+217Root[8+ 5#1− 17#1
! + #1!&,3]!, 1}} for the largest eigenvector and
28
{{−10951 +
12451 Root[8+ 5#1− 17#1! + #1!&,3]
−751Root[8+ 5#1− 17#1
! + #1!&,3]!,217−
3317Root[8+ 5#1
− 17#1! + #1!&,3]
+217Root[8+ 5#1− 17#1
! + #1!&,3]!, 1}, {−10951 +
12451 Root[8
+ 5#1− 17#1! + #1!&,2]
−751Root[8+ 5#1− 17#1
! + #1!&,2]!,217−
3317Root[8+ 5#1
− 17#1! + #1!&,2]+217Root[8+ 5#1− 17#1
! + #1!&,2]!, 1}} for the first two largest eigenvectors. As before replacing 2 with 2.0 changes the result to its numerical form and we have, as expected, that the first two largest eigenvectors are
{0.22142001196653555,0.4028516187880904,0.8880786854443441}
and
−0.062223306916501325,−0.8292519697060646,0.5554002438016992 .
Special functions MATHEMATICA has state-of-the art implementations of the most used special functions. A survey of these function is outside the scope of these notes, but we would like to emphasize that “Hundreds of thousands of mathematical results derived at Wolfram Research give Mathematica unprecedented strength in the transformation and simplification of hypergeometric functions. This allows hypergeometric functions for the first time to take their place as a practical nexus between many special functionsóand makes possible a major new level of algorithmic calculus.” The quote from the MATHEMATICA Guide makes it very clear that they are the building block behind many of the symbolic calculations. To illustrate MATHEMATICA’s computational strength we show below a 50-level contour plot of the Gamma function
ContourPlot[Abs[Gamma[x + I y]], {x, -‐3, 3}, {y, -‐2, 2}, PlotPoints -‐> 50]
29
which is computed almost instantly on any desktop computer. The associated surface plot is determined through
Plot3D[Abs[Gamma[x + I y]], {x, -‐3, 3}, {y, -‐2, 2}]
which yields
Plotting MATHEMATICA has a broad set of plotting functions specifically designed for one-, two-, and three-dimensional plots. The simplest plotting function is Plot which is ideal for one-dimensional functions. As a first example let us look at
Plot[Sin[x], {x, 0, 10}, AspectRatio -‐> 1, AxesLabel -‐> {x, "Sin(x)"}]
which, when executed, produces the following graphic
30
What is special about the plotting functions in MATHEMATICA is the number of Options which can be added after the plotting interval. One can use as Options in the Plot functions the aspect ratio of the resulting image (AspectRatio, set to 1 in the above example), the labels of the axes (AxesLabel, set to x and Sin(x) in the above example), the color of the plotted function (ColorFunction), number of points in the plots (PlotPoints), the ranges on the axes (PlotRange), the type of plot (PlotStyle), the precision of the computations (WorkingPrecision), and many others. For surfaces one uses the Plot3D function which receives one additional argument. As a simple example let us look at function Sin[x*y] which is detailed below
Plot3D[Sin[x*y], {x, 0, 3}, {y, 0, 3}]
The Options that can be used with Plot3D are almost identical with those can be used with Plot, with the difference that the number of arguments is higher, due to the increased dimensionality.
2 4 6 8 10x
- 1.0
- 0.5
0.5
1.0
SinHxL
31
Finally, the last type of plotting functions includes those dedicated to parametric three-dimensional plots such as ParametricPlot3D. This function is suited for three-dimensional plots of both parametric curves and surfaces and produces impressive results. For example, let us look at the simple line below which produces a “squeezed” helix
ParametricPlot3D[{Sin[u], Cos[u], Sqrt[u]}, {u, 0, 40}] defined through its three components which are all functions of u. Please notice that the “squeezing” of the helix comes from the z-component which goes as Sqrt[u] and not as a linear function of u.
For parametric surfaces we follow the same pattern, but instead of only one we now have two parametric variables. As a simple example let us consider the line below which produces a torus-like object ParametricPlot3D[{Cos[u], Sin[u] + Sin[u]^2*Cos[v], Sin[v]}, {u,
0, 2 pi}, {v, -‐pi, pi}]
We show below the output of the command in two forms: to the left we have the standard output, while to the right we have the same surfaces rotated for improved clarity.
32
Basic programming MATHEMATICA can be used as a programming environment, though this is less common. The standard for and while loops take the form
For[start,test,increment,statements] and
While[test,statements] Similarly, the if clause takes the form
If[condition,t statements,false]
where the t statements are performed if the condition is true, and the f statements are performed is the condition is false.
References
1. An engineer’s guide to MATHEMATICA, E. B. Magrab, 2014, Wiley: The book offers an ample introduction into the numerical and symbolic/analytic solutions of some of the most used models in engineering, with examples coming from vibrations, fluid mechanics and aerodynamics, heat transfer, controls and signal processing, optimization, and structures. The volume includes exercises ranging in complexity and is accompanied by a website hosting the solutions to the problems.
33
2. An introduction to programming with MATHEMATICA, P. R. Wellin, R. J. Gaylord, S. N. Kamin, 2005, Cambridge University Press: As the name states, the book provides the reader with an introductory text focused on MATHEMATICA’s programming capabilities. The volume overviews the classical programming functions, also discussing numeric, procedural, rule-based, and front-end programming.
3. MATHEMATICA navigator: mathematics, statistics and graphics, H. Ruskeepaa, 2004, Academic Press: This bulky books provides a comprehensive view on what MATHEMATICA has to offer, its 27 chapters spanning everything from plotting and basic programming in a sheet to partial differential equations and probability theory. A very clear book. Highly recommended.
4. MATHEMATICA for theoretical physics, G. Baumann, 2005, Springer, 2 vols.: The two volumes dedicated to the use of MATHEMATICA in studying theoretical physics problems represent a unique set of lectures which show how one can replace almost all analytical computations usually done by hand, with very practical MATHEMATICA sheets, for roughly all subjects of theoretical physics. The volumes cover classical mechanics, thermodynamics, nonlinear dynamics, quantum mechanics, relativity, and fractional calculations. These books are a must-read for those looking to reduce the amount of work on theoretical physics calculations.