Lab 5 Numerical Differentiation and Integration

Name:
Student #:

In this section, we wil explore the accuracy of taking derivatives numerically.

a)  Define and plot the function g(x)=(e^(-cos(x))).  Plot from x=0 to x=2.

Remove["Global`*"]

b) Use Mathematica to find the value of (dg/dx) at x=1.3 to 20 decimal places, and store this result in a variable called exact.  Recall the function N[ ] and that 1.3 is different from 13/10 as far as Mathematica is concerned.

c) Write a function that numerically calculates the derivative of an inputted function at a given point, using the forward difference approximation.  The 3 arguments of your function should be the function to be differentiated, the point at which to evaluate the derivative, and the stepsize.  Use your function to estimate g'[1.3] using a stepsize of h=0.1.  What is the relative error?

Fill in the blanks
DFor[  ,  ,  ]:=((f[x+h]-f[x])/h)

res = DFor[  ,  ,  ]

relerror=Abs[((res-exact)/exact)]

d)  Plot the absolute value of the relative error on a double log plot for your forward difference scheme as a function of h, with h ranging from 0.1 to (10^(-12)) in multiplicative steps of 0.1.  Use logarthmmic axes for both h and the error.  Note the minimum that appears as h becomes small.

Let's first familiarize how to deal with lists.  Try typing in and understanding the following commands.

Table[(2^i),{i,0,5}]

tab=Table[{i,(2^i)},{i,0,5}]

ListPlot[tab]

ListPlot[{tab, tab}, Joined -> {True, False}, PlotStyle -> {Blue, {Red, PointSize[0.02]}}]

ErrorTab=Table[{(10^(-i)),Abs[((DFor[g,1.3,(10^(-i))]-exact)/exact)]},{i,1,12}]

ListLogLogPlot[ErrorTab, AxesLabel -> {"Stepsize h", "Relative error"}]

For approximately what value of h is the most accurate value of the derivative obtained?

Answer:

In this section we will numerically differentiate and integrate discrete data.  You may need to refer to the notes for help.

a) First we shall generate some synthetic data with two columns: one for time in s, the other for velocity in m/s with some noise.

Remove["Global`*"]

velt = Table[{1. t, 6.2 - 9.8 t + Random[Real, {-0.2, 0.2}]}, {t, 0, 1, 1/10}]

ListPlot[velt, AxesLabel -> {"t [s]", "v [m/s]"}]

b) Write a function that calculates the derivative for data in a list, using the centred difference scheme.  Use your function to generate a list containing time and acceleration.  Make a graph of the acceleration of the car as a function of time.  The graph will be quite noisy.

Fill in the blanks
a[data_,i_]:=((data[[i+1,BLANK]]-data[[BLANK,2]])/(data[[BLANK,1]]-data[[i-1,1]]))

accel = Table[{velt[[i, 1]], a[BLANK, BLANK]}, {i, 2, Length[velt] - 1}];

ListPlot[accel,Joined->True,AxesLabel->{"t (s)","a (m/(s^2))"},PlotRange->{All,{-15,-5}}]

What is the mean acceleration?

Mean[accel]

Answer:

c) Using the trapezoid rule, write a function that integrates a list of data, from the beginning to an arbitrary end point.  Integrate the velocity data to find the displacement as a function of time assuming initial displacement is zero.  What do you determine the final displacement to be?

Fill in the blanks
DNITrap[list_,k_]:=(Δx=list[[2,1]]-list[[1,1]];((Δx)/2)(list[[1,2]]+list[[k,2]]+2(∑_(i=BLANK)^BLANK)list[[i,2]]))

DispTab = Table[{velt[[i, 1]], DNITrap[velt, i]}, {i, 2, Length[velt]}]

ListPlot[DispTab, Joined -> True, AxesLabel -> {"t [s]", "Displacement [m]"}, PlotRange -> All]

DispTab[[-1]]

Answer:

If there were no noise in the data, what would you expect the final displacement to be?
Integrate[FILL IN, {t, 0, 1}]

Answer: