Excercise 2

Plotting

Simple plots

Plot the following functions
1. sin(x)
2. cos(x)
3. (e^x)
4. Random[]
4. sin(cos(x)) and (e^(sin(x)))in the same figure.
5. (Subscript[J, 0])(x),  (Subscript[J, 1])(x),  (Subscript[J, 2])(x), and (Subscript[J, 3])(x) where (Subscript[J, n])(x)is the Bessel function of order n, given in Mathematica by the function BesselJ[n,x].
6. Try plotting the factorial function, n!.

Solution

Plot[Sin[x], {x, 0, 2  π}]
Plot[Cos[x], {x, 0, 2  π}]
Plot[Exp[x], {x, -1, 1}]
Plot[Random[],  {x, 0, 1}]
Plot[{Sin[Cos[x]], Exp[Sin[x]]}, {x, 0, 
        2  π}]
Plot[{BesselJ[0, x], BesselJ[1, x], BesselJ[2, x], BesselJ[3, x]}, {x, 
        0, 10}]
Plot[n!, {n, 0, 3}]

Parametric plots

1. Plot the curve x(t)  =  (t^2) + 10  t + 1,  
      y(t) = (1/((t^2) + 10))in x,  y -coordinates.

2. Plot the curve (x(t),  y(t))  =  (cos(2 t), 
          sin(π t))in x, y -coordinates.

3*. Plot the function z = (e^(i t)),  
      0  <  t  <  2  π on the complex z-plane, that is, in (Re z,  Im z) coordinates. The functions Re[...] and Im[...] give the real and imaginary parts of a variable

Solution

ParametricPlot[{t^2 + 10  t + 1, 1/(t^2 + 10)}, {t, -2, 
        2}]
ParametricPlot[{Cos[2  t], Sin[Pi t]}, {t, -5, 
        5}]
ParametricPlot[{Re[Exp[I t]], Im[Exp[I t]]}, {t, 0, 
        2  Pi}]

3D and contour plots

1. Use Plot3D[...] and ContourPlot[...] to plot the following functions:
	a. (1/((x^2) + (y^2))), -5 < x < 5,  -5 < y < 5
	
	b. sin(x)cos(y)

Solution

Plot3D[
      1/(x^2 + y^2), {x, -5, 5}, {y, -5, 
        5}]
ContourPlot[
      1/(x^2 + y^2), {x, -5, 5}, {y, -5, 
        5}]
Plot3D[
      Sin[x] Cos[y], {x, -5, 5}, {y, -5, 
        5}]
ContourPlot[Sin[x] Cos[y], {x, -5, 5}, {y, -5, 5}]

Plot options

1. Using Mathematicas help system, find out how you can make coloured plots. Try it by plotting sin(x) and cos(x) to same figure with different colours. Can you also find a way to change the thickness of the lines?

2. Plot cos(2000 sin(x)), where 0 < x < π. Find out how you can increase the number of points used in the plot to produce a better plot.

Solution

Coloured plots are made by giving the Plot[...] command the option PlotStyle. Also, the PlotStyle option controls the thickness of the line. For example:

Plot[t^2, {t, -2, 2}, 
      PlotStyle -> Hue[0]]
Plot[{Sin[t], Cos[t]}, {t, -π, π}, 
      PlotStyle -> {Hue[0], Hue[1/3]}]
Plot[{BesselJ[0, t], BesselJ[1, t], BesselJ[2, t]}, {t, 0, 10}, 
      PlotStyle -> {RGBColor[1, 0, 0], RGBColor[0, 1, 0], 
          RGBColor[0, 0, 1]}]
Plot[{Exp[t/4], Exp[t/3], Exp[t/2], Exp[t]}, {t, 0, 1}, 
      PlotStyle -> {Thickness[0.001], Thickness[0.002], Thickness[0.01], 
          Thickness[0.05]}]

Use Mathematica help system to learn about the meaning of Hue[...], RGBColor[...], and Thickness[...].

Plot[Cos[2000  Sin[x]], {x, 0, π}]
Plot[Cos[2000  Sin[x]], {x, 0, π}, 
      PlotPoints -> 300]

Additional excersises

1. Mathematica knows many special functions. Try plotting some of the following:
	LegendreP[n,x], try with some integer values of n.
	HermiteH[n,x]
	LaguerreL[n,x]
	MathieuC[a,q,x], try for example  a = 3,  q = 2
	Zeta[s,a]
If you're interested, you can use the Mathematica help system to get more information about these functions.

2. Parametric 3D plot. Surfaces in three dimensions can be plotted by parametrizing the surface with two variables. As an example, try plotting the Möbius strip, given by the equations
	x(t, s)  =  [R + s cos((1/2) t)] 
          cos t
y(t, s) =  [R + s cos((1/2) t)] 
          sin t
z(t, s)  =  s sin((1/2) t),	0 ≤ t < 2  π,  -w ≤ 
        s < w,
where R is the radius of the strip and w its width. Use Mathematicas built-in function ParametricPlot3D[{x, y, z}, {s, (Subscript[s, start]),  (Subscript[s, end])}, {t, (Subscript[t, start]),(Subscript[t, end])}], where x,  y,  and z are the coordinates and s and t are the parameters. Try different values of R and w.

Solution

Plot[{LegendreP[1, x], LegendreP[4, x]}, {x, 0, 
        10}]
Plot[{HermiteH[2, x], HermiteH[3, x]}, {x, 0, 
        10}]
Plot[{LaguerreL[1, x], LaguerreL[2, x]}, {x, 0, 
        10}]
Plot[MathieuC[3, 2, x], {x, 0, 20}]
Plot[Zeta[s, 4], {s, 0, 5}]

R = 4;
w = 2;
ParametricPlot3D[{(R + s Cos[t/2]) Cos[t], (R + s Cos[t/2]) 
          Sin[t], s Sin[t/2]}, {t, 0, 2  π}, {s, -w, 
        w}]
Clear[R, w]

Defining functions

1. Define the function f(x) = cos(10 sin(x))and plot it over t ∈  [0, 2  π]
2. Define the function a(x, y) that calculates the average of two numbers x and y.
3. Define functions f(x) = (x^2), g(x) = exp(-x) and h(x)  =  g(f(x)). Plot the function h over -5 < x < 5.
4. Define a function that computes (e^(-((x^2) + (y^2)))) and plot it as a three dimensional plot.
5. Test the difference between delayed and direct assignment.
	i. Set a = 1
	ii. Define the function f1(x) = exp(a x) using direct assignment ('=' operator)
	iii. Define the function f2(x) = exp(a x) using delayed assignment (':=' operator)
	iv. Plot f1(x) and f2(x) for 0 < x < 2.
	v. Set a = 1/2
	vi. Plot f1(x) and f2(x) for 0 < x < 2.

Solution

Clear[f, g, h, x, y, a]
  
f[x_] = Cos[10 Sin[x]]
Plot[f[x], {x, 0, 2  π}]

  
a[x_, y_] = (x + y)/2

  
f[x_] = x^2
g[x_] = Exp[-x]
h[x_] = g[f[x]]
Plot[h[x], {x, -5, 5}]

  
F[x_, y_] = Exp[-(x^2 + y^2)]
Plot3D[
      F[x, y], {x, -4, 4}, {y, -4, 
        4}]

  
a = 1;
f1[x_] = Exp[a x]
f2[x_] :=  Exp[a x]
Plot[{f1[x], f2[x]}, {x, 0, 2}]
a = 1/2
Plot[{f1[x], f2[x]}, {x, 0, 
        2}]

Clear[f, a, g, h, F, f1, f2]

Equations

Polynomial equations

1. Solve the following sets of equations symbolically, and find the numerical values of the solutions:
a.	(x^3) + x + 1 = 0

b. 	(x^2) + (y^2) = 1, 
y = 
        2  x + 1

2. Use Eliminate[...] to eliminate the variable z from the following set of equations
	x + y + z = 0
x y z =  1
(x/z) = 2

3. Use the function Reduce[...] to find the roots of the following equations:
a.	(x^3) + p (x^2) + q (x^2) + r = 0

b.	x y  =  -2
x + y = 1

c.	x y  =  -2
x + y = 1
x  >  0

d.	(x^3) - a (x^2) + x - a = 0
x > 0

4. Find the general solution of a (x^4) +  b (x^3) + c (x^2) + d x + e = 0. The result be really complicated!

5. Find the general solution of (x^2) + p x + 1 = 0 and plot the first solution as a function of p.

6*. Find the general solution of (x^2) + p x + q = 0, make a function f(p, q) that gives the first solution, and finally plot the function as a three dimensional plot, for -15 < p < 15,  -15 < q < 15.

7*. Try what SolveAlways[...] does. Use it like Solve[...] on the second order polynomial equation a (x^2) + b x + c = 0.

Solution