Assignment 4

Name:
Number:

Q1

Consider the velocity of a particle given as v(t) below.

v[t_]:=Exp[Sin[t]]

Plot[v[t],{t,0,5}]

a)

Using Mathematica’s built-in numerical integrator, plot the position of the particle from t=0 to 5, assuming x(t=0)=0.  What is the final position x(5)?

b)

Obtain and plot the position as a function of time from t=0 to 5, but now use the rectangle rule to integrate.  Use rectangles just narrow enough to recover x(5) form part (a) to 5 significant figures.  You will need to try different step sizes (widths), but keep h such that the number of rectangles is an integer.
  
What is the width of the rectangles (h) and how many rectangles are needed to get x(5) to the desired precision?

Store your result (the position as a function of time) in a list before plotting it.  Report data at every rectangle, i.e., if your rectangle width is 0.1 - which is way to big for this method - then store positions every 0.1 s.  The integration will take a little while.  

Plot using a red line with Joined->True option in ListPlot.

In solving this problem, it is probably a good idea to define a function that takes as its arguments 1) a function to integrate, 2) a step size and 3) a final time.

c)

Repeat part (b), except use the trapezoid rule.  What is h in this case?  It should be signifcantly larger than in part (b).

When plotting your result, use plot circles for data points, and use a red line to join them.  Use something like,

ListPlot[{posT,posT}, Joined->{True,False},PlotStyle->{Red,Black}]

d)

Repeat with Simpson’s rule.  Be careful!  Simpson’s rule requires an even number of slabs (odd number of points), so obtain your solution in steps of 2h.  Plot your results (circles) overtop of the results from part (a).  You can use the Show command.

Compared to part (b), h should be giant.

Does Simpson’s Rule make sense when t_final =0?

Q2

Using the same velocity function as in Q1, the following generates a list of data with some noise in it.

velTab=Table[{t,v[t]+RandomReal[{-0.001,0.001}]},{t,0,5,0.05}]

a)

Use the forward difference scheme, centred difference scheme and 5-point scheme

f'(x) = ((-f(x+2h)+8 f(x+h)-8f(x-h)+f(x-2h))/(12 h))

to generate and plot different estimates of the acceleration, using as many points as each method allows between t=0 and 5.  Differentiate the data directly (use velTab, not v[t]).  Be careful about end points.  The 5-point formula above is written assuming a continuous function, so you will need to make adjustments owing to the fact that you are working with discrete tabular data.

In the end, produce a plot of the three derivative estimates plotted over top of the analytic result v’(t).

Note that the function call Length[velTab] returns the number of elements in velTab.

b)

In the above plot, it might be difficult to tell which method gives a btter result.  To remedy this, plot the difference between the numerical derivative and v’(t).  You will need to make a table for each method that reports the quantity (numerical derivative at t)- v’(t).

Comment on your results.

c)

In part b) why does the five-point scheme (a higher order scheme) not give better results?

To help answer this, repeat the necesary parts of a) and b) to compare the centred difference scheme and the five-point scheme when no noise is introduced.  (Create velTab2 just as velTab was created, but omit the random part.)

To get a better comparison, report the absolute value of the difference between numerical and exact result (i.e., Abs[(numerical derivative at t)- v’(t)]) and plot this error on a logarithmic scale.