Skip to main content Skip to footer site map
Reza Malek-Madani

A Review of Useful Mathematica Commands

To plot the graph of the function y = sin(2x) + ln x, in the interval (1,5):


Plot[Sin[2x]+Log[x], {x, 1, 5}]
[Graphics:reviewgr2.gif][Graphics:reviewgr1.gif][Graphics:reviewgr2.gif][Graphics:reviewgr3.gif]

To label this graph:


Show[%, PlotLabel-> "Midshipman Joe Smith, 979797"]
[Graphics:reviewgr2.gif][Graphics:reviewgr4.gif][Graphics:reviewgr2.gif][Graphics:reviewgr5.gif]

To draw several graphs on the same screen:


Plot[Evaluate[Table[Sin[i x], {i, 1, 3}]], {x, -2 Pi, 2 Pi}]
[Graphics:reviewgr2.gif][Graphics:reviewgr6.gif][Graphics:reviewgr2.gif][Graphics:reviewgr7.gif]

To plot snapshots of a function of two variables (such as
u(x,t), where u is the temperature profile of a rod at x at
time t):


u[x_, t_] = Exp[-Pi t] Sin[Pi x/3];
Plot[Evaluate[Table[u[x, t], {t, 0, 1, 0.1}]], {x, 0, 3}]
[Graphics:reviewgr2.gif][Graphics:reviewgr8.gif][Graphics:reviewgr2.gif][Graphics:reviewgr9.gif]

To plot u(x,t), when u is found using the method of separation
of variables (e.g., u(x,t) = sum from i=1 to 20 of 1/i exp(-pi i t)
sin(pi i x/3)):


u[x_, t_] = Sum[1/i Exp[-Pi i t] Sin[Pi i x/3],{i, 20}];
Plot[Evaluate[Table[u[x, t], {t, 0, 1, 0.1}]], {x, 0, 3}]
[Graphics:reviewgr10.gif][Graphics:reviewgr11.gif]

To solve the system of differential equations
x' = sin x cos y, y' = -0.1 y- cos x sin y,
x(0) = 1, y(0) = -1, 
for t in the interval (0, 20):


sol = NDSolve[{x'[t] == Sin[x[t]] Cos[y[t]],
y'[t] == -0.1 y[t]- Cos[x[t]] Sin[y[t]],
x[0] == 1, y[0] == -1}, {x, y}, {t, 0, 20}];
ParametricPlot[Evaluate[{x[t], y[t]} /. sol], {t, 0, 20}]
[Graphics:reviewgr12.gif][Graphics:reviewgr13.gif]

To draw contours of a function of two variables (e.g., 
f(x,y) = sin 3x cos 2y):


ContourPlot[Sin[3 x] Cos[2 y], {x, -1, 1}, {y, -1, 1}]
[Graphics:reviewgr14.gif][Graphics:reviewgr15.gif]

To get levels with smoother boundaries:


ContourPlot[Sin[3 x] Cos[2 y], {x, -1, 1}, {y, -1, 1},
PlotPoints->40]
[Graphics:reviewgr16.gif][Graphics:reviewgr17.gif]

To draw a line between two points (0, 1) and (-1, 2):


Show[Graphics[Line[{{0,1},{-1,2}}]]]
[Graphics:reviewgr18.gif][Graphics:reviewgr19.gif]

To draw a line between the two points (0,1,0) and (-1, 2, 2):


Show[Graphics3D[Line[{{0,1,0}, {-1,2,2}}]]]
[Graphics:reviewgr20.gif][Graphics:reviewgr21.gif]

To draw a vector field (such as v = <y, -x>):


<<Graphics`PlotField`
a=PlotVectorField[{y, -x}, {x, -1, 1}, {y, -1, 1}]
[Graphics:reviewgr22.gif][Graphics:reviewgr23.gif]

To combine the above vector field with the contours of its
associated stream function psi = 1/2(x^2 + y^2):



b=ContourPlot[-1/2(x^2+y^2), {x, -1, 1}, {y, -1, 1}];
Show[b,a]
[Graphics:reviewgr24.gif][Graphics:reviewgr25.gif][Graphics:reviewgr26.gif]
go to Top