UC SCI 111, vM+.
Notebook 4

Off[General::spell1];
Off[Break::nofwd];

Mathematica prerequisites.

Finding the primitive of a function : Integrate.

The basic command for finding primitives in Mathematica is Integrate.

f[x_] := Sin[x]
Integrate[f[x], x]

Exercise 3.
a) Find the primitives of the following pairs of functions

	sinh x		and 		cosh x
	sin x		and		cos x	
	
b) Compare the different Taylor approximations in x=0.
FYI:

	sinh x    := (1/2) ((e^x) - (e^(-x)) )
	cosh x   := (1/2) ((e^x) + (e^(-x)) )

If we write

Integrate[Tan[x], x]

Mathematica calculates

(∫_a^x)Tan[t] \[DifferentialD]t

for some value of a, usually a=0, though you can not rely on this.

Exercise 4.
Explain why Mathematica makes another choice of a in this case :

	Integrate[1/x,x]

There is a way to  choose the exact primitive yourself. In the next examples this is demonstrated.

Integrate[Sin[x], {x, (Subscript[x, 0]), (Subscript[x, 1])}]

Integrate[x^2  Sin[x], {x, (Subscript[x, 0]), (Subscript[x, 1])}]

Mathematica also understands that differentiation is the inverse of integration.

D[Integrate[unknownfunction[x], x], x]

Finding primitives of functions can be an arduous task. To illustrate this we give a pathological example that illustrates that, whilst using Mathematica, common, and not computer sense should prevail.

f1[x_] := Log[x + Sqrt[1 + x^2]]

D[f1[x], x]

% // Simplify

We define f2 to be the derivative of f1.

f2[x_] := 1/Sqrt[1 + x^2]

So if we integrate f2, we expect to get f1 plus some constant.

Integrate[f2[x], x]

But we seem to get something different. Let's plot both functions to see whether they are really different.

Plot[{ArcSinh[x], f1[x]}, {x, -1.5, 1.5}];

We see only one graph, so f1 equals ArcSinh. Funny enough, if we introduce a parameter in f1 Mathematica doesn't think about ArcSinh.

Integrate[1/Sqrt[a^2 + x^2], x]

Built-in numeric integration : NIntegrate.

In the exercises that follow we will ask you to program the methods you learned about, rectangular rule, trapezoidal rule and Simpsons' rule, yourself. This is not very difficult, but still you would expect that Mathematica has these routines already implemented inside. This is indeed the case.   The command is called NIntegrate. The main difference between Integrate and NIntegrate is that when using NIntegrate you will have to specify the integration interval as well as parameters, in terms of numbers  rather than symbols and likewise the outcome is a number rather than an expression.

NIntegrate[Exp[x], {x, 0, 1}]

NIntegrate uses all sorts of complicated algorithms to obtain a good result. You can also force it to use a particular method. Among the methods it knows about is the Trapezoidal rule.

NIntegrate[Sin[x], {x, 0, 1}, Method -> Trapezoidal, 
      PrecisionGoal -> 3, WorkingPrecision -> 13]

The previous expression has three options set. The first option is the rule Method -> Trapezoidal. That does what you would expect it to do. The other two options are for the numerical precision of the outcome. 

Precision in numerical Mathematica calculations is a somewhat confusing subject. In this context we can only give you a rule of thumb. If you want the first n digits to be correct in NIntegrate then set the PrecisicionGoal to n and the WorkingPrecisicion about ten higher.

NIntegrate[Sin[x], {x, 0, 1}, PrecisionGoal -> 13, 
      WorkingPrecision -> 23]

Exercise 5.
Use an integral and NIntegrate to compute ln(10) up to 10 correct digits.

Projects.

How long is a race track?

A car laps a race track in 84 seconds. The speed of the car at each 6-seconds interval is determined using a radar gun and given from the beginning of the lap, in ft/sec, by the entries in the following table:

({{time, speed}, {0, 124}, {6, 134}, {12, 148}, {18, 156}, {24, 147}, {30, 133}, {36, 121}, {42, 109}, {48, 99}, {54, 85}, {60, 78}, {66, 89}, {72, 104}, {78, 116}, {84, 123}})

This results in the following Mathematica function

\(\(speed[0]\  := \ 124;\)

        \(speed[6]\  := \ 134;\)

        \(speed[12]\  := \ 148;\)

        \(speed[18]\  := \ 156;\)

        \(speed[24]\  := \ 147;\)

        \(speed[30]\  := \ 133;\)

        \(speed[36]\  := \ 121;\)

        \(speed[42]\  := \ 109;\)

        \(speed[48]\  := \ 99;\)

        \(speed[54]\  := \ 85;\)

        \(speed[60]\  := \ 78;\)

        \(speed[66]\  := \ 89;\)

        \(speed[72]\  := \ 104;\)

        \(speed[78]\  := \ 116;\)

        speed[84]\  := \ 123\);

Exercise 6.
a) Plot the data.
Hint: use the Mathematica commands Table and ListPlot.
b) Use the rectangular rule to compute the length of the track.
Hint: use the command Sum.

The motion of a mass moving through a fluid.

A particle of mass  m  moves through a fluid and feels a viscous resistance  R, which is a function of the velocity v. The relationship between the resistance  R, velocity v, and time t  is given by the equation:
	
			t = (∫_(v ((Subscript[t, 0])))^(v (t)))(m/(R (v)))dv	

Suppose that  R(v)= -v(Sqrt[v])  for a particular fluid, where  R  is in  Newtons and v is in  m/sec .

Exercise 7.
If  m=10  kg  and  v(0) = 10  m/sec , approximate the time required for the particle to slow down to  v=5   m/sec :
a) Use the trapezoidal rule with stepsize  h=0.25 .
b) Use the Simpson rule with stepsize  h=0.25 .
c) Compare these approximations to the actual value.

Oscillatory integrals.

You have seen that the rectangular and the trapezoidal rule are both quite accurate. However, in physics one meets the so called oscillatory integrals. A standard example is

Plot[Cos[x^2], {x, 0, 10}];

When one tries to integrate this function numerically the outlined methods for numerical integration no longer work.

Exercise 8.
a) Use your own program for the trapezoidal rule to integrate numerically cos (x^2) from 0 to 20.
Try different stepsizes. (The idea being that an approximation is accurate if it hardly changes when the stepsize is halved.)
b) Integrate cos (x^2)  from 0 + \(TraditionalForm\`10\^\(-3\)\) to 20 + (10^(-3)).
c) Explain why the method does not work.