Basic Commands of Mathematica

How to enter commands: In general, just type the command, then "shift-enter" (or just "enter" if you use the key on the numeric keypad).

To execute all of the commands in this document: Press control-a (to select all), then press shift-enter. Do that now. When you do that, you will quickly see which lines below were my input lines, now labeled with "In[xx]", and which lines were produced by Mathematica as output, now labeled with "Out[xx]".

Good advice to remember: if you are doing your own Mathematica commands and nothing seems to be working, choose "Quit Kernel → Local" from the "Evaluation" menu. That clears the memory, and will restart the Mathematica engine the next time you enter a command.

The online help is very good. To find out more information about any of the commands that I present below, simply highly the command then press the F1 key.

Some useful functions

1+1

6 5

6^5

10^0.5

Sqrt[10]

N[%]

N[Sqrt[10],30]

N[Sin[10],30]

Things to observe : (1) +, -, *, /, and ^ all work pretty much as you'd expect. (2) Function calls such as Sqrt must use square brackets. (3) Pre-defined functions such as the square-root and sine functions must be capitalized. (4) The "N" command can be used to force a numerical decimal result. (5) The "%" symbol gets interpreted as "the results of the previously- executed command". (6) Commas are used to separate additional arguments for functions. The second argument to the N command, if used, specifies the number of decimal places to compute. (7) Trigonometric functions can only use radians.

How to define a function

f[x_] = - x^4 + 3 x^3 +  4 Sin[x]

Things to observe : (1) Function definitions must use the underscore symbol. (2) A space can be used for multiply. The asterisk symbol *  also works.

How to find a function value at a particular number

f[2]

N[%]

N[f[2]]

N[f[2],20]

Things to observe: As mentioned above, all function calls must use square brackets.

How to plot a function

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

Plot[f[x],{x,-2,4}, PlotRange->{-20,20}]

Things to observe: (1) Again, since it is a built-in function "plot" must be capitalized. (2) Again, all functions (in this case both "plot" and "f") must be called with brackets. (3) When a function argument contain multiple values rather than a single number, it is typically done via curly braces. In this case, the second argument of the plot command, "{x,-2,4}" specifies the variable to be plotted, the start of the plot range, and the end of the plot range. (4) There are various settings you can give to the Plot command via additional arguments; PlotRange is probably the most useful of them. It specifies the vertical range for occasions when the automatic vertical scale doesn't show you the range you want to see. The right arrow is produced automatically when you type "-" followed by ">".

How to plot two functions

f1[x_]=Sin[x]

f2[x_]=Cos[x]

Plot[{f1[x],f2[x]},{x,0,10}]

Things to observe: To plot two (or more) functions, just group the functions together with braces and use them as the first argument of the Plot command.

How to integrate a function symbolically

Integrate[f[x],x]

Integrate[(Sin[x])^2,x]

Integrate[f[x],{x,0,3}]

Things to observe: The second argument of the Integrate command ("x" vs "{x,0,3}", in the examples above) specifies whether you want an indefinite or a definite integral, and (if the latter) what the limits of integration are.

How to integrate a function numerically

NIntegrate[f[x],{x,0,3}]

Things to observe: (1) The "NIntegrate" command calculates the numerical answer to an integral, and can only handle definite integrals. (2) Mathematica is very sensitive to capitalization. Typing "Nintegrate" will not work at all.

Built-in constants

g[x_] = E^(-(x/Pi)^2)

Plot[g[x],{x,-10,10}]

NIntegrate[g[x],{x,-Infinity,Infinity}]

Things to observe: E, Pi, and Infinity are useful built-in constants. They must be capitalized because Mathematica essentially treats them as functions without arguments.

How to differentiate a function

fderiv[x_] =f'[x]

fsecondderiv[x_] = f''[x]

Things to observe: The tick mark  '  acts like a derivative function.  Two tick marks gives you a second derivative.  (The "D" function can also be used; see Mathematica's on-line help for more details if you like.)

How to find when a function value equals a specified number

FindRoot[f[x]==-10,{x,-1}]

FindRoot[f[x]==0,{x,3}]

Things to observe: (1) That's a double equals sign, ==, not a single equals sign, =. (2) The FindRoot command finds when the left side of the double equals sign is equal to the right hand side. If you type in "0" for the right hand side, it will find a root of the equation, but in general it is more flexible than that. (3) The second argument of the FindRoot command, for example "{x,-1}", specifies where you would like Mathematica to start looking. You can translate the first FindRoot command into English like this: "Find the place around x = -1 where f(x) equals -10."

How to find the maximum/minimum of a function

FindRoot[f'[x]==0,{x,2}]

Things to observe: I've simply combined two functions in one: first the derivative function, ', to compute the derivative of the function f, and then the FindRoot command to find where the derivative equals zero. I gave it x=2 as a starting point because I knew from the very first plot above that x=2 was close to where f(x) had a maximum.

Summations

Sum[1/n^2, {n,1,100}]

N[%, 20]

Sum[1/n^3,{n,1,1000}];

N[%,20]

ysum[x_] = Sum[ n x^n, {n,1,10}]

Things to observe: (1) The Sum command can be very useful to quickly compute numerical summations. It basically does exactly what you'd expect: the first summation above would be read as "Sum 1/n^2, with n ranging from 1 to 100". Specifically, the second argument (which must be in braces) tells Mathematica both the variable to sum over and what the summation range is. That's exactly how the second argument of the Integrate and NIntegrate commands behave. (2) A semicolon can be used at the end to suppress the output. (Try "Sum[1/n^3, {n, 1, 1000}]" without the semicolon.) (3) Sum can also be used to define functions which include symbolic summations.

Mathematica is far more powerful than what I've shown here, but at least this gives you an introduction into the basic commands you will need for Physics 123.